String postBody = params.get("--postBody");
String postFile = params.get("--postFile");
String paramLocation = params.get("--paramLocation");
String bodySigning = params.get("--bodySigning");
HttpRequest request = new HttpRequest(Uri.parse(url));
if (contentType != null) {
request.setHeader("Content-Type", contentType);
} else {
request.setHeader("Content-Type", OAuth.FORM_ENCODED);
}
if (postBody != null) {
request.setPostBody(postBody.getBytes());
}
if (postFile != null) {
request.setPostBody(IOUtils.toByteArray(new FileInputStream(postFile)));
}
OAuthParamLocation paramLocationEnum = OAuthParamLocation.URI_QUERY;
if (paramLocation != null) {
paramLocationEnum = OAuthParamLocation.valueOf(paramLocation);
}
BodySigning bodySigningEnum = BodySigning.none;
if (bodySigning != null) {
bodySigningEnum = BodySigning.valueOf(bodySigning);
}
List<OAuth.Parameter> oauthParams = Lists.newArrayList();
UriBuilder target = new UriBuilder(Uri.parse(url));
String query = target.getQuery();
target.setQuery(null);
oauthParams.addAll(OAuth.decodeForm(query));
if (OAuth.isFormEncoded(contentType) && request.getPostBodyAsString() != null) {
oauthParams.addAll(OAuth.decodeForm(request.getPostBodyAsString()));
} else if (bodySigningEnum == BodySigning.legacy) {
oauthParams.add(new OAuth.Parameter(request.getPostBodyAsString(), ""));
} else if (bodySigningEnum == BodySigning.hash) {
oauthParams.add(
new OAuth.Parameter(OAuthConstants.OAUTH_BODY_HASH,
new String(Base64.encodeBase64(DigestUtils.sha(postBody.getBytes())), "UTF-8")));
}
if (consumerKey != null) {
oauthParams.add(new OAuth.Parameter(OAuth.OAUTH_CONSUMER_KEY, consumerKey));
}
if (xOauthRequestor != null) {
oauthParams.add(new OAuth.Parameter("xoauth_requestor_id", xOauthRequestor));
}
OAuthConsumer consumer = new OAuthConsumer(null, consumerKey, consumerSecret, null);
OAuthAccessor accessor = new OAuthAccessor(consumer);
accessor.accessToken = accessToken;
OAuthMessage message = accessor.newRequestMessage(method, target.toString(), oauthParams);
List<Map.Entry<String, String>> entryList = OAuthRequest.selectOAuthParams(message);
switch (paramLocationEnum) {
case AUTH_HEADER:
request.addHeader("Authorization", OAuthRequest.getAuthorizationHeader(entryList));
break;
case POST_BODY:
if (!OAuth.isFormEncoded(contentType)) {
throw new RuntimeException(
"OAuth param location can only be post_body if post body if of " +
"type x-www-form-urlencoded");
}
String oauthData = OAuthUtil.formEncode(message.getParameters());
request.setPostBody(CharsetUtil.getUtf8Bytes(oauthData));
break;
case URI_QUERY:
request.setUri(Uri.parse(OAuthUtil.addParameters(request.getUri().toString(),
entryList)));
break;
}
request.setMethod(method);
HttpFetcher fetcher = new BasicHttpFetcher();
HttpResponse response = fetcher.fetch(request);
System.out.println("Request ------------------------------");
System.out.println(request.toString());
System.out.println("Response -----------------------------");
System.out.println(response.toString());
}