users@jersey.java.net

Re: [Jersey] Jersey/Guice: Serving Static Content: what are the implications of both solutions ?

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 19 Aug 2010 10:20:27 +0200

Hi Pierre,

The use of a filter allows one to pass on the request to another
filter in the filter chain. Where as a servlet will consume the
request, it is like a filter that is always at the end of the filter
chain (but the servlet can forward or redirect as you do to serve
static files).

Because of the way the main Guice filter works it can do a similar
thing in terms of servlet dispatching as the Jersey filter
configuration. I have no idea why you need to get the path info to
null for the StaticWrapperServlet.

In Jersey 1.3 i added another feature to avoid having to specify a
regex:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html
#FEATURE_FILTER_FORWARD_ON_404

but there is a slight bug that has been fixed for 1.4-ea05. When the
Jersey filter forwards it does not reset the status code from a 404 to
a 200 which can cause issues on some Web containers.

Paul.

On Aug 18, 2010, at 8:39 PM, Pierre Radermecker wrote:

> Hello,
>
> I have been reading these discussions :
> http://jersey.576304.n2.nabble.com/Jersey-Guice-JSP-how-to-td5349718.html
> http://jersey.576304.n2.nabble.com/WebPageContentRegex-and-Guice-integration-td3615214.html
>
>
>
> So I have now two possibilities to serve static content and would
> like to know
> the implications of each (or simply which one is best):
>
> First I can simply use "filter" instead of "serve" as suggested in
> the first
> discussion:
>
> Map<String, String> params = new HashMap<String, String>();
> params.put(PackagesResourceConfig.PROPERTY_PACKAGES,
> "mypackage");
>
> params.put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX, "/.*\\.
> (html|xsd)");
>
> filter("/*").through(GuiceContainer.class, params);
>
> All my tests pass but I am wondering what it means exactly to use
> "filter"
> instead of "serve" ....
>
> The second possiblity is to go on using "serve" following the second
> discussion,
> get the same bug as describe in the first link and then finding a
> workaround I
> am not so sure about:
>
> bind(StaticWrapperServlet.class);
> serve("*.xsd", "*.html").with(StaticWrapperServlet.class);
> Map<String, String> params = new HashMap<String, String>();
> params.put(PackagesResourceConfig.PROPERTY_PACKAGES,
> "mypackage");
> serve("/*").through(GuiceContainer.class, params);
>
> @Singleton
> public class StaticWrapperServlet extends HttpServlet {
> public void service(HttpServletRequest req,
> HttpServletResponse resp)
> throws ServletException, IOException {
> RequestDispatcher rd =
> getServletContext().getNamedDispatcher("default");
>
> HttpServletRequest wrapped = new
> HttpServletRequestWrapper(req) {
> /**
> * I really don't know why this is needed and
> working ....
> */
> public String getPathInfo() {
> return null;
> }
> };
> rd.forward(wrapped, resp);
> }
> }
>
> The workaround is to return null when I override "getPathInfo". Is
> this safe ?
>
> Thanks for your help.
>
> - Pierre
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>