You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@wink.apache.org by Srivatsan Sridharan <sa...@gmail.com> on 2009/11/17 12:38:19 UTC

Context injection using @Resource in the resource class

Hi,

We are working on restful service development that has a request handler and
a resource class. Request handler sets an attribute on the MessageContext.
To my knowledge, the set attribute could be accessed in the resource class
if the entire context could be injected using @Resource annotation. 

1) Is this the right way of doing it as I dont see a support for resource
injection in the resource classes? 
2) If there is an alternate way to get a handle to the context in the
resource class, please let me know.

Thanks,
Srivatsan
-- 
View this message in context: http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
Sent from the Apache Wink Users mailing list archive at Nabble.com.

Re: Context injection using @Resource in the resource class

Posted by Nicholas L Gallardo <nl...@us.ibm.com>.
And on a side note, there's a line in the JAX-RS 1.1 update that explicitly
states we cannot require @Resource as a means of injection.  So, I agree
with Michael this using the JAX-RS @Context mechanism will be the most
portable solution.

-Nick



Nicholas Gallardo
WebSphere  - REST & WebServices Development
nlgallar@us.ibm.com
Phone: 512-286-6258
Building: 903 / 5G-016


                                                                           
             Michael Elman                                                 
             <elman@apache.org                                             
             >                                                          To 
                                       wink-user@incubator.apache.org      
             11/17/2009 07:20                                           cc 
             AM                                                            
                                                                   Subject 
                                       Re: Context injection using         
             Please respond to         @Resource in the resource class     
             wink-user@incubat                                             
               or.apache.org                                               
                                                                           
                                                                           
                                                                           
                                                                           




This section is non-normative, therefore it is not supported...

Out of curiosity, are you using JEE6 container?

On Tue, Nov 17, 2009 at 3:13 PM, Srivatsan Sridharan <
sathishsrivatsan@gmail.com> wrote:
>
> I was referring to the section 6.2 of the spec
> https://jsr311.dev.java.net/nonav/releases/1.0/spec/index.html to inject
> context as a resource in the resource class.
>
> Thanks. What you have mentioned works.
>
>
> Michael Elman-2 wrote:
>>
>> I'm not familiar with @Resource annotation. Did you mean @Context?
>>
>> The attribute from the MessageContext can be received using the @Context
>> annotation.
>> For example
>>
>>        context.setAttribute(RuntimeContext.class, context)
>>
>> in the handler will set the message context itself, therefore it can be
>> retrieved in the resource using
>>
>> @Context RuntimeContext runtimeContext
>>
>> Note that context class must be interface, since it might be injected
into
>> singletons.
>>
>> On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
>> sathishsrivatsan@gmail.com> wrote:
>>
>>>
>>> Hi,
>>>
>>> We are working on restful service development that has a request
handler
>>> and
>>> a resource class. Request handler sets an attribute on the
>>> MessageContext.
>>> To my knowledge, the set attribute could be accessed in the resource
>>> class
>>> if the entire context could be injected using @Resource annotation.
>>>
>>> 1) Is this the right way of doing it as I dont see a support for
resource
>>> injection in the resource classes?
>>> 2) If there is an alternate way to get a handle to the context in the
>>> resource class, please let me know.
>>>
>>> Thanks,
>>> Srivatsan
>>> --
>>> View this message in context:
>>>
http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html

>>> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context:
http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018455.html

> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>

Re: Context injection using @Resource in the resource class

Posted by Michael Elman <el...@apache.org>.
This section is non-normative, therefore it is not supported...

Out of curiosity, are you using JEE6 container?

