@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse resp)
throws IOException {
OAuth2Accessor accessor = null;
try {
final OAuth2Message msg = this.oauth2MessageProvider.get();
msg.parseRequest(request);
final OAuth2Error error = msg.getError();
final String requestStateKey = msg.getState();
if (requestStateKey == null) {
if (error != null) {
OAuth2CallbackServlet.sendError(error, msg.getErrorDescription(), msg.getErrorUri(),
null, resp, null);
} else {
OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
"OAuth2CallbackServlet requestStateKey is null.", "", null, resp, null);
}
return;
}
final Integer index = Integer.decode(requestStateKey);
accessor = this.store.getOAuth2Accessor(index);
if (error != null) {
OAuth2CallbackServlet.sendError(error, msg.getErrorDescription(), msg.getErrorUri(),
accessor, resp, null);
return;
}
if ((accessor == null) || (!accessor.isValid()) || (accessor.isErrorResponse())) {
if (accessor != null) {
OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
"OAuth2CallbackServlet accessor is invalid " + accessor, "", accessor, resp,
accessor.getErrorException());
} else {
OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
"OAuth2CallbackServlet accessor is null", "", null, resp, null);
}
return;
}
if (!accessor.isRedirecting()) {
// Somehow our accessor got lost. We should not proceed.
OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
"OAuth2CallbackServlet accessor is not valid, isn't redirecting.", "", accessor, resp,
null);
return;
}
boolean foundHandler = false;
for (final AuthorizationEndpointResponseHandler authorizationEndpointResponseHandler : this.authorizationEndpointResponseHandlers) {
if (authorizationEndpointResponseHandler.handlesRequest(accessor, request)) {
final OAuth2HandlerError handlerError = authorizationEndpointResponseHandler
.handleRequest(accessor, request);
if (handlerError != null) {
OAuth2CallbackServlet.sendError(handlerError.getError(),
handlerError.getContextMessage(), null, accessor, resp, handlerError.getCause());
return;
}
foundHandler = true;
break;
}
}
if (!foundHandler) {
OAuth2CallbackServlet.sendError(OAuth2Error.NO_RESPONSE_HANDLER,
"OAuth2Callback servlet couldn't find a AuthorizationEndpointResponseHandler", "",
accessor, resp, null);
return;
}
HttpUtil.setCachingHeaders(resp, OAuth2CallbackServlet.ONE_HOUR_IN_SECONDS, true);
resp.setContentType("text/html; charset=UTF-8");
resp.getWriter().write(OAuth2CallbackServlet.RESP_BODY);
} catch (final Exception e) {
OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
"Exception occurred processing redirect.", "", accessor, resp, e);
if (IOException.class.isInstance(e)) {
throw (IOException) e;
}
} finally {
if (accessor != null) {
accessor.setRedirecting(false);
}
}
}