users@glassfish.java.net

Re: [Jersey] Pointing web app welcome-file to Jersey Application causes StringIndexOutOfBoundsException

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 15 Apr 2010 18:22:01 +0200

Hi,

I can reproduce this behavior on Tomcat 6, GF v3 and JBoss 5. But i do
and wonder if this is a bug. Including GF users list for more help.

When i have my own project set up to reproduce with the following in
the web.xml

     <servlet-mapping>
         <servlet-name>Jersey Web Application</servlet-name>
         <url-pattern>/webresources/*</url-pattern>
     </servlet-mapping>
     <welcome-file-list>
         <welcome-file>webresources/myresource</welcome-file>
     </welcome-file-list>

The servlet request information from the Jersey servlet is as follows:

   getContextPath: /mavenproject13
   getServletPath: /webresources
   getPathInfo: /myresource
   getRequestURL: http://localhost:8080/mavenproject13/
   getRequestURI: /mavenproject13/


This is confusing the heck out of Jersey because it expects the
request URI to be logically composed of:

   context path + servlet path + path info

(ignoring the case that such paths are in decoded form and the requets
URI is in the original encoded form)

It is like the web container is making an internal forwarding request
but with inconsistent information.

Is that the expected behavior?

Note that this whole area of managing this information from servlet is
very fragile. There are a number of edge cases to deal with. It is
actually damn hard to consistently produce the *encoded* base URI and
request URI from HttpServletRequest.

Paul.


On Apr 15, 2010, at 12:54 PM, Alex Treppass wrote:

> All,
>
> I've got my Web Project setup at /foo/WebProject/ and deployed to a
> Tomcat 5.5 server. My Jersey Application lives at /foo/WebProject/
> bar/ws/. I'm using Jersey 1.1.5.
>
> Wanting to have requests to foo/WebProject/ map to the response
> returned at /foo/WebProject/bar/ws/collections/list, I've added the
> following welcome-file- entry to my web.xml:
> <welcome-file-list>
> <welcome-file>ws/bar/collections/list</welcome-file>
> </welcome-file-list>
>
> This passes off to my Jersey webapp seamlessly, however when it gets
> to
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:
> 632), the requestURI variable is shorter than baseUri:
>
> baseURI: http://localhost:8080/foo/WebProject/ws/bar/
> requestURI: http://localhost:8080/foo/WebProject/
>
> This causes a StringIndexOutOfBoundsException further down when
> resolving the encoded Path at
> com
> .sun
> .jersey
> .spi.container.ContainerRequest.getEncodedPath(ContainerRequest.java:
> 359):
>
> java.lang.StringIndexOutOfBoundsException: String index out of
> range: -7
> at java.lang.String.substring(Unknown Source)
> at java.lang.String.substring(Unknown Source)
> at
> com
> .sun
> .jersey
> .spi.container.ContainerRequest.getEncodedPath(ContainerRequest.java:
> 359)
> at
> com
> .sun
> .jersey.spi.container.ContainerRequest.getPath(ContainerRequest.java:
> 352)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:984)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.service(WebComponent.java:
> 384)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:451)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:632)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
> ... (Catalina stack omitted for brevity).
>
> Offending code at ContainerRequest.java:359:
> return encodedPath =
> getRequestUri
> ().getRawPath().substring(getBaseUri().getRawPath().length());
>
> Has anyone come across this problem before? In the meantime I think
> I'm going to have to hook up a dummy servlet in my web.xml that
> takes an init-param "ws/bar/collections/list" and does a simple HTTP
> redirect to get around this.
>
> Regards,