On Tue, Nov 17, 2009 at 3:13 PM, Srivatsan Sridharan <
sathishsrivatsan@gmail.com> wrote:
>
> I was referring to the section 6.2 of the spec
> https://jsr311.dev.java.net/nonav/releases/1.0/spec/index.html to inject
> context as a resource in the resource class.
>
> Thanks. What you have mentioned works.
>
>
> Michael Elman-2 wrote:
>>
>> I'm not familiar with @Resource annotation. Did you mean @Context?
>>
>> The attribute from the MessageContext can be received using the @Context
>> annotation.
>> For example
>>
>>        context.setAttribute(RuntimeContext.class, context)
>>
>> in the handler will set the message context itself, therefore it can be
>> retrieved in the resource using
>>
>> @Context RuntimeContext runtimeContext
>>
>> Note that context class must be interface, since it might be injected
into
>> singletons.
>>
>> On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
>> sathishsrivatsan@gmail.com> wrote:
>>
>>>
>>> Hi,
>>>
>>> We are working on restful service development that has a request handler
>>> and
>>> a resource class. Request handler sets an attribute on the
>>> MessageContext.
>>> To my knowledge, the set attribute could be accessed in the resource
>>> class
>>> if the entire context could be injected using @Resource annotation.
>>>
>>> 1) Is this the right way of doing it as I dont see a support for
resource
>>> injection in the resource classes?
>>> 2) If there is an alternate way to get a handle to the context in the
>>> resource class, please let me know.
>>>
>>> Thanks,
>>> Srivatsan
>>> --
>>> View this message in context:
>>>
http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
>>> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context:
http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018455.html
> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>

Re: Context injection using @Resource in the resource class

Posted by Srivatsan Sridharan <sa...@gmail.com>.
I was referring to the section 6.2 of the spec
https://jsr311.dev.java.net/nonav/releases/1.0/spec/index.html to inject
context as a resource in the resource class.

Thanks. What you have mentioned works.


Michael Elman-2 wrote:
> 
> I'm not familiar with @Resource annotation. Did you mean @Context?
> 
> The attribute from the MessageContext can be received using the @Context
> annotation.
> For example
> 
>        context.setAttribute(RuntimeContext.class, context)
> 
> in the handler will set the message context itself, therefore it can be
> retrieved in the resource using
> 
> @Context RuntimeContext runtimeContext
> 
> Note that context class must be interface, since it might be injected into
> singletons.
> 
> On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
> sathishsrivatsan@gmail.com> wrote:
> 
>>
>> Hi,
>>
>> We are working on restful service development that has a request handler
>> and
>> a resource class. Request handler sets an attribute on the
>> MessageContext.
>> To my knowledge, the set attribute could be accessed in the resource
>> class
>> if the entire context could be injected using @Resource annotation.
>>
>> 1) Is this the right way of doing it as I dont see a support for resource
>> injection in the resource classes?
>> 2) If there is an alternate way to get a handle to the context in the
>> resource class, please let me know.
>>
>> Thanks,
>> Srivatsan
>> --
>> View this message in context:
>> http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
>> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018455.html
Sent from the Apache Wink Users mailing list archive at Nabble.com.

Re: Context injection using @Resource in the resource class

Posted by Bryant Luk <br...@gmail.com>.
Hi,

Is it possible for you to throw an exception with the attribute?  For
instance, catch the exception in your resource method and set the
attribute you want as a property of a new exception subclass.  Then in
the ExceptionMapper, you can access the attribute.

On Thu, Jan 7, 2010 at 2:04 AM, Srivatsan Sridharan
<sa...@gmail.com> wrote:
>
> There is a request handler that sets an attribute say
> context.setAttribute(Map.class, obj) in the message context. For a
> particular request, the resource method throws a runtime exception for which
> there is an exception mapping provider.
>
> Is there any restriction in accessing the attribute set in the
> MessageContext in the exception mapper using @Context Map obj because the
> inspection of the obj shows it as a $Proxy and not the actual class that was
> set. Further, any operation on the obj results in a NullPointerException.
>
> Bottom line is the user defined attribute set in the context should be
> accessible in the exception mapper. Any thoughts on how this should be done?
>
>
> Michael Elman-2 wrote:
>>
>> I'm not familiar with @Resource annotation. Did you mean @Context?
>>
>> The attribute from the MessageContext can be received using the @Context
>> annotation.
>> For example
>>
>>        context.setAttribute(RuntimeContext.class, context)
>>
>> in the handler will set the message context itself, therefore it can be
>> retrieved in the resource using
>>
>> @Context RuntimeContext runtimeContext
>>
>> Note that context class must be interface, since it might be injected into
>> singletons.
>>
>> On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
>> sathishsrivatsan@gmail.com> wrote:
>>
>>>
>>> Hi,
>>>
>>> We are working on restful service development that has a request handler
>>> and
>>> a resource class. Request handler sets an attribute on the
>>> MessageContext.
>>> To my knowledge, the set attribute could be accessed in the resource
>>> class
>>> if the entire context could be injected using @Resource annotation.
>>>
>>> 1) Is this the right way of doing it as I dont see a support for resource
>>> injection in the resource classes?
>>> 2) If there is an alternate way to get a handle to the context in the
>>> resource class, please let me know.
>>>
>>> Thanks,
>>> Srivatsan
>>> --
>>> View this message in context:
>>> http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
>>> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4265333.html
> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>

