Troubleshooting URL Rewriting in Spring Roo

Spring Roo automatically installs URL Rewriting for your application. If we want to configure another servlet in our web.xml file, we may need make configuration changes to the urlrewrite.xml file.


1. Enable Logging

Before you jump into log4j.properties file, you can simply enable logging right from within the Tuckey servlet filter configuration.

In thesrc/main/webapp/WEB-INF/web.xml file:

<filter>
   <filter-name>UrlRewriteFilter</filter-name>
   <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
      <init-param>
         <param-name>logLevel</param-name>
         <param-value>DEBUG</param-value>
   </init-param>
</filter>

2. Name your Filters

In the src/main/webapp/WEB-INF/urlrewrite.xml file:

<rule>
   <name>*** RULE:Resources ***</name>
   <from>/resources/**</from>
   <to last="true">/resources/$1</to>
</rule>

You will see the following in your log file:

INFO: org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: *** RULE: Resources *** (rule 0) run called with /myurl
INFO: org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: *** RULE: Static Web-Inf *** (rule 1) run called with /myurl
INFO: org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: *** RULE: static others *** (rule 2) run called with /myurl
...

Once a match is found, you will see the following:

INFO: org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: matched "from"

3. Display URL Rewriting Status

The Tuckey UrlRewriteFilter rules can be viewed when the server is running at the following url: http://localhost:8080/MyAppName/rewrite-status

Status is enabled by default. If we want to disable the status URL, add the following init-param to web.xml

<init-param>
   <param-name>statusEnabled</param-name>
   <param-value>false</param-value>
</init-param>

To change the url from rewrite-status to another string such as /my-cool-url-status

<init-param>
   <param-name>statusPath</param-name>
   <param-value>/my-cool-url-status</param-value>
</init-param>

NOTE: the URL must start with a slash.


Notes:

How do I change the log level in Roo?

We do not have to set the Roo logging level to DEBUG, but if you want more detail execute the following Roo command logging setup --level DEBUG
This will change your overall logging level from

log4j.rootLogger=error, stdout

to

log4j.rootLogger=DEBUG, stdout

Where is the Log File?

Roo, by default, configures a file appender named “R”, but does not enable it. To enable the file appender to write to application.log modify the log4j.rootLogger in the log4j.properties file as follows:

log4j.rootLogger=DEBUG, stdout, R

After restarting the server, the application.log file will appear in the root directory of our project.

How does the URL Rewriting work?

In the urlrewrite.xml file Roo has defined inbound URL pattern matching and one outbound rule. Tuckey is configured to convert almost all
of the inbound URLs prepending /app. If we look in web.xml we see the standard Spring DispatcherServlet is listening for all /app/* URLs.

Below is a simple, but cool, Balsamiq diagram, illustrating the URL rewriting:
URL Rewriting


Additional Reading

About Gordon

Technology enthusiast primarily focused on Java and Open Source projects. Spring Certified Professional and Trainer. http://twitter.com/gdickens http://linkedin.com/in/gordondickens http://github.com/gordonad
This entry was posted in Log4J, Roo, SLF4J, Spring, Spring Framework, Spring Roo and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>