users@jersey.java.net

UriComponent.validate does not allow brackets []?

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Thu, 17 Jul 2008 21:11:39 +0200

Hi,

we have a resource method that accepts some query parameter named fq
(filter query). The fq param accepts ranges for e.g. a price filter, so
that the value for the fq param might be "price:[160%20TO%20200]".

When a client performs a request with such a param value, the
UriComponent.validate says that this is not valid:

[ERROR] 17 Jul 2008 15:50:45,166 btpool0-1 com.sun.jersey.spi.spring.container.servlet.SpringServlet.service:
Caught exception.

java.lang.IllegalArgumentException: The string 'q=foo&fq=price:[160%20TO%20200]' for the URI component QUERY contains an invalid character, '[', at index 15
        at com.sun.jersey.api.uri.UriComponent.validate(UriComponent.java:95)
        at com.sun.jersey.impl.uri.UriBuilderImpl.encode(UriBuilderImpl.java:353)
        at com.sun.jersey.impl.uri.UriBuilderImpl.replaceQueryParams(UriBuilderImpl.java:286)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:273)

I think this should be valid, even UriComponent.decode handles this
correctly.

One possible patch I can think of is in
UriComponent.creatingEncodingTables:

Index: src/api/com/sun/jersey/api/uri/UriComponent.java
===================================================================
--- src/api/com/sun/jersey/api/uri/UriComponent.java (revision 1166)
+++ src/api/com/sun/jersey/api/uri/UriComponent.java (working copy)
@@ -243,6 +243,8 @@
         tables[Type.PATH.ordinal()] = creatingEncodingTable(l);

         l.add("?");
+ l.add("[");
+ l.add("]");

         tables[Type.QUERY.ordinal()] = creatingEncodingTable(l);
         tables[Type.FRAGMENT.ordinal()] = tables[Type.QUERY.ordinal()];

This test goes through then:

Index: test/com/sun/jersey/impl/UriComponentValidateTest.java
===================================================================
--- test/com/sun/jersey/impl/UriComponentValidateTest.java (revision 1166)
+++ test/com/sun/jersey/impl/UriComponentValidateTest.java (working copy)
@@ -54,4 +54,8 @@
         assertEquals(true, UriComponent.valid("/x20y", UriComponent.Type.PATH));
         assertEquals(true, UriComponent.valid("/x%20y", UriComponent.Type.PATH));
     }
+
+ public void testQuery() {
+ assertEquals(true, UriComponent.valid("fq=price:[1%20TO%20100]", UriComponent.Type.QUERY));
+ }
 }

Please notice that I took this diff in a branch, so line numbers might
be wrong.

Any thoughts on this?

Thanx && cheers,
Martin