use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class OAuthSignatureCalculatorTest method testPostCalculateSignature.
@Test
public void testPostCalculateSignature() throws UnsupportedEncodingException {
//
StaticOAuthSignatureCalculator calc = new //
StaticOAuthSignatureCalculator(new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), new RequestToken(TOKEN_KEY, TOKEN_SECRET), NONCE, TIMESTAMP);
final Request req = post("http://photos.example.net/photos").addFormParam("file", "vacation.jpg").addFormParam("size", "original").setSignatureCalculator(calc).build();
// From the signature tester, POST 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:
// POST&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: wPkvxykrw+BTdCcGqKr+3I+PsiM=
// 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="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%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, "wPkvxykrw+BTdCcGqKr+3I+PsiM=");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class OAuthSignatureCalculatorTest method testWithNullRequestToken.
@Test
public void testWithNullRequestToken() throws NoSuchAlgorithmException {
final Request request = get("http://photos.example.net/photos?file=vacation.jpg&size=original").build();
String signatureBaseString = new OAuthSignatureCalculatorInstance().signatureBaseString(new ConsumerKey("9djdj82h48djs9d2", CONSUMER_SECRET), new RequestToken(null, null), request.getUri(), request.getMethod(), request.getFormParams(), request.getQueryParams(), 137131201, Utf8UrlEncoder.percentEncodeQueryElement("ZLc92RAkooZcIO/0cctl0Q==")).toString();
assertEquals(signatureBaseString, "GET&" + "http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" + "oauth_consumer_key%3D9djdj82h48djs9d2%26" + "oauth_nonce%3DZLc92RAkooZcIO%252F0cctl0Q%253D%253D%26" + "oauth_signature_method%3DHMAC-SHA1%26" + "oauth_timestamp%3D137131201%26" + "oauth_version%3D1.0%26size%3Doriginal");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class OAuthSignatureCalculatorTest method testSignatureBaseStringWithRawUri.
@Test
public void testSignatureBaseStringWithRawUri() throws NoSuchAlgorithmException {
// note: @ is legal so don't decode it into %40 because it won't be
// encoded back
// note: we don't know how to fix a = that should have been encoded as
// %3D but who would be stupid enough to do that?
Request request = post("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r b").addFormParam("c2", "").addFormParam("a3", "2 q").build();
testSignatureBaseString(request);
testSignatureBaseStringWithEncodableOAuthToken(request);
}
use of org.asynchttpclient.Request 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");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class NettyConnectListener method onSuccess.
public void onSuccess(Channel channel, InetSocketAddress remoteAddress) {
if (connectionSemaphore != null) {
// transfer lock from future to channel
Object partitionKeyLock = future.takePartitionKeyLock();
if (partitionKeyLock != null) {
channel.closeFuture().addListener(future -> connectionSemaphore.releaseChannelLock(partitionKeyLock));
}
}
Channels.setActiveToken(channel);
TimeoutsHolder timeoutsHolder = future.getTimeoutsHolder();
if (futureIsAlreadyCancelled(channel)) {
return;
}
Request request = future.getTargetRequest();
Uri uri = request.getUri();
timeoutsHolder.setResolvedRemoteAddress(remoteAddress);
ProxyServer proxyServer = future.getProxyServer();
// in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request
if ((proxyServer == null || proxyServer.getProxyType().isSocks()) && uri.isSecured()) {
SslHandler sslHandler;
try {
sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost(), proxyServer != null);
} catch (Exception sslError) {
onFailure(channel, sslError);
return;
}
final AsyncHandler<?> asyncHandler = future.getAsyncHandler();
try {
asyncHandler.onTlsHandshakeAttempt();
} catch (Exception e) {
LOGGER.error("onTlsHandshakeAttempt crashed", e);
onFailure(channel, e);
return;
}
sslHandler.handshakeFuture().addListener(new SimpleFutureListener<Channel>() {
@Override
protected void onSuccess(Channel value) {
try {
asyncHandler.onTlsHandshakeSuccess(sslHandler.engine().getSession());
} catch (Exception e) {
LOGGER.error("onTlsHandshakeSuccess crashed", e);
NettyConnectListener.this.onFailure(channel, e);
return;
}
writeRequest(channel);
}
@Override
protected void onFailure(Throwable cause) {
try {
asyncHandler.onTlsHandshakeFailure(cause);
} catch (Exception e) {
LOGGER.error("onTlsHandshakeFailure crashed", e);
NettyConnectListener.this.onFailure(channel, e);
return;
}
NettyConnectListener.this.onFailure(channel, cause);
}
});
} else {
writeRequest(channel);
}
}
Aggregations