Discussion:
injecting httpservletrequest in timer
Matthew Broadhead
2017-12-07 08:49:58 UTC
Permalink
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?
Romain Manni-Bucau
2017-12-07 08:55:00 UTC
Permalink
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
Post by Matthew Broadhead
@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
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?
Matthew Broadhead
2017-12-07 10:21:24 UTC
Permalink
I couldn't scope it because otherwise couldn't return null
Post by Romain Manni-Bucau
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
Post by Matthew Broadhead
@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
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?
Romain Manni-Bucau
2017-12-07 10:48:42 UTC
Permalink
scope a wrapper?

Romain Manni-Bucau
@rmannibucau | Blog | Old Blog | Github | LinkedIn
Post by Matthew Broadhead
I couldn't scope it because otherwise couldn't return null
Post by Romain Manni-Bucau
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
Post by Matthew Broadhead
@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
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?
Matthew Broadhead
2017-12-07 14:00:54 UTC
Permalink
yes it worked
Post by Romain Manni-Bucau
scope a wrapper?
Romain Manni-Bucau
@rmannibucau | Blog | Old Blog | Github | LinkedIn
Post by Matthew Broadhead
I couldn't scope it because otherwise couldn't return null
Post by Romain Manni-Bucau
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
Post by Matthew Broadhead
@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
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?
Loading...