You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Matthew Broadhead <ma...@nbmlaw.co.uk> on 2017/12/07 08:49:58 UTC

injecting httpservletrequest in timer

i have this method to @Produces KeycloakSecurityContext
@Produces
     @Default
     @Dependent
     public KeycloakSecurityContext keycloakSecurityContext() {
         try {
             if (httpServletRequest instanceof KeycloakPrincipal) {
                 KeycloakPrincipal<KeycloakSecurityContext> 
keycloakPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>) 
httpServletRequest
                         .getUserPrincipal();
                 return keycloakPrincipal.getKeycloakSecurityContext();
             }
         } catch (NullPointerException e) {
             // e.printStackTrace();
         }
         return null;
     }
i added catch the NullPointerException because httpServletRequest causes 
an NPE if the KeycloakSecurityContext is injected into an 
@ApplicationScoped where it has been run from a timer and there is no 
servlet request.  there is no way to null check the httpServletRequest 
as it blows up behind the scenes
is this the right way to do it or is there another way to detect there 
is no request?

Re: injecting httpservletrequest in timer

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk>.
yes it worked

On 07/12/2017 11:48, Romain Manni-Bucau wrote:
> scope a wrapper?
>
> Romain Manni-Bucau
> @rmannibucau |  Blog | Old Blog | Github | LinkedIn
>
>
> 2017-12-07 11:21 GMT+01:00 Matthew Broadhead <ma...@nbmlaw.co.uk>:
>> I couldn't scope it because otherwise couldn't return null
>>
>>
>> On 07/12/2017 09:55, Romain Manni-Bucau wrote:
>>> if you scope this producer you would have a lazy instance and in the
>>> context of the timer you wouldnt call it so you wouldnt have this NPE
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau |  Blog | Old Blog | Github | LinkedIn
>>>
>>>
>>> 2017-12-07 9:49 GMT+01:00 Matthew Broadhead
>>> <ma...@nbmlaw.co.uk>:
>>>> i have this method to @Produces KeycloakSecurityContext
>>>> @Produces
>>>>       @Default
>>>>       @Dependent
>>>>       public KeycloakSecurityContext keycloakSecurityContext() {
>>>>           try {
>>>>               if (httpServletRequest instanceof KeycloakPrincipal) {
>>>>                   KeycloakPrincipal<KeycloakSecurityContext>
>>>> keycloakPrincipal
>>>> = (KeycloakPrincipal<KeycloakSecurityContext>) httpServletRequest
>>>>                           .getUserPrincipal();
>>>>                   return keycloakPrincipal.getKeycloakSecurityContext();
>>>>               }
>>>>           } catch (NullPointerException e) {
>>>>               // e.printStackTrace();
>>>>           }
>>>>           return null;
>>>>       }
>>>> i added catch the NullPointerException because httpServletRequest causes
>>>> an
>>>> NPE if the KeycloakSecurityContext is injected into an @ApplicationScoped
>>>> where it has been run from a timer and there is no servlet request.
>>>> there
>>>> is no way to null check the httpServletRequest as it blows up behind the
>>>> scenes
>>>> is this the right way to do it or is there another way to detect there is
>>>> no
>>>> request?
>>


Re: injecting httpservletrequest in timer

Posted by Romain Manni-Bucau <rm...@gmail.com>.
scope a wrapper?

Romain Manni-Bucau
@rmannibucau |  Blog | Old Blog | Github | LinkedIn


