vipx / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Servlet match on a servlet path, not the actual request path #449

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Similar to ISSUE-418, the servlet should not match on getServletPath()
(because this is the path of the underlying servlet) but on the request URI. 

Original issue reported on code.google.com by hgsch...@gmail.com on 24 Nov 2009 at 8:46

GoogleCodeExporter commented 9 years ago
I have the same issue using Jersey (1.4) with Guice (from trunk) and the 
jersey-guice integration within OSGi. I do not use a web.xml descriptor, but 
register GuiceFilter programatically using the Apache Felix ExtHttpService. I 
basically have the following code in my activator:

Guice.createInjector(new RestModule());

try {
    httpService.registerFilter(new GuiceFilter(), "/.*", null, 0, null);
} catch (ServletException e) {
    e.printStackTrace();
}

The rest module binds a JerseyResource as follows:

protected void configureServlets() {
    bind(GuiceJerseyResource.class);
    serve("/rest/*").with(GuiceContainer.class);
}

The resource is annotated with @Path("/guicejersey"). So I would expect to be 
to access the rsource under http://localhost:8181/rest/guicejersey. However, 
that's not the case by default. If I use a match-all pattern (serve("*")) it 
matches when I access http://localhost:8181/guicejersey.

Some debugging showed me that the guice-servlet extension matches patterns 
agains servlet path, which seems to be empty all the time. I fixed the issue by 
changing ServletDefintion.java at two places:

In getPathInfo() I changed

pathInfo = 
getServletPath().substring(getContextPath().length()).replaceAll("[/]{2,}","/").
substring(servletPathLength);

to

pathInfo = 
getRequestURI().substring(getContextPath().length()).replaceAll("[/]{2,}", 
"/").substring(servletPathLength);

and in computePath() I changed

String servletPath = super.getServletPath();

to

String servletPath = super.getRequestURI();

I am actually not sure whether this breaks something else. But from my 
understanding of getServletPath and getRequestUri it seems to me that the 
latter is correct method to use. But I have hardly any experience with servlets 
so far, so I don't know exactly how the behaviour is in other contexts (e.g. 
the role of the contextPath).

Original comment by kodeman...@googlemail.com on 15 Oct 2010 at 10:16

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 22 Feb 2011 at 1:43

GoogleCodeExporter commented 9 years ago
You also want to apply the attached patch which makes sure that this actually 
works for non-root webapps, too.

Original comment by henn...@schmiedehausen.org on 22 Feb 2011 at 6:39

Attachments:

GoogleCodeExporter commented 9 years ago
handling as part of 418

Original comment by sberlin on 22 Mar 2011 at 3:16