log.logp(Level.FINEST, CLASS, method, "accessToken has expired at {0}", new Object[]{expiresAt});
}
try {
this.tokenStore.removeAccessToken(accessor);
} catch (final GadgetException e) {
throw new OAuth2RequestException(OAuth2Error.MISSING_SERVER_RESPONSE,
"error removing access_token", null);
}
accessToken = null;
accessor.setAccessToken(null);
if (!lastAttempt) {
return null;
}
}
}
}
OAuth2Token refreshToken = accessor.getRefreshToken();
if (refreshToken != null) {
final long expiresAt = refreshToken.getExpiresAt();
if (expiresAt != 0) {
if (currentTime >= expiresAt) {
if (log.isLoggable(Level.FINEST)) {
log.logp(Level.FINEST, CLASS, method, "refreshToken has expired at {0}", new Object[]{expiresAt});
}
try {
this.tokenStore.removeRefreshToken(accessor);
} catch (final GadgetException e) {
throw new OAuth2RequestException(OAuth2Error.MISSING_SERVER_RESPONSE,
"error removing refresh_token", null);
}
refreshToken = null;
accessor.setRefreshToken(null);
if (!lastAttempt) {
return null;
}
}
}
}
if (accessToken != null) {
final boolean isAllowed = isUriAllowed(request.getUri(), accessor.getAllowedDomains());
if (isAllowed) {
String tokenType = accessToken.getTokenType();
if (tokenType == null || tokenType.length() == 0) {
tokenType = OAuth2Message.BEARER_TOKEN_TYPE;
}
for (final ResourceRequestHandler resourceRequestHandler : this.resourceRequestHandlers) {
if (tokenType.equalsIgnoreCase(resourceRequestHandler.getTokenType())) {
resourceRequestHandler.addOAuth2Params(accessor, request);
}
}
} else {
log.logp(Level.WARNING, CLASS, method,
"Gadget {0} attempted to send OAuth2 Token to an unauthorized domain: {1}.",
new Object[] { accessor.getGadgetUri(), request.getUri() });
throw new OAuth2RequestException(OAuth2Error.SERVER_REJECTED_REQUEST,
"The accessor is not allowed to be sent to the domain of the request.", null);
}
}
try {
ret = this.fetcher.fetch(request);
} catch (final GadgetException e) {
throw new OAuth2RequestException(OAuth2Error.MISSING_SERVER_RESPONSE,
"GadgetException fetchFromServer", e);
}
final int responseCode = ret.getHttpStatusCode();
if (log.isLoggable(Level.FINEST)) {
log.logp(Level.FINEST, CLASS, method, "responseCode = {0}", new Object[]{responseCode});
}
if (responseCode == HttpResponse.SC_UNAUTHORIZED) {
if (accessToken != null) {
try {
this.tokenStore.removeAccessToken(accessor);
} catch (final GadgetException e) {
throw new OAuth2RequestException(OAuth2Error.MISSING_SERVER_RESPONSE,
"error removing access_token", null);
}
accessor.setAccessToken(null);
}