users@jersey.java.net

Re: [Jersey] setting session attributes at resource startup

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 30 Jun 2008 16:25:16 +0200

Hi,

Ah, i did not realize you resource class was a singleton. The more
information/code you per-email send the faster i can help you :-)

This is working fine for me in 0.8, see example below. Does that example
work for you?

Paul.

public class Inject {
     @Path("singleton")
     @Singleton
     public static class SingletonResource {
         @Context HttpServletRequest r;

         @ProduceMime("text/plain")
         @GET public String get() {
             return r.getPathInfo();
         }
     }

     @Path("perrequest")
     public static class PerRequestResource {
         @Context HttpServletRequest r;

         @ProduceMime("text/plain")
         @GET public String get() {
             return r.getPathInfo();
         }
     }

     @Path("perrequest/constructor")
     public static class PerRequestConstructorResource {
         String path;

         public PerRequestConstructorResource(@Context
HttpServletRequest r) {
             path = r.getPathInfo();
         }

         @ProduceMime("text/plain")
         @GET public String get() {
             return path;
         }
     }
}

Arul Dhesiaseelan wrote:
> Paul,
>
> I get this exception now.
>
> Caused by: java.lang.InstantiationException: resources.MyResource
> at java.lang.Class.newInstance0(Class.java:335)
> at java.lang.Class.newInstance(Class.java:303)
> at
> com.sun.ws.rest.impl.application.WebApplicationImpl$DefaultComponentProvider.getInstance(WebApplicationImpl.java:293)
>
> at
> com.sun.ws.rest.impl.resource.SingletonProvider.init(SingletonProvider.java:44)
>
> ... 50 more
> 2008-06-30 07:57:32.584::WARN: Nested in
> javax.servlet.ServletException:
> com.sun.ws.rest.api.container.ContainerException: Unable to create
> resource:
> com.sun.ws.rest.api.container.ContainerException: Unable to create resource
> at
> com.sun.ws.rest.impl.resource.SingletonProvider.init(SingletonProvider.java:46)
>
>
>
> When I remove @Singleton annotation from my resource, this exception
> goes away. But, now I still get the NPE.
>
> Thank you,
> Arul
>
> Paul Sandoz wrote:
>> On Jun 27, 2008, at 11:06 PM, Arul Dhesiaseelan wrote:
>>
>>> Hi,
>>>
>>> I am trying to set up a session attributes in the constructor of the
>>> Resource. But, it looks like the session object is not injected yet
>>> when I try to set attribute on the session.
>>>
>>> @Context
>>> private HttpServletRequest httpRequest;
>>>
>>> public MyResource() {
>>> httpRequest.getSession().setAttribute("securityenabled",
>>> "true");//fails due to NPE
>>> }
>>>
>>>
>>> Is there any other way to setup session attributes in the resource at
>>> startup?
>>>
>>
>> Injection on fields can only after construction. Try doing the
>> following instead:
>>
>> private HttpServletRequest httpRequest;
>>
>> public MyResource(@Context HttpServletRequest httpRequest) {
>> this.httpRequest = httpRequest;
>> httpRequest.getSession().setAttribute("securityenabled",
>> "true");//fails due to NPE
>> }
>>
>> Paul.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>> ________________________________
>> Scanned by MessageLabs for Flux
>> ________________________________
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109