UrlRewriteFilter

repository·main·Indexed 43 days ago

https://github.com/paultuckey/urlrewritefilter

A Java Web Filter for J2EE compliant application servers such as Tomcat, Jetty, or Resin that allows URL rewriting before requests reach application code. Supports modern Servlet API (version 5.1.3) and older javax environments (version 4.0.3). Configured via urlrewrite.xml and web.xml, with support for custom RewriteMatch implementations.

Tokens
1.1K
Snippets
3
Records
5
Agent score
6%

What's inside UrlRewriteFilter

  1. Set up urlrewrite.xml configuration

    main
    UrlRewriteFilter is configured using an XML file named urlrewrite.xml. This file must be placed in your WEB-INF directory (for Maven projects, this is typically located at src/main/webapp/WEB-INF/). After adding the file and updating web.xml, you must restart the web application context for changes to take effect.
  2. View rewrite status

    main

    You can monitor the filter's output and status by visiting the following URL in your browser:

    http://127.0.0.1:8080/rewrite-status

    (Note: Replace 127.0.0.1:8080 with your local web application's address and context path. This status page is only accessible from localhost.)

  3. Install UrlRewriteFilter via Maven

    main

    To use UrlRewriteFilter in your project, add the appropriate dependency to your pom.xml.

    • For modern Servlet API environments, use version 5.1.3.
    • For older environments using Servlet API 4 or less (javax), use version 4.0.3.
    <!-- Modern Servlet API -->
    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>5.1.3</version>
    </dependency>
    
    <!-- Servlet API 4 or less (javax) -->
    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>4.0.3</version>
    </dependency>
  4. Configure UrlRewriteFilter in web.xml

    main

    Register the filter in your WEB-INF/web.xml file. It is recommended to place this configuration near the top of the file, above your existing servlet mappings. The filter should be mapped to /* and include both REQUEST and FORWARD dispatchers to ensure all incoming and internal requests are processed.

    <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>
  5. Implement a custom RewriteMatch

    main

    To extend the functionality of the UrlRewriteFilter, you can implement a custom match by extending the RewriteMatch class. You must override the execute(HttpServletRequest request, HttpServletResponse response) method.

    This method is called when the filter determines a match has occurred. It returns a boolean:

    • true if the match was successful and the rewrite process should continue or complete.
    • false if the match should be ignored.

    Inside execute, you can perform custom logic such as database lookups, setting request attributes, or performing a RequestDispatcher.forward() to a specific resource.

    import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    import jakarta.servlet.ServletException;
    import java.io.IOException;
    
    public class MyCustomMatch extends RewriteMatch {
        @Override
        public boolean execute(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
            // Custom logic here
            // e.g., request.setAttribute("key", value);
            // e.g., request.getRequestDispatcher("/path").forward(request, response);
            
            return true;
        }
    }