2017-12-07 11:21 GMT+01:00 Matthew Broadhead <ma...@nbmlaw.co.uk>:
> I couldn't scope it because otherwise couldn't return null
>
>
> On 07/12/2017 09:55, Romain Manni-Bucau wrote:
>>
>> if you scope this producer you would have a lazy instance and in the
>> context of the timer you wouldnt call it so you wouldnt have this NPE
>>
>> Romain Manni-Bucau
>> @rmannibucau |  Blog | Old Blog | Github | LinkedIn
>>
>>
>> 2017-12-07 9:49 GMT+01:00 Matthew Broadhead
>> <ma...@nbmlaw.co.uk>:
>>>
>>> i have this method to @Produces KeycloakSecurityContext
>>> @Produces
>>>      @Default
>>>      @Dependent
>>>      public KeycloakSecurityContext keycloakSecurityContext() {
>>>          try {
>>>              if (httpServletRequest instanceof KeycloakPrincipal) {
>>>                  KeycloakPrincipal<KeycloakSecurityContext>
>>> keycloakPrincipal
>>> = (KeycloakPrincipal<KeycloakSecurityContext>) httpServletRequest
>>>                          .getUserPrincipal();
>>>                  return keycloakPrincipal.getKeycloakSecurityContext();
>>>              }
>>>          } catch (NullPointerException e) {
>>>              // e.printStackTrace();
>>>          }
>>>          return null;
>>>      }
>>> i added catch the NullPointerException because httpServletRequest causes
>>> an
>>> NPE if the KeycloakSecurityContext is injected into an @ApplicationScoped
>>> where it has been run from a timer and there is no servlet request.
>>> there
>>> is no way to null check the httpServletRequest as it blows up behind the
>>> scenes
>>> is this the right way to do it or is there another way to detect there is
>>> no
>>> request?
>
>

Re: injecting httpservletrequest in timer

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk>.
I couldn't scope it because otherwise couldn't return null

On 07/12/2017 09:55, Romain Manni-Bucau wrote:
> if you scope this producer you would have a lazy instance and in the
> context of the timer you wouldnt call it so you wouldnt have this NPE
>
> Romain Manni-Bucau
> @rmannibucau |  Blog | Old Blog | Github | LinkedIn
>
>
> 2017-12-07 9:49 GMT+01:00 Matthew Broadhead <ma...@nbmlaw.co.uk>:
>> i have this method to @Produces KeycloakSecurityContext
>> @Produces
>>      @Default
>>      @Dependent
>>      public KeycloakSecurityContext keycloakSecurityContext() {
>>          try {
>>              if (httpServletRequest instanceof KeycloakPrincipal) {
>>                  KeycloakPrincipal<KeycloakSecurityContext> keycloakPrincipal
>> = (KeycloakPrincipal<KeycloakSecurityContext>) httpServletRequest
>>                          .getUserPrincipal();
>>                  return keycloakPrincipal.getKeycloakSecurityContext();
>>              }
>>          } catch (NullPointerException e) {
>>              // e.printStackTrace();
>>          }
>>          return null;
>>      }
>> i added catch the NullPointerException because httpServletRequest causes an
>> NPE if the KeycloakSecurityContext is injected into an @ApplicationScoped
>> where it has been run from a timer and there is no servlet request.  there
>> is no way to null check the httpServletRequest as it blows up behind the
>> scenes
>> is this the right way to do it or is there another way to detect there is no
>> request?


Re: injecting httpservletrequest in timer

Posted by Romain Manni-Bucau <rm...@gmail.com>.
if you scope this producer you would have a lazy instance and in the
context of the timer you wouldnt call it so you wouldnt have this NPE

Romain Manni-Bucau
@rmannibucau |  Blog | Old Blog | Github | LinkedIn


2017-12-07 9:49 GMT+01:00 Matthew Broadhead <ma...@nbmlaw.co.uk>:
> i have this method to @Produces KeycloakSecurityContext
> @Produces
>     @Default
>     @Dependent
>     public KeycloakSecurityContext keycloakSecurityContext() {
>         try {
>             if (httpServletRequest instanceof KeycloakPrincipal) {
>                 KeycloakPrincipal<KeycloakSecurityContext> keycloakPrincipal
> = (KeycloakPrincipal<KeycloakSecurityContext>) httpServletRequest
>                         .getUserPrincipal();
>                 return keycloakPrincipal.getKeycloakSecurityContext();
>             }
>         } catch (NullPointerException e) {
>             // e.printStackTrace();
>         }
>         return null;
>     }
> i added catch the NullPointerException because httpServletRequest causes an
> NPE if the KeycloakSecurityContext is injected into an @ApplicationScoped
> where it has been run from a timer and there is no servlet request.  there
> is no way to null check the httpServletRequest as it blows up behind the
> scenes
> is this the right way to do it or is there another way to detect there is no
> request?