Matthew Broadhead
2017-12-07 08:49:58 UTC
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?
@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?