3

我正在尝试从以下位置重写 URL:

  • localhost:8080/sendEmail/newEmail.pdf?request_id=23456&emailAddress=

至:

  • localhost:8080/sendEmail/newEmail.pdf?request_id=23456

过滤器类代码和映射如下。我怎样才能完成这项任务?(高度赞赏一个例子的答案)。

过滤器映射:

<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>

过滤器代码:

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);
        }
    }
}
4

2 回答 2

7

如果您真正需要的只是一个简单的 URL 清理,那么使用正则表达式或 URL 重写模块可能会矫枉过正。

一个完全简单的实现可能是这样的:

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);
        }
    }
}

它很容易使用org.springframework.mock.web.*类进行单元测试:

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());
    }
}
于 2017-03-14T23:42:18.613 回答
3

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

于 2013-06-18T19:48:21.243 回答