public String getAsHeaderElement(String key) { String value = getFirst(key); if (value == null) { return null; } return key + "=\"" + value + "\""; }
public void putAll(String[] keyValuePairs, boolean percentEncode) { for (int i = 0; i < keyValuePairs.length - 1; i += 2) { this.put(keyValuePairs[i], keyValuePairs[i + 1], percentEncode); } }
/** * Concatenates all values for the given key to a list of key/value pairs * suitable for use in a URL query string. * * @param key * the parameter name * @return the query string */ public String getAsQueryString(Object key) { return getAsQueryString(key, true); }
public HttpParameters getOAuthParameters() { HttpParameters oauthParams = new HttpParameters(); for (Entry<String, SortedSet<String>> param : this.entrySet()) { String key = param.getKey(); if (key.startsWith("oauth_") || key.startsWith("x_oauth_")) { oauthParams.put(key, param.getValue()); } } return oauthParams; } }
/** * Collects OAuth Authorization header parameters as per OAuth Core 1.0 spec * section 9.1.1 */ protected void collectHeaderParameters(HttpRequest request, HttpParameters out) { HttpParameters headerParams = OAuth.oauthHeaderToParamsMap(request.getHeader(OAuth.HTTP_AUTHORIZATION_HEADER)); out.putAll(headerParams, false); }
public static HttpParameters oauthHeaderToParamsMap(String oauthHeader) { HttpParameters params = new HttpParameters(); if (oauthHeader == null || !oauthHeader.startsWith("OAuth ")) { return params; } oauthHeader = oauthHeader.substring("OAuth ".length()); String[] elements = oauthHeader.split(","); for (String keyValuePair : elements) { String[] keyValue = keyValuePair.split("="); params.put(keyValue[0].trim(), keyValue[1].replace("\"", "").trim()); } return params; }
@Override protected void closeConnection(HttpRequest request, HttpResponse response) { HttpURLConnection connection = (HttpURLConnection) request.unwrap(); if (connection != null) { connection.disconnect(); } } }
/** * Collects x-www-form-urlencoded body parameters as per OAuth Core 1.0 spec * section 9.1.1 */ protected void collectBodyParameters(HttpRequest request, HttpParameters out) throws IOException { // collect x-www-form-urlencoded body params String contentType = request.getContentType(); if (contentType != null && contentType.startsWith(OAuth.FORM_ENCODED)) { InputStream payload = request.getMessagePayload(); out.putAll(OAuth.decodeForm(payload), true); } }
public AbstractOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, String authorizationWebsiteUrl) { this.requestTokenEndpointUrl = requestTokenEndpointUrl; this.accessTokenEndpointUrl = accessTokenEndpointUrl; this.authorizationWebsiteUrl = authorizationWebsiteUrl; this.responseParameters = new HttpParameters(); this.defaultHeaders = new HashMap<String, String>(); }
/** * Collects HTTP GET query string parameters as per OAuth Core 1.0 spec * section 9.1.1 */ protected void collectQueryParameters(HttpRequest request, HttpParameters out) { String url = request.getRequestUrl(); int q = url.indexOf('?'); if (q >= 0) { // Combine the URL query string with the other parameters: out.putAll(OAuth.decodeForm(url.substring(q + 1)), true); } }
@Override protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response) throws Exception { if (response != null) { HttpMethod method = (HttpMethod) response.unwrap(); if (method != null) { method.releaseConnection(); } } } }
/** * Convenience method to add a single value for the parameter specified by * 'key'. * * @param key * the parameter name * @param value * the parameter value * @return the value */ public String put(String key, String value) { return put(key, value, false); }
/** * Convenience method for {@link #getFirst(key, false)}. * * @param key * the parameter name (must be percent encoded if it contains unsafe * characters!) * @return the first value found for this parameter */ public String getFirst(Object key) { return getFirst(key, false); }
/** * Convenience method to allow for storing null values. {@link #put} doesn't * allow null values, because that would be ambiguous. * * @param key * the parameter name * @param nullString * can be anything, but probably... null? * @return null */ public String putNull(String key, String nullString) { return put(key, nullString); }
/** * Returns a single query parameter as served by the service provider in a * token reply. You must call {@link #setResponseParameters} with the set of * parameters before using this method. * * @param key * the parameter name * @return the parameter value */ protected String getResponseParameter(String key) { return responseParameters.getFirst(key); }
@BeforeClass public static void initOAuthParams() { OAUTH_PARAMS.put("oauth_consumer_key", CONSUMER_KEY); OAUTH_PARAMS.put("oauth_signature_method", SIGNATURE_METHOD); OAUTH_PARAMS.put("oauth_timestamp", TIMESTAMP); OAUTH_PARAMS.put("oauth_nonce", NONCE); OAUTH_PARAMS.put("oauth_version", OAUTH_VERSION); OAUTH_PARAMS.put("oauth_token", TOKEN); }
public boolean matches(Object argument) { HttpParameters params = (HttpParameters) argument; assertEquals("1 1", params.getFirst("a", true)); assertEquals("2 2", params.getFirst("b", true)); assertEquals("http://example.com", params.getFirst("realm", true)); assertEquals("12%345", params.getFirst("oauth_token", true)); // signature should be dropped, not valid to pre-set assertNull(params.getFirst("oauth_signature")); return true; } };
private void assertAllOAuthParametersExist(HttpParameters params) { assertNotNull(params.getFirst("oauth_consumer_key")); assertNotNull(params.getFirst("oauth_token")); assertNotNull(params.getFirst("oauth_signature_method")); assertNotNull(params.getFirst("oauth_signature")); assertNotNull(params.getFirst("oauth_timestamp")); assertNotNull(params.getFirst("oauth_nonce")); assertNotNull(params.getFirst("oauth_version")); }