users@jersey.java.net

Re: [Jersey] Arbitrary path length in service URL

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Tue, 07 Oct 2008 14:18:29 -0700

Chuck Harris wrote:
> ----- Original Message ----
>
>> From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
>>
> <snip>
>
>>>
>>>
>> This is where the regular expression matching comes in really handy.
>> Try something like this:
>>
>> @GET
>> @Path("/resources/service/{path:.+}")
>> public Response myMethod(@PathParam("path") String path) {
>> ...
>> }
>>
>> The "path" method parameter will absorb the entire set of elements after
>> /resources/service.
>>
>> Craig
>>
>>
>
> Craig,
>
> This is nearly exactly what I had tried originally, but I get the following exception stack trace:
>
> java.util.regex.PatternSyntaxException: Illegal repetition near index 18
> /resources/service/{path:\.+}
> ^
> java.util.regex.Pattern.error(Pattern.java:1730)
> java.util.regex.Pattern.closure(Pattern.java:2792)
> java.util.regex.Pattern.sequence(Pattern.java:1906)
> java.util.regex.Pattern.expr(Pattern.java:1769)
> java.util.regex.Pattern.compile(Pattern.java:1477)
> java.util.regex.Pattern.<init>(Pattern.java:1150)
> java.util.regex.Pattern.compile(Pattern.java:840)
> com.sun.jersey.api.uri.UriPattern.<init>(UriPattern.java:93)
> com.sun.jersey.api.uri.UriTemplate.<init>(UriTemplate.java:202)
> com.sun.jersey.impl.uri.PathTemplate.<init>(PathTemplate.java:73)
> com.sun.jersey.impl.model.ResourceClass.processSubResourceMethods(ResourceClass.java:225)
> com.sun.jersey.impl.model.ResourceClass.<init>(ResourceClass.java:107)
> com.sun.jersey.impl.application.WebApplicationImpl.newResourceClass(WebApplicationImpl.java:282)
> com.sun.jersey.impl.application.WebApplicationImpl.getResourceClass(WebApplicationImpl.java:253)
> com.sun.jersey.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:742)
> com.sun.jersey.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:609)
> com.sun.jersey.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:472)
> com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:541)
> com.sun.jersey.spi.container.servlet.ServletContainer.load(ServletContainer.java:465)
> com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:147)
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
> java.lang.Thread.run(Thread.java:636)Regardless of the way I tweak the pattern in the Path annotation, it always reports the error right before the opening brace character ("{").
>
> A simple version of my code that exhibits this behavior is:
>
> @Path( "/resources/service" )
> public class JsBeansResource {
>
> /** Creates a new instance of JsBeansResource */
> public JsBeansResource() {
> }
>
> @GET
> @Path( "/resources/service/{path:.+}" )
>
If you have a "/resources/service" @Path annotation on the class (as you
show above), you only want to capture the *rest* of the path with a
@Path annotation on the method. Try just this here:

    @Path("{path:.+}")
> @ProduceMime( "application/xml" )
>
Hmm, that means you're probably still on version 0.8 or earlier. I've
only tried this with 0.11 or later (i.e. the current trunk of Jersey
which is about to be 1.0), so your mileage may vary.
> public String getXml( @PathParam( "path" ) String path ) {
>
> return "<foo>" + path + "</foo>";
> }
> }
>
> I appreciate any insight into this you can grant me.
>
> Thanks,
> Chuck
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>