2013-03-26 216 views
1

我試圖從重寫URL:Servlet過濾器重寫URL

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

要:

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

Filter類代碼和映射在下面。我怎樣才能完成這項任務? (一個例子的答案將不勝感激)。

過濾器映射:

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

回答

1

我會建議使用現有的實現,而不是你自己寫的。

似乎有一個 Java URL重寫實現,這是Tuckey的URLrewriteFilter。

參見:http://tuckey.org/urlrewrite/

這應該做你想要什麼,以及更多。

或者,如果您在web容器前使用apache,則可能需要查看 mod_rewrite,它在apache上執行相同的操作。

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

+0

我明白reluctancy推倒重來,但我不能相信任何人都會認爲在servlet容器之前添加Apache只是爲了重寫URL而不是用幾行代碼創建一個簡單的過濾器。 – stepanian 2016-04-06 06:44:06

+0

@stepanian是的,這是過度的,這就是爲什麼我首先提出Java解決方案,並說如果你已經有Apache(無論出於什麼原因),mod_rewrite可能是一種替代方法。 – marc82ch 2016-04-07 11:49:34

1

如果你真正需要的是再使用正則表達式或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()); 
    } 
}