// Validate the access token
if (!Common.ACCESS_TOKEN_VALID.equals(accessToken)) {
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(OAuthError.ResourceResponse.INVALID_TOKEN)
.buildHeaderMessage();
//return Response.status(Response.Status.UNAUTHORIZED).build();
return Response.status(Response.Status.UNAUTHORIZED)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
// Return the resource
return Response.status(Response.Status.OK).entity(accessToken).build();
} catch (OAuthProblemException e) {
// Check if the error code has been set
String errorCode = e.getError();
if (OAuthUtils.isEmpty(errorCode)) {
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.buildHeaderMessage();
// If no error code then return a standard 401 Unauthorized response
return Response.status(Response.Status.UNAUTHORIZED)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(e.getError())
.setErrorDescription(e.getDescription())
.setErrorUri(e.getDescription())
.buildHeaderMessage();
return Response.status(Response.Status.BAD_REQUEST)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
}