Source:
public void exchangeCodeForToken() throws IOException { HttpServletRequest req = (HttpServletRequest) facesCtx.getExternalContext().getRequest(); try { Optional<AbstractOAuth2AuthenticationProvider> oIdp = parseStateFromRequest(req); Optional<String> code = parseCodeFromRequest(req); if (oIdp.isPresent() && code.isPresent()) { AbstractOAuth2AuthenticationProvider idp = oIdp.get(); OAuth20Service svc = idp.getService(systemConfig.getOAuth2CallbackUrl()); oauthUser = idp.getUserRecord(code.get(), svc); UserRecordIdentifier idtf = oauthUser.getUserRecordIdentifier(); AuthenticatedUser dvUser = authenticationSvc.lookupUser(idtf); if (dvUser == null) { newAccountPage.setNewUser(oauthUser); FacesContext.getCurrentInstance().getExternalContext().redirect("/oauth2/firstLogin.xhtml"); } else { session.setUser(dvUser); session.configureSessionTimeout(); final OAuth2TokenData tokenData = oauthUser.getTokenData(); tokenData.setUser(dvUser); tokenData.setOauthProviderId(idp.getId()); oauth2Tokens.store(tokenData); String destination = redirectPage.orElse("/"); HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); String prettyUrl = response.encodeRedirectURL(destination); FacesContext.getCurrentInstance().getExternalContext().redirect(prettyUrl); } } } catch (OAuth2Exception ex) { error = ex; logger.log(Level.INFO, "OAuth2Exception caught. HTTP return code: {0}. Message: {1}. Message body: {2}", new Object[]{error.getHttpReturnCode(), error.getLocalizedMessage(), error.getMessageBody()}); Logger.getLogger(OAuth2LoginBackingBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException | ExecutionException ex) { error = new OAuth2Exception(-1, "Please see server logs for more details", "Could not login due to threading exceptions."); logger.log(Level.WARNING, "Threading exception caught. Message: {0}", ex.getLocalizedMessage()); } }

Target:
public void exchangeCodeForToken() throws IOException { HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); try { Optional<AbstractOAuth2AuthenticationProvider> oIdp = parseStateFromRequest(req); Optional<String> code = parseCodeFromRequest(req); if (oIdp.isPresent() && code.isPresent()) { AbstractOAuth2AuthenticationProvider idp = oIdp.get(); OAuth20Service svc = idp.getService(systemConfig.getOAuth2CallbackUrl()); oauthUser = idp.getUserRecord(code.get(), svc); UserRecordIdentifier idtf = oauthUser.getUserRecordIdentifier(); AuthenticatedUser dvUser = authenticationSvc.lookupUser(idtf); if (dvUser == null) { newAccountPage.setNewUser(oauthUser); FacesContext.getCurrentInstance().getExternalContext().redirect("/oauth2/firstLogin.xhtml"); } else { session.setUser(dvUser); session.configureSessionTimeout(); final OAuth2TokenData tokenData = oauthUser.getTokenData(); tokenData.setUser(dvUser); tokenData.setOauthProviderId(idp.getId()); oauth2Tokens.store(tokenData); String destination = redirectPage.orElse("/"); HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); String prettyUrl = response.encodeRedirectURL(destination); FacesContext.getCurrentInstance().getExternalContext().redirect(prettyUrl); } } } catch (OAuth2Exception ex) { error = ex; logger.log(Level.INFO, "OAuth2Exception caught. HTTP return code: {0}. Message: {1}. Message body: {2}", new Object[]{error.getHttpReturnCode(), error.getLocalizedMessage(), error.getMessageBody()}); Logger.getLogger(OAuth2LoginBackingBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException | ExecutionException ex) { error = new OAuth2Exception(-1, "Please see server logs for more details", "Could not login due to threading exceptions."); logger.log(Level.WARNING, "Threading exception caught. Message: {0}", ex.getLocalizedMessage()); } }