Skip to content

Commit 4c8dc38

Browse files
committedMar 30, 2015
Merge pull request #1 from AsyncHttpClient/master
updating to latest
2 parents 24deb70 + adc0354 commit 4c8dc38

File tree

13 files changed

+156
-26
lines changed

13 files changed

+156
-26
lines changed
 

‎README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Async Http Client
22
-----------------
33

4+
[Javadoc](http://www.javadoc.io/doc/com.ning/async-http-client/)
5+
46
Getting started [HTML](https://asynchttpclient.github.io/async-http-client/),
57
with [WebSockets](http://jfarcand.wordpress.com/2011/12/21/writing-websocket-clients-using-asynchttpclient/)
68

@@ -10,7 +12,7 @@ Async Http Client library purpose is to allow Java applications to easily execut
1012
<dependency>
1113
<groupId>com.ning</groupId>
1214
<artifactId>async-http-client</artifactId>
13-
<version>1.9.11</version>
15+
<version>1.9.15</version>
1416
</dependency>
1517
```
1618

@@ -20,7 +22,7 @@ You can also download the artifact
2022

2123
[Maven Search](http://search.maven.org)
2224

23-
Then in your code you can simply do [Javadoc](https://asynchttpclient.github.io/async-http-client/apidocs/reference/packages.html)
25+
Then in your code you can simply do
2426

2527
```java
2628
import com.ning.http.client.*;

‎api/src/main/java/org/asynchttpclient/AsyncCompletionHandler.java

-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public void onThrowable(Throwable t) {
7272

7373
/**
7474
* Invoked once the HTTP response processing is finished.
75-
* <p/>
76-
* <p/>
77-
* Gets always invoked as last callback method.
7875
*
7976
* @param response The {@link Response}
8077
* @return T Value that will be returned by the associated {@link java.util.concurrent.Future}

‎api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

+36
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public class AsyncHttpClientConfig {
100100
protected int ioThreadMultiplier;
101101
protected String[] enabledProtocols;
102102
protected String[] enabledCipherSuites;
103+
protected Integer sslSessionCacheSize;
104+
protected Integer sslSessionTimeout;
103105
protected AsyncHttpProviderConfig<?, ?> providerConfig;
104106

105107
// AHC 2 specific
@@ -139,6 +141,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
139141
int ioThreadMultiplier, //
140142
String[] enabledProtocols,//
141143
String[] enabledCipherSuites,//
144+
Integer sslSessionCacheSize,//
145+
Integer sslSessionTimeout,//
142146
AsyncHttpProviderConfig<?, ?> providerConfig,//
143147
boolean spdyEnabled, //
144148
int spdyInitialWindowSize, //
@@ -173,6 +177,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
173177
this.ioThreadMultiplier = ioThreadMultiplier;
174178
this.enabledProtocols = enabledProtocols;
175179
this.enabledCipherSuites = enabledCipherSuites;
180+
this.sslSessionCacheSize = sslSessionCacheSize;
181+
this.sslSessionTimeout = sslSessionTimeout;
176182
this.providerConfig = providerConfig;
177183
this.spdyEnabled = spdyEnabled;
178184
this.spdyInitialWindowSize = spdyInitialWindowSize;
@@ -505,6 +511,20 @@ public String[] getEnabledCipherSuites() {
505511
return enabledCipherSuites;
506512
}
507513

514+
/**
515+
* since 1.9.13
516+
*/
517+
public Integer getSslSessionCacheSize() {
518+
return sslSessionCacheSize;
519+
}
520+
521+
/**
522+
* since 1.9.13
523+
*/
524+
public Integer getSslSessionTimeout() {
525+
return sslSessionTimeout;
526+
}
527+
508528
/**
509529
* Builder for an {@link AsyncHttpClient}
510530
*/
@@ -540,6 +560,8 @@ public static class Builder {
540560
private int ioThreadMultiplier = defaultIoThreadMultiplier();
541561
private String[] enabledProtocols;
542562
private String[] enabledCipherSuites;
563+
private Integer sslSessionCacheSize = defaultSslSessionCacheSize();
564+
private Integer sslSessionTimeout = defaultSslSessionTimeout();
543565
private AsyncHttpProviderConfig<?, ?> providerConfig;
544566

545567
// AHC 2
@@ -992,6 +1014,16 @@ public Builder setEnabledCipherSuites(String[] enabledCipherSuites) {
9921014
return this;
9931015
}
9941016

1017+
public Builder setSslSessionCacheSize(Integer sslSessionCacheSize) {
1018+
this.sslSessionCacheSize = sslSessionCacheSize;
1019+
return this;
1020+
}
1021+
1022+
public Builder setSslSessionTimeout(Integer sslSessionTimeout) {
1023+
this.sslSessionTimeout = sslSessionTimeout;
1024+
return this;
1025+
}
1026+
9951027
/**
9961028
* Create a config builder with values taken from the given prototype configuration.
9971029
*
@@ -1032,6 +1064,8 @@ public Builder(AsyncHttpClientConfig prototype) {
10321064
acceptAnyCertificate = prototype.acceptAnyCertificate;
10331065
enabledProtocols = prototype.enabledProtocols;
10341066
enabledCipherSuites = prototype.enabledCipherSuites;
1067+
sslSessionCacheSize = prototype.sslSessionCacheSize;
1068+
sslSessionTimeout = prototype.sslSessionTimeout;
10351069

10361070
spdyEnabled = prototype.isSpdyEnabled();
10371071
spdyInitialWindowSize = prototype.getSpdyInitialWindowSize();
@@ -1083,6 +1117,8 @@ public AsyncHttpClientConfig build() {
10831117
ioThreadMultiplier, //
10841118
enabledProtocols, //
10851119
enabledCipherSuites, //
1120+
sslSessionCacheSize, //
1121+
sslSessionTimeout, //
10861122
providerConfig, //
10871123
spdyEnabled, //
10881124
spdyInitialWindowSize, //

‎api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigBean.java

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ void configureDefaults() {
6464
disableUrlEncodingForBoundRequests = defaultDisableUrlEncodingForBoundRequests();
6565
strict302Handling = defaultStrict302Handling();
6666
acceptAnyCertificate = defaultAcceptAnyCertificate();
67+
sslSessionCacheSize = defaultSslSessionCacheSize();
68+
sslSessionTimeout = defaultSslSessionTimeout();
6769

6870
if (defaultUseProxySelector()) {
6971
proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector();
@@ -223,4 +225,14 @@ public AsyncHttpClientConfigBean setAcceptAnyCertificate(boolean acceptAnyCertif
223225
this.acceptAnyCertificate = acceptAnyCertificate;
224226
return this;
225227
}
228+
229+
public AsyncHttpClientConfigBean setSslSessionCacheSize(Integer sslSessionCacheSize) {
230+
this.sslSessionCacheSize = sslSessionCacheSize;
231+
return this;
232+
}
233+
234+
public AsyncHttpClientConfigBean setSslSessionTimeout(Integer sslSessionTimeout) {
235+
this.sslSessionTimeout = sslSessionTimeout;
236+
return this;
237+
}
226238
}

‎api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java

+8
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,12 @@ public static int defaultSpdyMaxConcurrentStreams() {
120120
public static boolean defaultAcceptAnyCertificate() {
121121
return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "acceptAnyCertificate");
122122
}
123+
124+
public static Integer defaultSslSessionCacheSize() {
125+
return Integer.getInteger(ASYNC_CLIENT + "sslSessionCacheSize");
126+
}
127+
128+
public static Integer defaultSslSessionTimeout() {
129+
return Integer.getInteger(ASYNC_CLIENT + "sslSessionTimeout");
130+
}
123131
}

‎api/src/main/java/org/asynchttpclient/SSLEngineFactory.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ public DefaultSSLEngineFactory(AsyncHttpClientConfig config) {
4444

4545
@Override
4646
public SSLEngine newSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException {
47-
SSLContext sslContext = config.getSSLContext();
48-
49-
if (sslContext == null)
50-
sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate());
47+
SSLContext sslContext = SslUtils.getInstance().getSSLContext(config);
5148

5249
SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort);
5350
if (!config.isAcceptAnyCertificate()) {

‎api/src/main/java/org/asynchttpclient/util/SslUtils.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.asynchttpclient.util;
1717

18-
import javax.net.ssl.SSLContext;
19-
import javax.net.ssl.TrustManager;
20-
import javax.net.ssl.X509TrustManager;
21-
2218
import java.security.GeneralSecurityException;
2319
import java.security.KeyManagementException;
2420
import java.security.NoSuchAlgorithmException;
2521
import java.security.SecureRandom;
2622

23+
import javax.net.ssl.SSLContext;
24+
import javax.net.ssl.TrustManager;
25+
import javax.net.ssl.X509TrustManager;
26+
27+
import org.asynchttpclient.AsyncHttpClientConfig;
28+
2729
/**
2830
* This class is a copy of http://github.com/sonatype/wagon-ning/raw/master/src/main/java/org/apache/maven/wagon/providers/http/SslUtils.java
2931
*/
@@ -64,7 +66,16 @@ public static SslUtils getInstance() {
6466
return SingletonHolder.instance;
6567
}
6668

67-
public SSLContext getSSLContext(boolean acceptAnyCertificate) throws GeneralSecurityException {
68-
return acceptAnyCertificate? looseTrustManagerSSLContext: SSLContext.getDefault();
69+
public SSLContext getSSLContext(AsyncHttpClientConfig config) throws GeneralSecurityException {
70+
SSLContext sslContext = config.getSSLContext();
71+
72+
if (sslContext == null) {
73+
sslContext = config.isAcceptAnyCertificate() ? looseTrustManagerSSLContext : SSLContext.getDefault();
74+
if (config.getSslSessionCacheSize() != null)
75+
sslContext.getClientSessionContext().setSessionCacheSize(config.getSslSessionCacheSize());
76+
if (config.getSslSessionTimeout() != null)
77+
sslContext.getClientSessionContext().setSessionTimeout(config.getSslSessionTimeout());
78+
}
79+
return sslContext;
6980
}
7081
}

‎api/src/main/java/org/asynchttpclient/ws/WebSocketUpgradeHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public final WebSocket onCompleted() throws Exception {
9191
for (WebSocketListener listener : listeners) {
9292
listener.onError(e);
9393
}
94-
return null;
94+
throw e;
9595
}
9696

9797
if (webSocket == null) {
98-
throw new IllegalStateException("WebSocket is null");
98+
throw new NullPointerException("webSocket");
9999
}
100100
return webSocket;
101101
}

‎api/src/test/java/org/asynchttpclient/ws/CloseCodeReasonMessageTest.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.testng.annotations.Test;
2828

2929
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.ExecutionException;
3031
import java.util.concurrent.atomic.AtomicReference;
3132

3233
public abstract class CloseCodeReasonMessageTest extends AbstractBasicTest {
@@ -100,6 +101,34 @@ public void onError(Throwable t) {
100101
}
101102
}
102103

104+
@Test(timeOut = 60000, expectedExceptions = { ExecutionException.class })
105+
public void getWebSocketThrowsException() throws Throwable {
106+
final CountDownLatch latch = new CountDownLatch(1);
107+
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
108+
client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
109+
110+
@Override
111+
public void onMessage(String message) {
112+
}
113+
114+
@Override
115+
public void onOpen(WebSocket websocket) {
116+
}
117+
118+
@Override
119+
public void onClose(WebSocket websocket) {
120+
}
121+
122+
@Override
123+
public void onError(Throwable t) {
124+
latch.countDown();
125+
}
126+
}).build()).get();
127+
}
128+
129+
latch.await();
130+
}
131+
103132
@Test(timeOut = 60000)
104133
public void wrongStatusCode() throws Throwable {
105134
try (AsyncHttpClient c = getAsyncHttpClient(null)) {
@@ -125,7 +154,7 @@ public void onError(Throwable t) {
125154
throwable.set(t);
126155
latch.countDown();
127156
}
128-
}).build()).get();
157+
}).build());
129158