Re: Context injection using @Resource in the resource class

Posted by Srivatsan Sridharan <sa...@gmail.com>.
There is a request handler that sets an attribute say
context.setAttribute(Map.class, obj) in the message context. For a
particular request, the resource method throws a runtime exception for which
there is an exception mapping provider. 

Is there any restriction in accessing the attribute set in the
MessageContext in the exception mapper using @Context Map obj because the
inspection of the obj shows it as a $Proxy and not the actual class that was
set. Further, any operation on the obj results in a NullPointerException.

Bottom line is the user defined attribute set in the context should be
accessible in the exception mapper. Any thoughts on how this should be done?


Michael Elman-2 wrote:
> 
> I'm not familiar with @Resource annotation. Did you mean @Context?
> 
> The attribute from the MessageContext can be received using the @Context
> annotation.
> For example
> 
>        context.setAttribute(RuntimeContext.class, context)
> 
> in the handler will set the message context itself, therefore it can be
> retrieved in the resource using
> 
> @Context RuntimeContext runtimeContext
> 
> Note that context class must be interface, since it might be injected into
> singletons.
> 
> On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
> sathishsrivatsan@gmail.com> wrote:
> 
>>
>> Hi,
>>
>> We are working on restful service development that has a request handler
>> and
>> a resource class. Request handler sets an attribute on the
>> MessageContext.
>> To my knowledge, the set attribute could be accessed in the resource
>> class
>> if the entire context could be injected using @Resource annotation.
>>
>> 1) Is this the right way of doing it as I dont see a support for resource
>> injection in the resource classes?
>> 2) If there is an alternate way to get a handle to the context in the
>> resource class, please let me know.
>>
>> Thanks,
>> Srivatsan
>> --
>> View this message in context:
>> http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
>> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4265333.html
Sent from the Apache Wink Users mailing list archive at Nabble.com.

Re: Context injection using @Resource in the resource class

Posted by Michael Elman <el...@apache.org>.
I'm not familiar with @Resource annotation. Did you mean @Context?

The attribute from the MessageContext can be received using the @Context
annotation.
For example

       context.setAttribute(RuntimeContext.class, context)

in the handler will set the message context itself, therefore it can be
retrieved in the resource using

@Context RuntimeContext runtimeContext

Note that context class must be interface, since it might be injected into
singletons.

On Tue, Nov 17, 2009 at 1:38 PM, Srivatsan Sridharan <
sathishsrivatsan@gmail.com> wrote:

>
> Hi,
>
> We are working on restful service development that has a request handler
> and
> a resource class. Request handler sets an attribute on the MessageContext.
> To my knowledge, the set attribute could be accessed in the resource class
> if the entire context could be injected using @Resource annotation.
>
> 1) Is this the right way of doing it as I dont see a support for resource
> injection in the resource classes?
> 2) If there is an alternate way to get a handle to the context in the
> resource class, please let me know.
>
> Thanks,
> Srivatsan
> --
> View this message in context:
> http://n2.nabble.com/Context-injection-using-Resource-in-the-resource-class-tp4018056p4018056.html
> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>