users@jersey.java.net

Re: [Jersey] Accessing Request Parameters in MessageBodyWriter?

From: Jan Algermissen <algermissen1971_at_mac.com>
Date: Mon, 05 Oct 2009 14:39:41 +0200

Paul,

thank you. A few questions though:

On Oct 5, 2009, at 11:08 AM, Paul Sandoz wrote:

> Hi Jan,
>
> You can inject UriInfo, and other JAX-RS artifacts (like HttpHeades,
> Request etc):

Yes, that is what I was thinking, but I missed the fact that I can
make it an instance variable and still access the per-request data via
hread local.

However, is this good/intended design at all? IIRC I read that (at
least for security information) going via thread local is not the best
thing to do.

Would it be better to create a wrapper around the 'business object'
returned from the resource method and to include the per-request data
there and then write a MessageBodyWriter that maps the wrapper object
instead of the business object?

(Sorry for having so many questions - I do deeply regret that I was
stuck in customer projects 2007/2008 and could not participate in the
JSR311 effort as I planned.)

Thanks,

Jan



>
> @Context UriInfo ui;
>
> My preference is for constructor injection:
>
> public MyMessageBodyWriter .... {
> private final UriUnfi ui;
>
> public MyMessageBodyWriter(@Context UriInfo ui) {
> this.ui = ui;
> }
> }
>
> But you can use field or method injection. Note that a thread local
> proxy is injected so if you try to access the "ui" instance outside
> the scope of a request an IllegalStateException will be thrown (e.g.
> if you try and do System.out.println(ui) in the constructor).
>
> Paul.
>
> On Oct 4, 2009, at 12:12 PM, Jan Algermissen wrote:
>
>> Can anyone provide a hint, how I access the actual circumstances
>> (e.g. parameters) of a request inside the applied
>> MessageBodyWriter's writeTo method?
>>
>> In my scenario, the produced body depends on some request parameters.
>>
>> Thanks,
>> Jan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>

--------------------------------------
Jan Algermissen

Mail: algermissen_at_acm.org
Blog: http://algermissen.blogspot.com/
Home: http://www.jalgermissen.com
--------------------------------------