Search in sources :

Example 1 with OAuth

use of net.oauth.OAuth in project bigbluebutton by bigbluebutton.

the class HttpMessage method newRequest.

/**
     * Construct an HTTP request from this OAuth message.
     * 
     * @param style
     *            where to put the OAuth parameters, within the HTTP request
     */
public static HttpMessage newRequest(OAuthMessage from, ParameterStyle style) throws IOException {
    final boolean isPost = OAuthMessage.POST.equalsIgnoreCase(from.method);
    InputStream body = from.getBodyAsStream();
    if (style == ParameterStyle.BODY && !(isPost && body == null)) {
        style = ParameterStyle.QUERY_STRING;
    }
    String url = from.URL;
    final List<Map.Entry<String, String>> headers = new ArrayList<Map.Entry<String, String>>(from.getHeaders());
    switch(style) {
        case QUERY_STRING:
            url = OAuth.addParameters(url, from.getParameters());
            break;
        case BODY:
            {
                byte[] form = OAuth.formEncode(from.getParameters()).getBytes(from.getBodyEncoding());
                headers.add(new OAuth.Parameter(CONTENT_TYPE, OAuth.FORM_ENCODED));
                headers.add(new OAuth.Parameter(CONTENT_LENGTH, form.length + ""));
                body = new ByteArrayInputStream(form);
                break;
            }
        case AUTHORIZATION_HEADER:
            headers.add(new OAuth.Parameter("Authorization", from.getAuthorizationHeader(null)));
            // Find the non-OAuth parameters:
            List<Map.Entry<String, String>> others = from.getParameters();
            if (others != null && !others.isEmpty()) {
                others = new ArrayList<Map.Entry<String, String>>(others);
                for (Iterator<Map.Entry<String, String>> p = others.iterator(); p.hasNext(); ) {
                    if (p.next().getKey().startsWith("oauth_")) {
                        p.remove();
                    }
                }
                // Place the non-OAuth parameters elsewhere in the request:
                if (isPost && body == null) {
                    byte[] form = OAuth.formEncode(others).getBytes(from.getBodyEncoding());
                    headers.add(new OAuth.Parameter(CONTENT_TYPE, OAuth.FORM_ENCODED));
                    headers.add(new OAuth.Parameter(CONTENT_LENGTH, form.length + ""));
                    body = new ByteArrayInputStream(form);
                } else {
                    url = OAuth.addParameters(url, others);
                }
            }
            break;
    }
    HttpMessage httpRequest = new HttpMessage(from.method, new URL(url), body);
    httpRequest.headers.addAll(headers);
    return httpRequest;
}
Also used : ExcerptInputStream(net.oauth.client.ExcerptInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) URL(java.net.URL) OAuth(net.oauth.OAuth) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 OAuth (net.oauth.OAuth)1 ExcerptInputStream (net.oauth.client.ExcerptInputStream)1