Question

So I am hoping to user Tuckey's UrlRewriteFilter to redirect users attempting to access older versions of a web-service product. Simple stuff like redirecting www.blah.com/oldversion/blah/blah to www.blah.com/newversion/blah/blah

I've spent a bit of time trying to properly set this up according to the somewhat vague instructions on Tuckey's website but am having some difficulties.

I have the files in the correct locations. urlrewrite.xml is in the webapp package's WEB-INF folder, adjacent to web.xml. The urlrewrite-3.2.0.jar is in the WEB-INF's lib folder.

Here is my 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>

Here is my 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>

When I build and deploy the project, these lines pertaining to UrlRewriteFilter are printed in the server log:

[#|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)|#]

When I attempt to access a url at 2.0, it should redirect me to 3.2-SNAPSHOT. Instead, I receive a 404 error. Nothing shows up in the server.log, and in my java error logs I see that the UrlRewriteFilter package is never touched. So I think there must be something incorrect about my setup.

Let me know if you need any more information, and thanks for your time and assistance.

Was it helpful?

Solution

The issue ended up being related to my ear package's pom.xml. In that, the application's context root was set to the include the piece of the URI I was attempting to change to. Because the application would only be accessed when the context root was matched, tuckey could of course not be utilized.

ex: user enters the URL www.blank.com/2.0/etc and I wanted to redirect to www.blank.com/3.0/etc

incorrect ear pom snippet:

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

correct ear pom snippet:

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

Because I no longer have that context root specified, I of course have to modify the path parameters for my various URIs throughout the program to include the version number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top