private static final Logger logger = Logger.getLogger(sourceClass);
@Override
public void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
Context context = Context.get();
OAuth2Handler oAuthHandler = (OAuth2Handler)context.getSessionMap().get(Configuration.OAUTH2_HANDLER);
if (oAuthHandler == null) {
// this can happen if you access the application using a different hostname
// to the one registered as the OAuth2.0 redirect URI
StringBuffer requestUrl = request.getRequestURL();
String msg = "Unable to retrieve OAuth2.0 handler for redirect request to {0}. Please check you are accessing the application using the same hostname used in the OAuth 2.0 redirect URI.";
logger.info(MessageFormat.format(msg, requestUrl));
return;
}
String authcode = extractAuthorizationToken(request);
oAuthHandler.setAuthorization_code(authcode);
try {
oAuthHandler.getAccessTokenForAuthorizedUser(); // This retrieves and sets all authentication information in OAuth2Handler
AccessToken token = oAuthHandler.createToken(oAuthHandler.getAppId(),oAuthHandler.getServiceName());
// Store the new key
oAuthHandler.setAccessTokenObject(token);
if(!context.isCurrentUserAnonymous()) {
CredentialStore credStore = CredentialStoreFactory.getCredentialStore(oAuthHandler.getCredentialStore());
if(credStore!=null) {
credStore.store(oAuthHandler.getServiceName(), OAuth2Handler.ACCESS_TOKEN_STORE_TYPE, context.getCurrentUserId(), token);
}
} else {
// Store the token for anonymous user
AnonymousCredentialStore.storeCredentials(context, token, oAuthHandler.getAppId(), oAuthHandler.getServiceName());
}
Context.get().sendRedirect(oAuthHandler.getApplicationPage());
} catch (Exception e) {
e.printStackTrace();
}
}