dev@jersey.java.net

Re: [Jersey] list operator in URI templates

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 05 Feb 2010 11:21:13 +0100

Hi Jim,

JAX-RS only supports a sub set of URI templates, specifically the set
originally defined in one of the earlier internet drafts before Joe
significantly advanced the spec:

   http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-01.html

Plus JAX-RS templates support a syntax for declaring regular
expressions when matching. The syntax is specified here:

   https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/
Path.html#value%28%29

Embedded template parameters are allowed and are of the form:
  param = "{" *WSP name *WSP [ ":" *WSP regex *WSP ] "}"
  name = (ALPHA / DIGIT / "_")*(ALPHA / DIGIT / "." / "_" / "-" ) ;
\w[\w\.-]*
  regex = *( nonbrace / "{" *nonbrace "}" ) ; where nonbrace is any
char other than "{" and "}"

The URI template internet draft was designed to produce URIs not to
match URIs hence there is a bit of a conflict. However there may be
more of a sub-set that could be included that is relevant to matching.
We could consider this for any JAX-RS 2.0 effort. If you like could
you log an issue against JSR 311?


Also of relevance here is JAX-RS by default supports matrix
parameters, so the ';' after a path segment signals zero or more
matrix parameters and they will be stripped from the path for matching
purposes. You can switch this behavior off by setting the following
feature to false:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceConfig.html
#FEATURE_MATCH_MATRIX_PARAMS

An alternative way is to do the following, for ',' separated values is:

     public static class CommaSeparatedList extends ArrayList<String> {
         public CommaSeparatedList(String s) {
             for (String e : s.split(",") ) {
                 if (e.trim().length() > 0)
                     add(e);
             }
         }
     }

     @GET
     @Path("foo/{types}")
     public Response testMethod(@PathParam("types") CommaSeparatedList
csl) {
         return Response.ok(csl.toString()).build();
     }


Hope this helps,
Paul.

On Feb 5, 2010, at 1:06 AM, Jim Webber wrote:

> Hi folks,
>
> I'm trying to use the list operator in a URI template. A simple
> example would be:
>
> @GET
> @Path("/foo/{-list|;|types}")
> public Response testMethod(@PathParam("types") String[] strings) {
> for(String str : strings) {
> System.out.println(str);
> }
>
> return Response.ok().build();
> }
>
> At runtime, I get an exception pointing to the "-" in the list
> operator being problematic:
>
> java.lang.IllegalArgumentException: Illegal character '-' at
> position 6 is not as the start of a name
> at
> com
> .sun
> .jersey.api.uri.UriTemplateParser.parseName(UriTemplateParser.java:
> 306)
>
> [snip]
>
>
> Can anyone shed any light on this at all?
>
> Thanks,
>
> Jim
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>