Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a webapp forward to another webapp?

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?

like image 311
Ross Drew Avatar asked Nov 12 '22 20:11

Ross Drew


1 Answers

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);
    }
  }
like image 94
Ross Drew Avatar answered Nov 15 '22 05:11

Ross Drew