users@jersey.java.net

Re: UriBuilder

From: Richard Wallace <rwallace_at_thewallacepack.net>
Date: Fri, 07 Dec 2007 13:35:34 -0800

Marc Hadley wrote:
> On Dec 7, 2007, at 2:12 PM, Richard Wallace wrote:
>>
>> I'm trying to use the UriBuilder.fromResource(MyResource.class) to
>> easily build links to other resources. To my surprise, doing
>> something like the following:
>>
>> UriBuilder.fromResource(MyResource.class).build("resourcename");
>>
>> where MyResource has the @Path("/myresources/{name}") results in the
>> URI "http://:-1/myresources/resourcename" to being constructed. I
>> was a bit surprised by this, but being that the fromResource() and
>> other UriBuilder methods that return a builder instance are static,
>> they would have difficulty getting access to the ServletContext and
>> ResourceConfig to be able to properly build the full uris.
>>
>> Is there another utility method I should be using to do this or am I
>> going to have to come up with some other solution?
>>
> You can use UriInfo.getBaseUriBuilder() to get a builder initialized
> with the base URI of the application. You can then append the path to
> a specific resource using the path() method. I.e. to get an absolute
> URI for the resource above do something like:
>
> uriInfo.getBaseUriBuilder().path(Myresource.class).build("resourcename");
>
Thanks for the info. That's definitely what I'm looking for. Now the
question is, how can I get the UriInfo object in a MessageBodyWriter?

I've come up with about half a dozen ways that I can do it by using an
interceptor and putting the UriInfo object in a thread local, or
overriding ServletContainer and wrapping the WebApplicationImpl object
to catch the handleRequest() method and stuff the ContainerRequest in a
thread local. Or I could have my objects that I return from my resource
methods be something like a Result object that wraps the actual response
data and the UriInfo object.

Of those, the last is probably the one that I would be most likely to
pick, but it still seems a bit ugly to me. But if that's what I gotta
do, I guess that's what I gotta do.

Any other suggestions?

Thanks,
Rich
> Marc.
>
> ---
> Marc Hadley <marc.hadley at sun.com>
> CTO Office, Sun Microsystems.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>