users@jersey.java.net

Re: WebPageContentRegex and Guice integration

From: Christian Helmbold <christian.helmbold_at_yahoo.de>
Date: Fri, 12 Nov 2010 03:15:17 -0800 (PST)

Hi Charlie

probably I'm too late with my response but I stumbled upon this thread,
because I had the same problem. The simple solution is to use a filter
instead of a Servlet:

      Map<String, String> params = new HashMap<String, String>();
      params.put("com.sun.jersey.config.property.WebPageContentRegex",
"/(images|css)/.*");
      filter("/*").through(GuiceContainer.class, params);

With this configuration no "file servlet" is needed.

Hopefully this is useful for someone with the same problem ...

Regards
Christian


> Re: WebPageContentRegex and Guice integration
> Sep 12, 2009; 01:05am — by
> Charlie Knudsen Charlie Knudsen
> Hi All,
> I figured I would follow up my questions with a fix that seems to work
> and my findings to maybe help somebody down the road.
>
> What the Jersey filter does is look at the incoming path and tries to
> matches it against the WebPageContentRegex regex. If it matches it
> sends the request to the default servlet and stops any processing. I
> think the reason this does not work when the Jersey-Guice plugin is in
> place is because the GuiceConfig Servlet is being served so the
> default servlet does not come into play so when Jersey defers
> processing a 404 error occurs. My understanding of how exactly this
> is working is not perfect so please correct me if I am wrong.
>
> The fix I put in place was to use the Guice servlet plugin to serve
> the regex for static files and never even send them to Jersey. To do
> this I changed the Guice ContextListener code to look like:
> //////////////////////////////////////////
> // bind web resources
> bind(GuiceResource.class);
>
> // this serves the static content
> serveRegex("/(images|css|jsp)/.*").with(DefaultWrapperServlet.class);
>
> // setup Jersey
> Map<String, String> params = new HashMap<String, String>();
> params.put("com.sun.jersey.config.feature.ImplicitViewables", "true");
> params.put("com.sun.jersey.config.feature.Redirect", "true");
> params.put("com.sun.jersey.config.property.packages",
> "net.cknudsen.jerseyexample.web");
> serve("/*").with(GuiceContainer.class, params);
> /////////////////////////////////
>
> For the DefaultWrapperServlet.class above for now I basically use the
> example found on the page
> http://stackoverflow.com/questions/132052/servlet-for-serving-static-content
> which simply wraps the default servlet of the container. However any
> servlet should work.
>
> Anyway I hope that helps somebody eventually, and is not painfully
> obvious to everyone but me. Also if what I did looks incorrect or has
> issues please let me know.
>
> Charlie
>
>
> On Wed, Sep 9, 2009 at 5:55 PM, Charlie Knudsen
> <[hidden email]> wrote:
>
>> Hi All,
>> I was wondering if anybody knows how to use the property
>> "com.sun.jersey.config.property.WebPageContentRegex" when using the Guice
>> integration? Right now I have a very basic web application running that
>> is
>> basically shown
>> at
>> https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
>> however
>> when I try to integrate the WebPageContentRegex property I get a 404
>> error
>> when trying to view a css page at /css/test.css. Note that the page does
>> work when I use the regular Jersey filter info in the web.xml file
>> instead
>> of the Guice version. Right now I am working with Jersey 1.0.3.1.
>> I am configuring Guice integration as shown below
>> (net.cknudsen.jerseyexample.WebConfig):
>> @Override
>> protected void configureServlets() {
>> // web resources
>> bind(GuiceResource.class);
>> Map<String, String> params = new HashMap<String, String>();
>> // --------
>> // the next three lines are things I have
>> tried to change, but they don't seem to matter.
>> //params.put("com.sun.jersey.config.feature.ImplicitViewables", "true");
>> //params.put("com.sun.jersey.config.feature.Redirect", "true");
>> //params.put("com.sun.jersey.config.property.packages",
>> "net.cknudsen.jerseyexample.web");
>> // --------
>>
>> params.put("com.sun.jersey.config.property.WebPageContentRegex",
>> "/(images|css|jsp)/.*");
>> serve("/*").with(GuiceContainer.class, params);
>> }
>> });
>> In my web.xml file I have:
>> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns="http://java.sun.com/xml/ns/javaee"
>> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>> id="WebApp_ID" version="2.5">
>> <listener>
>>
>> <listener-class>net.cknudsen.jerseyexample.WebConfig</listener-class>
>> </listener>
>> <filter>
>> <filter-name>GuiceFilter</filter-name>
>>
>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>> </filter>
>> <filter-mapping>
>> <filter-name>GuiceFilter</filter-name>
>> <url-pattern>/*</url-pattern>
>> </filter-mapping>
>> </web-app>
>> I am trying to deploy to Glassfish v3 prelude and using java 6.
>> Does anybody know what I am doing wrong or the best place for me to look
>> to
>> find a fix for this issue?
>> Thanks,
>>
>> Charlie
-- 
View this message in context: http://jersey.576304.n2.nabble.com/WebPageContentRegex-and-Guice-integration-tp3615214p5731957.html
Sent from the Jersey mailing list archive at Nabble.com.