You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Kirys <ki...@neoteroi.org> on 2016/05/08 11:36:27 UTC

Is this legit? @RequestScoped vs HttpServletRequest

I'm am making some experiments
and I've written this cdi called from within a jsf page
but I get a nullpointer exception is that a valid setup or is not legit 
to get HttpServletRequest from within a request scoped cdi?

Thank You

Details follow:

Apache Tomcat (TomEE)/7.0.68 (1.7.4) (plumee)

CDI code extract

@Named
@RequestScoped
public class UserProfileManager implements Serializable {

     @Context
     private ServletContext context;

     @Context
     private HttpServletRequest request;
     @Context
     private HttpServletResponse response;

     public LoggedUser getUser() {
         Enumeration<String> names= request.getAttributeNames(); //null 
pointer exception here -> request is not null
         /* other not relevant code */

     }


}

Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Exactly. Recent tomee would log something like "no jaxrs call to value
@Context".
Le 8 mai 2016 18:10, "John D. Ament" <jo...@apache.org> a écrit :

> I know Romain's done some work in this area before.  Per the spec, its not
> required to work the way you have it defined.  JAX-RS injection via
> @Context is only mandated to work in JAX-RS managed components.
>
> Try to use @Inject instead of @Context, that may be what was implemented in
> TomEE.  That is mandated in EE7 (but not in EE6).
>
> John
>
> On Sun, May 8, 2016 at 7:36 AM Kirys <ki...@neoteroi.org> wrote:
>
> > I'm am making some experiments
> > and I've written this cdi called from within a jsf page
> > but I get a nullpointer exception is that a valid setup or is not legit
> > to get HttpServletRequest from within a request scoped cdi?
> >
> > Thank You
> >
> > Details follow:
> >
> > Apache Tomcat (TomEE)/7.0.68 (1.7.4) (plumee)
> >
> > CDI code extract
> >
> > @Named
> > @RequestScoped
> > public class UserProfileManager implements Serializable {
> >
> >      @Context
> >      private ServletContext context;
> >
> >      @Context
> >      private HttpServletRequest request;
> >      @Context
> >      private HttpServletResponse response;
> >
> >      public LoggedUser getUser() {
> >          Enumeration<String> names= request.getAttributeNames(); //null
> > pointer exception here -> request is not null
> >          /* other not relevant code */
> >
> >      }
> >
> >
> > }
> >
>

Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by Romain Manni-Bucau <rm...@gmail.com>.
if you do all your processing/work in the request thread yes.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2016-05-10 12:07 GMT+02:00 Kirys <ki...@neoteroi.org>:

> On 08/05/2016 20:04, Romain Manni-Bucau wrote:
>
>> If you use tomee 1 that's normal since it is a JavaEE 7 feature. TomEE 7
>> supports it.
>>
>> A filter is safe if you support asynchroron't use it. Asynchronism or
>> banalized threads is the main issue you can encounter with a ThreadLocal.
>>
>>
> I'm not sure if I've understood it.
> BTW I'm experimenting with a lib (pac4j) and i want to cache certain info
> into a sessionscoped cdi, so i've built a filter that intercept the cases
> that produces a change of state and I update the session info accordingly.
> For now it is only an experiment for studying the lib and use cases.
> Is it a reasonable way?
> Bye
> K.
>
>

Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by Kirys <ki...@neoteroi.org>.
On 08/05/2016 20:04, Romain Manni-Bucau wrote:
> If you use tomee 1 that's normal since it is a JavaEE 7 feature. TomEE 7
> supports it.
>
> A filter is safe if you support asynchroron't use it. Asynchronism or
> banalized threads is the main issue you can encounter with a ThreadLocal.
>

I'm not sure if I've understood it.
BTW I'm experimenting with a lib (pac4j) and i want to cache certain 
info into a sessionscoped cdi, so i've built a filter that intercept the 
cases that produces a change of state and I update the session info 
accordingly.
For now it is only an experiment for studying the lib and use cases.
Is it a reasonable way?
Bye
K.


Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by Romain Manni-Bucau <rm...@gmail.com>.
If you use tomee 1 that's normal since it is a JavaEE 7 feature. TomEE 7
supports it.

A filter is safe if you support asynchroron't use it. Asynchronism or
banalized threads is the main issue you can encounter with a ThreadLocal.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2016-05-08 20:00 GMT+02:00 Kirys <ki...@neoteroi.org>:

> On 08/05/2016 18:09, John D. Ament wrote:
>
>> I know Romain's done some work in this area before.  Per the spec, its not
>> required to work the way you have it defined.  JAX-RS injection via
>> @Context is only mandated to work in JAX-RS managed components.
>>
>> Try to use @Inject instead of @Context, that may be what was implemented
>> in
>> TomEE.  That is mandated in EE7 (but not in EE6).
>>
>> John
>>
> @inject gave me an error that it could to find a class to inject, I don't
> remember.
> For now I managed to populate the data I need using filters, not really
> elegant but works, is it safe to put it in a filter?
> Thank you
> F
>

Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by Kirys <ki...@neoteroi.org>.
On 08/05/2016 18:09, John D. Ament wrote:
> I know Romain's done some work in this area before.  Per the spec, its not
> required to work the way you have it defined.  JAX-RS injection via
> @Context is only mandated to work in JAX-RS managed components.
>
> Try to use @Inject instead of @Context, that may be what was implemented in
> TomEE.  That is mandated in EE7 (but not in EE6).
>
> John
@inject gave me an error that it could to find a class to inject, I 
don't remember.
For now I managed to populate the data I need using filters, not really 
elegant but works, is it safe to put it in a filter?
Thank you
F

Re: Is this legit? @RequestScoped vs HttpServletRequest

Posted by "John D. Ament" <jo...@apache.org>.
I know Romain's done some work in this area before.  Per the spec, its not
required to work the way you have it defined.  JAX-RS injection via
@Context is only mandated to work in JAX-RS managed components.

Try to use @Inject instead of @Context, that may be what was implemented in
TomEE.  That is mandated in EE7 (but not in EE6).

John

On Sun, May 8, 2016 at 7:36 AM Kirys <ki...@neoteroi.org> wrote:

> I'm am making some experiments
> and I've written this cdi called from within a jsf page
> but I get a nullpointer exception is that a valid setup or is not legit
> to get HttpServletRequest from within a request scoped cdi?
>
> Thank You
>
> Details follow:
>
> Apache Tomcat (TomEE)/7.0.68 (1.7.4) (plumee)
>
> CDI code extract
>
> @Named
> @RequestScoped
> public class UserProfileManager implements Serializable {
>
>      @Context
>      private ServletContext context;
>
>      @Context
>      private HttpServletRequest request;
>      @Context
>      private HttpServletResponse response;
>
>      public LoggedUser getUser() {
>          Enumeration<String> names= request.getAttributeNames(); //null
> pointer exception here -> request is not null
>          /* other not relevant code */
>
>      }
>
>
> }
>