0

所以我希望用戶Tuckey的UrlRewriteFilter重定向試圖訪問舊版Web服務產品的用戶。簡單的東西,如重定向www.blah.com/oldversion/blah/blah到www.blah.com/newversion/blah/blah我如何正確設置Tuckey的UrlRewriteFilter?

我花了一點時間試圖正確設置這個根據有點模糊關於塔克的website的指示,但我有一些困難。

我在正確的位置有文件。 urlrewrite.xml位於webapp軟件包的WEB-INF文件夾中,與web.xml相鄰。 urlrewrite-3.2.0.jar位於WEB-INF的lib文件夾中。

這裏是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 

<filter> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
</filter-mapping> 
<filter> 
    <filter-name>Jersey Web Application</filter-name> 
    <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class> 
    <init-param> 
     <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> 
     <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value> 
    </init-param> 
    <!-- 
    <init-param> 
     <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name> 
     <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value> 
    </init-param> 
    --> 
    <init-param> 
     <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> 
     <param-value>project's.security.filter</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>project's.base.package</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>Jersey Web Application</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<ejb-local-ref> 
    REFERENCED EJB 
</ejb-local-ref> 

<ejb-local-ref> 
    REFERENCED EJB 
</ejb-local-ref> 
</web-app> 

這裏是我的urlrewrite.xml:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" 
    "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">--> 


<!-- 

Configuration file for UrlRewriteFilter 
http://tuckey.org/urlrewrite/ 

--> 
<urlrewrite> 

<rule> 
    <from>^/2.0/rest/$</from> 
    <to type="redirect">/3.2-SNAPSHOT/rest/$1</to> 
</rule> 
<rule> 
    <note> 
     The rule means that requests to /test/status/ will be redirected to /rewrite-status 
     the url will be rewritten. 
    </note> 
    <from>/test/status/</from> 
    <to type="redirect">%{context-path}/rewrite-status</to> 
</rule> 


<outbound-rule> 
    <note> 
     The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url) 
     the url /rewrite-status will be rewritten to /test/status/. 

     The above rule and this outbound-rule means that end users should never see the 
     url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks 
     in your pages. 
    </note> 
    <from>/rewrite-status</from> 
    <to>/test/status/</to> 
</outbound-rule> 

</urlrewrite> 

當我構建和部署項目,關於UrlRewriteFilter這些行打印服務器日誌:

[#|2011-12-06T10:28:56.924-0500|INFO|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=24;_ThreadName=Thread-2;|PWC1412: WebModule[null] ServletContext.log():org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: destroy called|#] 
... 
[#|2011-12-06T10:29:04.069-0500|INFO|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=24;_ThreadName=Thread-2;|PWC1412: WebModule[null] ServletContext.log():org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)|#] 

當我嘗試訪問2.0的URL時,它應該重定向到3.2-SNAPSH OT。相反,我收到一個404錯誤。沒有什麼顯示在server.log中,在我的Java錯誤日誌中,我發現UrlRewriteFilter包永遠不會被觸及。所以我認爲我的設置一定有一些不正確的地方。

讓我知道你是否需要更多的信息,並感謝您的時間和協助。

回答

0

這個問題最終與我的ear包的pom.xml有關。其中,應用程序的上下文根被設置爲包含我試圖更改的URI的一部分。由於應用程序只能在上下文根匹配時才能訪問,因此當然不能使用tuckey。

例如:用戶輸入URL www.blank.com/2.0/etc,我想重定向到www.blank.com/3.0/etc

不正確的耳朵POM片段:

<webModule> 
    <groupId>com.etc</groupId> 
    <artifactId>webservice-rest-webapp</artifactId> 
    <contextRoot>/3.0/</contextRoot> 
</webModule> 

正確的耳朵POM片段:

<webModule> 
    <groupId>com.etc</groupId> 
    <artifactId>webservice-rest-webapp</artifactId> 
    <contextRoot>/</contextRoot> 
</webModule> 

因爲我不再有指定的上下文根,我當然要修改整個程序爲我各種的URI路徑參數,包括版本號。