Quantcast

Accessing HttpSession from a ContainerRequestFilter

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Accessing HttpSession from a ContainerRequestFilter

Erdinc Yilmazel
How can I access the HttpSession from a ContainerRequestFilter implementation? Or at least properties within the session?

Erdinc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Jersey] Accessing HttpSession from a ContainerRequestFilter

Paul Sandoz
Administrator

On Jan 25, 2010, at 2:09 AM, Erdinc Yilmazel wrote:

> How can I access the HttpSession from a ContainerRequestFilter  
> implementation? Or at least properties within the session?
>

Inject HttpServletRequest:

   @Context HttpServletRequest hsr;

and get the session from that.

Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Jersey] Accessing HttpSession from a ContainerRequestFilter

Erdinc Yilmazel
Paul I think I got something wrong,

I have a ResourceFilterFactory which returns a list of ResourceFilter instances on its create method. In this method I am returning only one instance of ResourceFilter.

The following method is called by Jersey, only once during the application startup.

   public List<ResourceFilter> create(AbstractMethod am) {
      LoginRequired lr = am.getAnnotation(LoginRequired.class);
      if (lr == null) {
         lr = am.getResource().getAnnotation(LoginRequired.class);
         if (lr == null) {
            return null;
         }
      }

      final LoginResourceFilter filter = new LoginResourceFilter();

      return new ArrayList<ResourceFilter>() {{
         add(filter);
      }};
   }

The actual resource filter is something like this:

public class LoginResourceFilter implements ResourceFilter, ContainerRequestFilter {
   public ContainerRequestFilter getRequestFilter() {
      return this;
   }

   public ContainerResponseFilter getResponseFilter() {
      // Not Supported
      return null;
   }
   
   public ContainerRequest filter(ContainerRequest req) {
        return req;
   }
}

Where in this class can I inject HttpServletRequest? Since this is a single instance, injecting it as a class property will not work. I need it injected in the filter method, or I need to find a way to use a new instance of my ResourceFilter in every request.


Erdinc

On Mon, Jan 25, 2010 at 11:00 AM, Paul Sandoz <[hidden email]> wrote:

On Jan 25, 2010, at 2:09 AM, Erdinc Yilmazel wrote:

How can I access the HttpSession from a ContainerRequestFilter implementation? Or at least properties within the session?


Inject HttpServletRequest:

 @Context HttpServletRequest hsr;

and get the session from that.

Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Jersey] Accessing HttpSession from a ContainerRequestFilter

Paul Sandoz
Administrator

On Jan 25, 2010, at 12:18 PM, Erdinc Yilmazel wrote:

> Paul I think I got something wrong,
>
> I have a ResourceFilterFactory which returns a list of  
> ResourceFilter instances on its create method. In this method I am  
> returning only one instance of ResourceFilter.
>
> The following method is called by Jersey, only once during the  
> application startup.
>
>    public List<ResourceFilter> create(AbstractMethod am) {
>       LoginRequired lr = am.getAnnotation(LoginRequired.class);
>       if (lr == null) {
>          lr = am.getResource().getAnnotation(LoginRequired.class);
>          if (lr == null) {
>             return null;
>          }
>       }
>
>       final LoginResourceFilter filter = new LoginResourceFilter();
>
>       return new ArrayList<ResourceFilter>() {{
>          add(filter);
>       }};
>    }
>
> The actual resource filter is something like this:
>
> public class LoginResourceFilter implements ResourceFilter,  
> ContainerRequestFilter {
>    public ContainerRequestFilter getRequestFilter() {
>       return this;
>    }
>
>    public ContainerResponseFilter getResponseFilter() {
>       // Not Supported
>       return null;
>    }
>
>    public ContainerRequest filter(ContainerRequest req) {
>         return req;
>    }
> }
>
> Where in this class can I inject HttpServletRequest? Since this is a  
> single instance, injecting it as a class property will not work. I  
> need it injected in the filter method, or I need to find a way to  
> use a new instance of my ResourceFilter in every request.

You need to inject on the ResourceFilterFactory instance. A thread  
local proxy will be injected. If you use inner classes for the  
ResourceFilter you access the field reference on ResourceFilterFactory.

Since you are responsible for instantiating LoginResourceFilter no  
injection will be performed.

Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...