如何正确设置Tuckey的UrlRewriteFilter?

时间:2011-12-06 19:14:52

标签: java web-services url-rewriting tuckey-urlrewrite-filter

因此,我希望使用Tuckey的UrlRewriteFilter来重定向尝试访问旧版Web服务产品的用户。很简单的东西,如www.blah.com/oldversion/blah/blah重定向到www.blah.com/newversion/blah/blah

我已经花了一些时间尝试根据Tuckey的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的网址时,它应该将我重定向到3.2-SNAPSHOT。相反,我收到404错误。在server.log中没有显示任何内容,在我的java错误日志中,我看到永远不会触及UrlRewriteFilter包。所以我认为我的设置肯定有些不正确。

如果您需要更多信息,请与我们联系,并感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:0)

这个问题最终与我的ear包的pom.xml有关。在那里,应用程序的上下文根被设置为包括我试图改变的URI片段。因为只有在匹配上下文根时才能访问应用程序,所以当然不能使用tuckey。

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

错误的耳塞片段:

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

正确的耳塞片段:

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

因为我不再指定上下文根,所以我当然必须在整个程序中修改各种URI的路径参数以包含版本号。

相关问题