if (isLogging) {
BasicOAuth2Request.LOG.entering(BasicOAuth2Request.LOG_CLASS, "attemptFetch", new Object[] {
accessor, tryAgain });
}
HttpResponseBuilder ret = null;
// If there's an error in the accessor don't continue.
if (!accessor.isErrorResponse()) {
if (BasicOAuth2Request.haveAccessToken(accessor) != null) {
// We have an access_token, use it and stop!
ret = this.fetchData(accessor);
} else {
// We don't have an access token, we need to try and get one.
// First step see if we have a refresh token
if (BasicOAuth2Request.haveRefreshToken(accessor) != null) {
if (BasicOAuth2Request.checkCanRefresh()) {
final OAuth2HandlerError handlerError = this.refreshToken(accessor);
if (handlerError == null) {
// No errors refreshing, attempt the fetch again.
ret = this.attemptFetch(accessor, true);
} else {
// There was an error refreshing, stop.
final OAuth2Error error = handlerError.getError();
ret = this.getErrorResponseBuilder(handlerError.getCause(), error,
handlerError.getContextMessage(), "");
}
} else {
// User cannot refresh, they'll have to try to authorize again.
accessor.setRefreshToken(null);
ret = this.attemptFetch(accessor, true);
}
} else {
// We have no access token and no refresh token.
// User needs to authorize again.
if (!accessor.isRedirecting() && this.checkCanAuthorize(accessor)) {
final String completeAuthUrl = this.authorize(accessor);
if (completeAuthUrl != null) {
// Send a response to redirect to the authorization url
this.responseParams.setAuthorizationUrl(completeAuthUrl);
accessor.setRedirecting(true);
} else {
// This wasn't a redirect type of authorization. try again
ret = this.attemptFetch(accessor, true);
}
}
}
}
if ((ret == null) && (!accessor.isRedirecting())) {
// We don't have a response, just issue the request and
// see what happens
ret = this.fetchData(accessor);
}
if ((ret == null) && (accessor.isRedirecting())) {
ret = new HttpResponseBuilder().setHttpStatusCode(HttpResponse.SC_OK).setStrictNoCache();
}
} else {
return null;
}