5

I have two static wars filled with help files served by a Jetty server and a root context war.

  • help.war - English help files
  • help_CS.war - Czech help files
  • ROOT.war

    Based on the locale of the request, I want to divert a user to the language relevant to them. i.e. user requests /help/index.htm and as they are requesting from a Czech locale, they get /help_CS/index.htm. The idea is that language packs can be added as required without too much fuss.

    I tried adding a custom RewriteHandler, referred to in Jetty.xml which grabs the locale from the Request and either forwards or redirects on handle(). Both complain as response codes have been sent by this point...somehow?!

    I tried a custom Filter in the web.xml of the ROOT.war which I couldn't get to match */help/** no matter what variation of the url-pattern I tried.

    I then added a reference to the same Filter as the last attempt into WEB-INF/web.xml to my help.war which would match and URLS could be generated but I can't rewrite the URL at this point because it is always prepended by /help/ so the URL with help replaced with help_CS ends up as domain/help/help_CS/index.htm.

    So my question. How should/could this be done?

Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • 2
    Use javascript for it. if(isCS) {window.location.href = hostname/help_CS}, where isCS boolean variable. – Sergey Morozov Oct 17 '13 at 10:10
  • Good plan but I can't. The help files are dynamically generated by some third party software that allows very little customization. That would mean that if we one day have fifty languages the JavaScript would need to be hand entered for 50*n (n being number of languages) every time help is generated. – Ross Drew Oct 17 '13 at 10:21
  • 1
    You are right. Store your help files in subdirectories in main web application. I think it rigth way. – Sergey Morozov Oct 17 '13 at 10:35
  • Another good idea unaccessible to me. We distribute in wars and restructuring how all our help is built and deployed isn't an option :( – Ross Drew Oct 17 '13 at 10:41
  • 1
    What about sneaking `/../` into the rewritten URL? – artbristol Oct 17 '13 at 10:59
  • Forgot to mention I'd tried that too. Doesn't allow it. ;) – Ross Drew Oct 17 '13 at 11:07

1 Answers1

1

So! After a few days of messing around with this I've found something that feels a little hacky but it works.

I use my custom Filter then put it in WEB_INF/web.xml for each help_XX.war with an individual servlet-mapping (but otherwise identical) for each war file as so

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/help_CS</url-pattern>
</servlet-mapping>

Then inside the Filter I get the ServletContext of the required war and forward using that, manually removing /help from the request address like so

  @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                                throws IOException, ServletException
  {
    HttpServletRequest request = (HttpServletRequest) req;
    String requestAddress = request.getRequestURI();
    String country_code = req.getLocale().getCountry();

    if (requestAddress.contains("/help/")) 
    {
        ServletContext forwardContext = config.getServletContext().getContext("/help_" + country_code);

        forwardContext.getRequestDispatcher(requestAddress.replace("/help", "")).forward(req, res);
    } 
    else 
    {
        chain.doFilter(req, res);
    }
  }
Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53