public HttpRequest getAuthorizationRequest(final OAuth2Accessor accessor,
final String completeAuthorizationUrl) throws OAuth2RequestException {
if (completeAuthorizationUrl == null || completeAuthorizationUrl.length() == 0) {
throw new OAuth2RequestException(ClientCredentialsGrantTypeHandler.ERROR,
"completeAuthorizationUrl is null", null);
}
if (accessor == null) {
throw new OAuth2RequestException(ClientCredentialsGrantTypeHandler.ERROR, "accessor is null",
null);
}
if (!accessor.isValid() || accessor.isErrorResponse() || accessor.isRedirecting()) {
throw new OAuth2RequestException(ClientCredentialsGrantTypeHandler.ERROR,
"accessor is invalid", null);
}
if (!accessor.getGrantType().equalsIgnoreCase(OAuth2Message.CLIENT_CREDENTIALS)) {
throw new OAuth2RequestException(ClientCredentialsGrantTypeHandler.ERROR,
"grant type is not client_credentials", null);
}
final HttpRequest request = new HttpRequest(Uri.parse(completeAuthorizationUrl));
request.setMethod("POST");
request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
request.setSecurityToken(new AnonymousSecurityToken("", 0L, accessor.getGadgetUri()));
for (final ClientAuthenticationHandler clientAuthenticationHandler : this.clientAuthenticationHandlers) {
if (clientAuthenticationHandler.geClientAuthenticationType().equalsIgnoreCase(
accessor.getClientAuthenticationType())) {
final OAuth2HandlerError error = clientAuthenticationHandler.addOAuth2Authentication(
request, accessor);
if (error != null) {
throw new OAuth2RequestException(error.getError(), error.getContextMessage(),
error.getCause(), error.getUri(), error.getDescription());
}
}
}
try {
request.setPostBody(this.getAuthorizationBody(accessor).getBytes("UTF-8"));
} catch (final UnsupportedEncodingException e) {
throw new OAuth2RequestException(OAuth2Error.CLIENT_CREDENTIALS_PROBLEM,
"ClientCredentialsGrantTypeHandler - exception setting post body", e);
}
return request;
}