Search in sources :

Example 6 with HttpRequest

use of org.apache.shindig.gadgets.http.HttpRequest in project liferay-ide by liferay.

the class OAuthRequest method createHttpRequest.

private HttpRequest createHttpRequest(HttpRequest base, List<Map.Entry<String, String>> oauthParams) throws OAuthRequestException {
    OAuthParamLocation paramLocation = accessorInfo.getParamLocation();
    // paramLocation could be overriden by a run-time parameter to fetchRequest
    HttpRequest result = new HttpRequest(base);
    // OAuth service providers.
    if (paramLocation == OAuthParamLocation.POST_BODY && !result.getMethod().equals("POST")) {
        paramLocation = OAuthParamLocation.AUTH_HEADER;
    }
    switch(paramLocation) {
        case AUTH_HEADER:
            result.addHeader("Authorization", getAuthorizationHeader(oauthParams));
            break;
        case POST_BODY:
            String contentType = result.getHeader("Content-Type");
            if (!OAuth.isFormEncoded(contentType)) {
                throw new OAuthRequestException(OAuthError.INVALID_REQUEST, "OAuth param location can only be post_body if it is of " + "type x-www-form-urlencoded");
            }
            String oauthData = OAuthUtil.formEncode(oauthParams);
            if (result.getPostBodyLength() == 0) {
                result.setPostBody(CharsetUtil.getUtf8Bytes(oauthData));
            } else {
                StringBuilder postBody = new StringBuilder();
                postBody.append(result.getPostBodyAsString());
                if (!result.getPostBodyAsString().endsWith("&")) {
                    postBody.append('&');
                }
                postBody.append(oauthData);
                result.setPostBody(postBody.toString().getBytes());
            }
            break;
        case URI_QUERY:
            result.setUri(Uri.parse(OAuthUtil.addParameters(result.getUri().toString(), oauthParams)));
            break;
    }
    return result;
}
Also used : HttpRequest(org.apache.shindig.gadgets.http.HttpRequest) OAuthParamLocation(org.apache.shindig.gadgets.oauth.AccessorInfo.OAuthParamLocation)

Aggregations

HttpRequest (org.apache.shindig.gadgets.http.HttpRequest)6 Parameter (net.oauth.OAuth.Parameter)3 OAuthMessage (net.oauth.OAuthMessage)3 OAuthAccessor (net.oauth.OAuthAccessor)2 IOException (java.io.IOException)1 OAuthException (net.oauth.OAuthException)1 Uri (org.apache.shindig.common.uri.Uri)1 UriBuilder (org.apache.shindig.common.uri.UriBuilder)1 HttpResponse (org.apache.shindig.gadgets.http.HttpResponse)1 HttpResponseBuilder (org.apache.shindig.gadgets.http.HttpResponseBuilder)1 OAuthParamLocation (org.apache.shindig.gadgets.oauth.AccessorInfo.OAuthParamLocation)1