users@jersey.java.net

[Jersey] Problems with matrix parameters

From: Daniel Larsson <daniel.j.larsson_at_gmail.com>
Date: Fri, 15 Jul 2011 18:08:16 +0200

I'm quite fond of matrix parameters, and use them a lot in my URLs. It seems
there's some problem with traversing URLs containing embedded matrix
parameters, however. I started
getting java.lang.StringIndexOutOfBoundsException inside the jersey code,
traversing the resource graph for certain URLs.

Attached is a small maven project exposing this problem. The URL needs to be
specially crafted to expose the error, but I haven't had time to try figure
out what exactly goes wrong. I've turned on tracing, and the output when the
exception isn't thrown looks very odd, it seems to cut the URL in "random"
places.

Here's a curl command that exposes the problem (for me):

# curl -v '
http://localhost:8080/matrix-param-problem-1.0-SNAPSHOT/resources/admin/companies;matching=foo%20bar1%25/
'

This URL works, but gives odd trace messages:

curl -v '
http://localhost:8080/matrix-param-problem-1.0-SNAPSHOT/resources/admin/companies;matching=foo%20bar%25/
'
* About to connect() to localhost port 8080 (#0)
* Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> GET
/matrix-param-problem-1.0-SNAPSHOT/resources/admin/companies;matching=foo%20bar%25/
HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7
OpenSSL/0.9.8l zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition
3.1.1-b08 Java/Apple Inc./1.6)
< Server: GlassFish Server Open Source Edition 3.1.1-b08
< X-Jersey-Trace-000: accept root resource classes: "/admin/companies/"
< X-Jersey-Trace-001: match path "/admin/companies/" ->
"/application\.wadl(/.*)?", "(/.*)?"
< X-Jersey-Trace-002: accept right hand path
java.util.regex.Matcher[pattern=(/.*)? region=0,17
lastmatch=/admin/companies/]: "/admin/companies/" -> "" :
"/admin/companies/"
< X-Jersey-Trace-003: accept resource: "admin/companies;matchi" ->
@Path("/") com.example.matrixparamproblem.BaseResource_at_8810bb
< X-Jersey-Trace-004: match path "/admin/companies/" -> "/(admin)(/.*)?", ""
< X-Jersey-Trace-005: accept right hand path
java.util.regex.Matcher[pattern=/(admin)(/.*)? region=0,17
lastmatch=/admin/companies/]: "/admin/companies/" -> "/admin" :
"/companies/"
< X-Jersey-Trace-006: accept sub-resource locator: "admin/companies;matchi"
: "ng=foo" -> @Path("/{id: admin}/")
com.example.matrixparamproblem.BaseResource_at_8810bb.getSub(javax.ws.rs.core.PathSegment)
= com.example.matrixparamproblem.BaseResource$SubResource_at_102a1c8
< X-Jersey-Trace-007: match path "/companies/" -> "/(companies)(/.*)?", ""
< X-Jersey-Trace-008: accept right hand path
java.util.regex.Matcher[pattern=/(companies)(/.*)? region=0,11
lastmatch=/companies/]: "/companies/" -> "/companies/" : ""
< X-Jersey-Trace-009: accept sub-resource locator:
"admin/companies;matching=foo" : " bar%/" -> @Path("/{id: companies}/")
com.example.matrixparamproblem.BaseResource$SubResource_at_102a1c8.getSubSub(javax.ws.rs.core.PathSegment)
=
com.example.matrixparamproblem.BaseResource$SubResource$SubSubResource_at_1cb82
< X-Jersey-Trace-010: match path "" -> ""
< X-Jersey-Trace-011: accept resource methods: "admin/companies;matching=foo
bar%/", GET ->
com.example.matrixparamproblem.BaseResource$SubResource$SubSubResource_at_1cb82
< X-Jersey-Trace-012: matched resource method: public java.util.Map
com.example.matrixparamproblem.BaseResource$SubResource$SubSubResource.getJson()
< X-Jersey-Trace-013: matched message body writer: java.util.HashMap_at_308d9,
"application/json" ->
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider_at_8f5109
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Fri, 15 Jul 2011 16:02:20 GMT

The application is deployed in a glassfish 3.1.1-b08 container, with jersey
1.8-ea03. I compiled my source against the 1.8 release of jersey, but
haven't bothered to update the jersey version inside glassfish. The real
application is using 1.8 inside glassfish, so I don't think this version
discrepancy is the reason.