Question

I am in the process of moving legacy code to a new implementation. The legacy code is hosted by a server with an "l" prefix for "legacy." As each page is moved over to the new server I want urlrewritefilter to change the url. So I need to do something like this:

from: http://lapp.company.com/page1.aspx to: http://app.company.com/pageOne

The situation is further complicated because this needs to work on several different environments (dev, stage, production) where the server name changes slightly in each case. For example:

local: http://applocal.companydev.com:8080 dev: http://app.companydev.com stage: http://app.companystage.com prod: http://app.company.com

Here's an example rule:

  <rule>
    <from>^/offers/buyKit.aspx$</from>
    <to type="redirect">%{context-path}/offers/buyKit</to>
  </rule>

When http://lapp.company.com/offers/buyKit.aspx comes in, I want it to be changed to http://app.company.com/offers/buyKit

Does urlrewritefilter support this kind of thing, or am I out of luck?

Was it helpful?

Solution

Here is the solution that I came up with. The rewrite filter allows you to specify a method that will be called when the rule is matched. I use this callback method to lookup the server context based on the current environment. This environment specific context is then set as an attribute of the request, which can be used by the "to" element of the rule.

Here is the updated rule from the xml file:

<rule>
    <from>^/offers/buyKit.aspx$</from>
    <run class="com.company.app.UrlHelper" method="setMyContext"/>
    <to type="redirect" last="true">%{attribute:myContext}/offers/buyKit</to>
</rule>

And here is the context setting method:

public class UrlHelper {

    private static final String APP_BASE_URL = "app.company.base.url";
    private Properties appConfig;

    public void setMyContext(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (appConfig == null) {
            appConfig = (Properties) StaticSpringApplicationContext.getBean("appConfig");
            Assert.notNull(appConfig, "Unable to get appConfig.");
        }
        String appBase = appConfig.getProperty(APP_BASE_URL);
        Assert.hasText(appBase, "Could not find property: " + APP_BASE_URL);
        request.setAttribute("myContext", appBase);
    }
}

It works quite well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top