Search in sources :

Example 1 with Param

use of org.asynchttpclient.Param in project async-http-client by AsyncHttpClient.

the class HttpUtils method urlEncodeFormParams0.

private static StringBuilder urlEncodeFormParams0(List<Param> params) {
    StringBuilder sb = StringBuilderPool.DEFAULT.stringBuilder();
    for (Param param : params) {
        encodeAndAppendFormParam(sb, param.getName(), param.getValue());
    }
    sb.setLength(sb.length() - 1);
    return sb;
}
Also used : Param(org.asynchttpclient.Param)

Example 2 with Param

use of org.asynchttpclient.Param in project async-http-client by AsyncHttpClient.

the class MultipartPart method visitCustomHeaders.

protected void visitCustomHeaders(PartVisitor visitor) {
    if (isNonEmpty(part.getCustomHeaders())) {
        for (Param param : part.getCustomHeaders()) {
            visitor.withBytes(CRLF_BYTES);
            visitor.withBytes(param.getName().getBytes(US_ASCII));
            visitor.withBytes(HEADER_NAME_VALUE_SEPARATOR_BYTES);
            visitor.withBytes(param.getValue().getBytes(US_ASCII));
        }
    }
}
Also used : Param(org.asynchttpclient.Param)

Example 3 with Param

use of org.asynchttpclient.Param in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testGetWithRequestBuilder.

@Test
public void testGetWithRequestBuilder() throws UnsupportedEncodingException {
    //
    StaticOAuthSignatureCalculator calc = new //
    StaticOAuthSignatureCalculator(//
    new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), //
    new RequestToken(TOKEN_KEY, TOKEN_SECRET), //
    NONCE, TIMESTAMP);
    final Request req = //
    get("http://photos.example.net/photos").addQueryParam("file", //
    "vacation.jpg").addQueryParam("size", //
    "original").setSignatureCalculator(//
    calc).build();
    final List<Param> params = req.getQueryParams();
    assertEquals(params.size(), 2);
    // From the signature tester, the URL should look like:
    // normalized parameters:
    // file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
    // signature base string:
    // GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
    // signature: tR3+Ty81lMeYAr/Fid0kMTYa/WM=
    // Authorization header: OAuth
    // realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"
    String authHeader = req.getHeaders().get(AUTHORIZATION);
    Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader);
    assertEquals(m.find(), true);
    String encodedSig = m.group(1);
    String sig = URLDecoder.decode(encodedSig, "UTF-8");
    assertEquals(sig, "tR3+Ty81lMeYAr/Fid0kMTYa/WM=");
    assertEquals(req.getUrl(), "http://photos.example.net/photos?file=vacation.jpg&size=original");
}
Also used : Matcher(java.util.regex.Matcher) Request(org.asynchttpclient.Request) Param(org.asynchttpclient.Param) Test(org.testng.annotations.Test)

Example 4 with Param

use of org.asynchttpclient.Param in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testGetWithRequestBuilderAndQuery.

@Test
public void testGetWithRequestBuilderAndQuery() throws UnsupportedEncodingException {
    //
    StaticOAuthSignatureCalculator calc = new //
    StaticOAuthSignatureCalculator(//
    new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), //
    new RequestToken(TOKEN_KEY, TOKEN_SECRET), //
    NONCE, TIMESTAMP);
    final Request req = //
    get("http://photos.example.net/photos?file=vacation.jpg&size=original").setSignatureCalculator(//
    calc).build();
    final List<Param> params = req.getQueryParams();
    assertEquals(params.size(), 2);
    // From the signature tester, the URL should look like:
    // normalized parameters:
    // file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
    // signature base string:
    // GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
    // signature: tR3+Ty81lMeYAr/Fid0kMTYa/WM=
    // Authorization header: OAuth
    // realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"
    String authHeader = req.getHeaders().get(AUTHORIZATION);
    Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader);
    assertTrue(m.find());
    String encodedSig = m.group(1);
    String sig = URLDecoder.decode(encodedSig, "UTF-8");
    assertEquals(sig, "tR3+Ty81lMeYAr/Fid0kMTYa/WM=");
    assertEquals(req.getUrl(), "http://photos.example.net/photos?file=vacation.jpg&size=original");
    assertEquals(authHeader, "OAuth oauth_consumer_key=\"dpf43f3p2l4k3l03\", oauth_token=\"nnch734d00sl2jdk\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D\", oauth_timestamp=\"1191242096\", oauth_nonce=\"kllo9940pd9333jh\", oauth_version=\"1.0\"");
}
Also used : Matcher(java.util.regex.Matcher) Request(org.asynchttpclient.Request) Param(org.asynchttpclient.Param) Test(org.testng.annotations.Test)

Example 5 with Param

use of org.asynchttpclient.Param in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorInstance method encodedParams.

private String encodedParams(ConsumerKey consumerAuth, RequestToken userAuth, long oauthTimestamp, String nonce, List<Param> formParams, List<Param> queryParams) {
    parameters.reset();
    /**
         * List of all query and form parameters added to this request; needed for calculating request signature
         */
    // start with standard OAuth parameters we need
    parameters.add(KEY_OAUTH_CONSUMER_KEY, Utf8UrlEncoder.percentEncodeQueryElement(consumerAuth.getKey())).add(KEY_OAUTH_NONCE, Utf8UrlEncoder.percentEncodeQueryElement(nonce)).add(KEY_OAUTH_SIGNATURE_METHOD, OAUTH_SIGNATURE_METHOD).add(KEY_OAUTH_TIMESTAMP, String.valueOf(oauthTimestamp));
    if (userAuth.getKey() != null) {
        parameters.add(KEY_OAUTH_TOKEN, Utf8UrlEncoder.percentEncodeQueryElement(userAuth.getKey()));
    }
    parameters.add(KEY_OAUTH_VERSION, OAUTH_VERSION_1_0);
    if (formParams != null) {
        for (Param param : formParams) {
            // formParams are not already encoded
            parameters.add(Utf8UrlEncoder.percentEncodeQueryElement(param.getName()), Utf8UrlEncoder.percentEncodeQueryElement(param.getValue()));
        }
    }
    if (queryParams != null) {
        for (Param param : queryParams) {
            // queryParams are already form-url-encoded
            // but OAuth1 uses RFC3986_UNRESERVED_CHARS so * and + have to be encoded
            parameters.add(percentEncodeAlreadyFormUrlEncoded(param.getName()), percentEncodeAlreadyFormUrlEncoded(param.getValue()));
        }
    }
    return parameters.sortAndConcat();
}
Also used : Param(org.asynchttpclient.Param)

Aggregations

Param (org.asynchttpclient.Param)5 Matcher (java.util.regex.Matcher)2 Request (org.asynchttpclient.Request)2 Test (org.testng.annotations.Test)2