130159
latch.await();
131160
assertNotNull(throwable.get());
@@ -158,7 +187,7 @@ public void onError(Throwable t) {
158187
throwable.set(t);
159188
latch.countDown();
160189
}
161-
}).build()).get();
190+
}).build());
162191

163192
latch.await();
164193
assertNotNull(throwable.get());

‎providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java

-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ private HttpClientCodec newHttpClientCodec() {
397397
public SslHandler createSslHandler(String peerHost, int peerPort) throws GeneralSecurityException, IOException {
398398
SSLEngine sslEngine = sslEngineFactory.newSSLEngine(peerHost, peerPort);
399399
SslHandler sslHandler = handshakeTimeout > 0 ? new SslHandler(sslEngine, getDefaultBufferPool(), false, nettyTimer, handshakeTimeout) : new SslHandler(sslEngine);
400-
sslHandler.setIssueHandshake(true);
401400
sslHandler.setCloseOnSSLException(true);
402401
return sslHandler;
403402
}

‎providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/request/NettyConnectListener.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jboss.netty.channel.Channel;
2626
import org.jboss.netty.channel.ChannelFuture;
2727
import org.jboss.netty.channel.ChannelFutureListener;
28+
import org.jboss.netty.handler.ssl.SslHandler;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

@@ -60,10 +61,12 @@ private void abortChannelPreemption(String partition) {
6061
channelManager.abortChannelPreemption(partition);
6162
}
6263

63-
private void writeRequest(Channel channel, String partition) {
64+
private void writeRequest(Channel channel) {
6465

6566
LOGGER.debug("Request using non cached Channel '{}':\n{}\n", channel, future.getNettyRequest().getHttpRequest());
6667

68+
Channels.setAttribute(channel, future);
69+
6770
if (future.isDone()) {
6871
abortChannelPreemption(partition);
6972
return;
@@ -79,8 +82,23 @@ private void writeRequest(Channel channel, String partition) {
7982
}
8083

8184
private void onFutureSuccess(final Channel channel) throws ConnectException {
82-
Channels.setAttribute(channel, future);
83-
writeRequest(channel, partition);
85+
SslHandler sslHandler = channel.getPipeline().get(SslHandler.class);
86+
87+
if (sslHandler != null) {
88+
sslHandler.handshake().addListener(new ChannelFutureListener() {
89+
90+
@Override
91+
public void operationComplete(ChannelFuture future) throws Exception {
92+
if (future.isSuccess())
93+
writeRequest(channel);
94+
else
95+
onFutureFailure(channel, future.getCause());
96+
}
97+
});
98+
99+
} else {
100+
writeRequest(channel);
101+
}
84102
}
85103

86104
private void onFutureFailure(Channel channel, Throwable cause) {

‎providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ protected String getTargetContentEncoding(String contentEncoding) throws Excepti
260260
public final void tryToOfferChannelToPool(Channel channel, boolean keepAlive, String partitionId) {
261261
if (channel.isActive() && keepAlive && channel.isActive()) {
262262
LOGGER.debug("Adding key: {} for channel {}", partitionId, channel);
263+
Channels.setDiscard(channel);
263264
channelPool.offer(channel, partitionId);
264265
if (maxConnectionsPerHostEnabled)
265266
channel2KeyPool.putIfAbsent(channel, partitionId);
266-
Channels.setDiscard(channel);
267267
} else {
268268
// not offered
269269
closeChannel(channel);

0 commit comments

Comments
 (0)
Please sign in to comment.