// Mode where an HTTP session exists
//
// =========================================================================================
public void execHttpSession() throws ServletException, IOException {
Context context = Context.get();
// Find the OAuth dance object being used
OAuth1Handler oAuthHandler = (OAuth1Handler)context.getSessionMap().get(Configuration.OAUTH1_HANDLER);
if (oAuthHandler == null) {
throw new ServletException(
"Internal Error: Cannot find the OAuth object back from the request");
}
// Read the oauth parameters
try {
String oauth_token = (String) context.getRequestParameterMap().get(OAConstants.OAUTH_TOKEN);
String oauth_verifier = (String) context.getRequestParameterMap().get(OAConstants.OAUTH_VERIFIER);
oAuthHandler.setAccessToken(oauth_token);
oAuthHandler.setVerifierCode(oauth_verifier);
AccessToken tk = oAuthHandler.readToken(oauth_token, oauth_verifier);
if (tk == null) {
// should not happen
throw new ServletException("Missing OAuth token");
}
// Store the new key
oAuthHandler.setAccessTokenObject(tk);
if (!context.isCurrentUserAnonymous()) {
CredentialStore cs = CredentialStoreFactory.getCredentialStore(oAuthHandler.getCredentialStore());
if (cs != null) {
// But we store it uniquely if the current user is not anonymous
cs.store(oAuthHandler.getServiceName(), OAuth1Handler.ACCESS_TOKEN_STORE_TYPE, context.getCurrentUserId(), tk);
}
} else {
AnonymousCredentialStore.storeCredentials(context, tk,
oAuthHandler.getAppId(), oAuthHandler.getServiceName());
}
// redirect to the initial page
String applicationPage = oAuthHandler.getApplicationPage();
if (StringUtil.isNotEmpty(applicationPage)) {
context.sendRedirect(applicationPage);
}
} catch (Exception e) {
throw new ServletException(e);
}
}