NabRaj_Baitadi
NabRaj_Baitadi

Reputation: 95

servlet filter to rewrite URL

I am trying to rewrite a URL from:

To:

Filter class code and mapping is below. How can I accomplish this task? (An answer with an example would be highly appreciated).

Filter mapping:

<filter>
    <filter-name>RequestFilter</filter-name>
    <filter-class>com.abc.ms.email.filter.RequestFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>RequestFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Filter code:

public class RequestFilter implements Filter {
    private static final Pattern REWRITE_PATTERN = Pattern.compile("(^[1-9]\\d*)$");

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
        HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper((HttpServletRequest)req);
        String url = wrapper.getRequestURL().toString();
        String number = url.substring(url.lastIndexOf("/")).replace("/", "");
        Matcher m = REWRITE_PATTERN.matcher(number);
        if (m.find()) {
            RequestDispatcher dispatcher = wrapper.getRequestDispatcher("request?id=" + m.group(1));
            dispatcher.forward(req, res);
        } else {
            fc.doFilter(wrapper, res);
        }
    }
}

Upvotes: 3

Views: 11723

Answers (2)

Stewart
Stewart

Reputation: 18302

If all you really need is this one simple URL cleanup then using regex or URL rewrite modules maybe overkill.

A totally simple implementation might go something like this:

public class RequestFilter implements Filter {

    private static final String LOOK_FOR = "sendEmail/newEmail.pdf";

    private static final String REMOVE = "&emailAddress=";

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
        HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper((HttpServletRequest)req);
        String original = wrapper.getRequestURL().toString();
        if(original.contains(LOOK_FOR)) {
            String filtered = StringUtils.substringBeforeLast(original, REMOVE);
            RequestDispatcher dispatcher = wrapper.getRequestDispatcher(filtered);
            dispatcher.forward(req, res);
        } else {
            fc.doFilter(wrapper, res);
        }
    }
}

It is easily unit tested using org.springframework.mock.web.* classes:

public class RequestFilterTest {

    private RequestFilter filter;

    @Before
    public void setUp() {
        filter = new RequestFilter();
    }

    @Test
    public void testRedirect() throws IOException, ServletException {
        MockHttpServletRequest request  = new MockHttpServletRequest("GET",
                "/sendEmail/newEmail.pdf?request_id=23456&emailAddress=");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain fc = new MockFilterChain();

        filter.doFilter(request , response, fc);

        Assert.assertEquals("http://localhost:80/sendEmail/newEmail.pdf?request_id=23456", response.getForwardedUrl());
    }
}

Upvotes: 7

marc82ch
marc82ch

Reputation: 1004

I would recommend using an existing implementation, rather than writing this on your own.

There seems to be the one Java URL rewriting implementation, which is Tuckey's URLrewriteFilter.

See: http://tuckey.org/urlrewrite/

This should do what you want, and a lot more.

Alternatively, if you use apache in front of your web container, you might want to look into mod_rewrite, which does the same on apache.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Upvotes: 3

Related Questions