Skip to content

updating to latest #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Async Http Client
-----------------

[Javadoc](http://www.javadoc.io/doc/com.ning/async-http-client/)

Getting started [HTML](https://asynchttpclient.github.io/async-http-client/),
with [WebSockets](http://jfarcand.wordpress.com/2011/12/21/writing-websocket-clients-using-asynchttpclient/)

Expand All @@ -10,7 +12,7 @@ Async Http Client library purpose is to allow Java applications to easily execut
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.9.11</version>
<version>1.9.15</version>
</dependency>
```

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

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

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

```java
import com.ning.http.client.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public void onThrowable(Throwable t) {

/**
* Invoked once the HTTP response processing is finished.
* <p/>
* <p/>
* Gets always invoked as last callback method.
*
* @param response The {@link Response}
* @return T Value that will be returned by the associated {@link java.util.concurrent.Future}
Expand Down
36 changes: 36 additions & 0 deletions api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public class AsyncHttpClientConfig {
protected int ioThreadMultiplier;
protected String[] enabledProtocols;
protected String[] enabledCipherSuites;
protected Integer sslSessionCacheSize;
protected Integer sslSessionTimeout;
protected AsyncHttpProviderConfig<?, ?> providerConfig;

// AHC 2 specific
Expand Down Expand Up @@ -139,6 +141,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
int ioThreadMultiplier, //
String[] enabledProtocols,//
String[] enabledCipherSuites,//
Integer sslSessionCacheSize,//
Integer sslSessionTimeout,//
AsyncHttpProviderConfig<?, ?> providerConfig,//
boolean spdyEnabled, //
int spdyInitialWindowSize, //
Expand Down Expand Up @@ -173,6 +177,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
this.ioThreadMultiplier = ioThreadMultiplier;
this.enabledProtocols = enabledProtocols;
this.enabledCipherSuites = enabledCipherSuites;
this.sslSessionCacheSize = sslSessionCacheSize;
this.sslSessionTimeout = sslSessionTimeout;
this.providerConfig = providerConfig;
this.spdyEnabled = spdyEnabled;
this.spdyInitialWindowSize = spdyInitialWindowSize;
Expand Down Expand Up @@ -505,6 +511,20 @@ public String[] getEnabledCipherSuites() {
return enabledCipherSuites;
}

/**
* since 1.9.13
*/
public Integer getSslSessionCacheSize() {
return sslSessionCacheSize;
}

/**
* since 1.9.13
*/
public Integer getSslSessionTimeout() {
return sslSessionTimeout;
}

/**
* Builder for an {@link AsyncHttpClient}
*/
Expand Down Expand Up @@ -540,6 +560,8 @@ public static class Builder {
private int ioThreadMultiplier = defaultIoThreadMultiplier();
private String[] enabledProtocols;
private String[] enabledCipherSuites;
private Integer sslSessionCacheSize = defaultSslSessionCacheSize();
private Integer sslSessionTimeout = defaultSslSessionTimeout();
private AsyncHttpProviderConfig<?, ?> providerConfig;

// AHC 2
Expand Down Expand Up @@ -992,6 +1014,16 @@ public Builder setEnabledCipherSuites(String[] enabledCipherSuites) {
return this;
}

public Builder setSslSessionCacheSize(Integer sslSessionCacheSize) {
this.sslSessionCacheSize = sslSessionCacheSize;
return this;
}

public Builder setSslSessionTimeout(Integer sslSessionTimeout) {
this.sslSessionTimeout = sslSessionTimeout;
return this;
}

/**
* Create a config builder with values taken from the given prototype configuration.
*
Expand Down Expand Up @@ -1032,6 +1064,8 @@ public Builder(AsyncHttpClientConfig prototype) {
acceptAnyCertificate = prototype.acceptAnyCertificate;
enabledProtocols = prototype.enabledProtocols;
enabledCipherSuites = prototype.enabledCipherSuites;
sslSessionCacheSize = prototype.sslSessionCacheSize;
sslSessionTimeout = prototype.sslSessionTimeout;

spdyEnabled = prototype.isSpdyEnabled();
spdyInitialWindowSize = prototype.getSpdyInitialWindowSize();
Expand Down Expand Up @@ -1083,6 +1117,8 @@ public AsyncHttpClientConfig build() {
ioThreadMultiplier, //
enabledProtocols, //
enabledCipherSuites, //
sslSessionCacheSize, //
sslSessionTimeout, //
providerConfig, //
spdyEnabled, //
spdyInitialWindowSize, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void configureDefaults() {
disableUrlEncodingForBoundRequests = defaultDisableUrlEncodingForBoundRequests();
strict302Handling = defaultStrict302Handling();
acceptAnyCertificate = defaultAcceptAnyCertificate();
sslSessionCacheSize = defaultSslSessionCacheSize();
sslSessionTimeout = defaultSslSessionTimeout();

if (defaultUseProxySelector()) {
proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector();
Expand Down Expand Up @@ -223,4 +225,14 @@ public AsyncHttpClientConfigBean setAcceptAnyCertificate(boolean acceptAnyCertif
this.acceptAnyCertificate = acceptAnyCertificate;
return this;
}

public AsyncHttpClientConfigBean setSslSessionCacheSize(Integer sslSessionCacheSize) {
this.sslSessionCacheSize = sslSessionCacheSize;
return this;
}

public AsyncHttpClientConfigBean setSslSessionTimeout(Integer sslSessionTimeout) {
this.sslSessionTimeout = sslSessionTimeout;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,12 @@ public static int defaultSpdyMaxConcurrentStreams() {
public static boolean defaultAcceptAnyCertificate() {
return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "acceptAnyCertificate");
}

public static Integer defaultSslSessionCacheSize() {
return Integer.getInteger(ASYNC_CLIENT + "sslSessionCacheSize");
}

public static Integer defaultSslSessionTimeout() {
return Integer.getInteger(ASYNC_CLIENT + "sslSessionTimeout");
}
}
5 changes: 1 addition & 4 deletions api/src/main/java/org/asynchttpclient/SSLEngineFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public DefaultSSLEngineFactory(AsyncHttpClientConfig config) {

@Override
public SSLEngine newSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException {
SSLContext sslContext = config.getSSLContext();

if (sslContext == null)
sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate());
SSLContext sslContext = SslUtils.getInstance().getSSLContext(config);

SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort);
if (!config.isAcceptAnyCertificate()) {
Expand Down
23 changes: 17 additions & 6 deletions api/src/main/java/org/asynchttpclient/util/SslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
*/
package org.asynchttpclient.util;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.asynchttpclient.AsyncHttpClientConfig;

/**
* 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
*/
Expand Down Expand Up @@ -64,7 +66,16 @@ public static SslUtils getInstance() {
return SingletonHolder.instance;
}

public SSLContext getSSLContext(boolean acceptAnyCertificate) throws GeneralSecurityException {
return acceptAnyCertificate? looseTrustManagerSSLContext: SSLContext.getDefault();
public SSLContext getSSLContext(AsyncHttpClientConfig config) throws GeneralSecurityException {
SSLContext sslContext = config.getSSLContext();

if (sslContext == null) {
sslContext = config.isAcceptAnyCertificate() ? looseTrustManagerSSLContext : SSLContext.getDefault();
if (config.getSslSessionCacheSize() != null)
sslContext.getClientSessionContext().setSessionCacheSize(config.getSslSessionCacheSize());
if (config.getSslSessionTimeout() != null)
sslContext.getClientSessionContext().setSessionTimeout(config.getSslSessionTimeout());
}
return sslContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public final WebSocket onCompleted() throws Exception {
for (WebSocketListener listener : listeners) {
listener.onError(e);
}
return null;
throw e;
}

if (webSocket == null) {
throw new IllegalStateException("WebSocket is null");
throw new NullPointerException("webSocket");
}
return webSocket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.testng.annotations.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;

public abstract class CloseCodeReasonMessageTest extends AbstractBasicTest {
Expand Down Expand Up @@ -100,6 +101,34 @@ public void onError(Throwable t) {
}
}

@Test(timeOut = 60000, expectedExceptions = { ExecutionException.class })
public void getWebSocketThrowsException() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

@Override
public void onMessage(String message) {
}

@Override
public void onOpen(WebSocket websocket) {
}

@Override
public void onClose(WebSocket websocket) {
}

@Override
public void onError(Throwable t) {
latch.countDown();
}
}).build()).get();
}

latch.await();
}

@Test(timeOut = 60000)
public void wrongStatusCode() throws Throwable {
try (AsyncHttpClient c = getAsyncHttpClient(null)) {
Expand All @@ -125,7 +154,7 @@ public void onError(Throwable t) {
throwable.set(t);
latch.countDown();
}
}).build()).get();
}).build());

latch.await();
assertNotNull(throwable.get());
Expand Down Expand Up @@ -158,7 +187,7 @@ public void onError(Throwable t) {
throwable.set(t);
latch.countDown();
}
}).build()).get();
}).build());

latch.await();
assertNotNull(throwable.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ private HttpClientCodec newHttpClientCodec() {
public SslHandler createSslHandler(String peerHost, int peerPort) throws GeneralSecurityException, IOException {
SSLEngine sslEngine = sslEngineFactory.newSSLEngine(peerHost, peerPort);
SslHandler sslHandler = handshakeTimeout > 0 ? new SslHandler(sslEngine, getDefaultBufferPool(), false, nettyTimer, handshakeTimeout) : new SslHandler(sslEngine);
sslHandler.setIssueHandshake(true);
sslHandler.setCloseOnSSLException(true);
return sslHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.handler.ssl.SslHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -60,10 +61,12 @@ private void abortChannelPreemption(String partition) {
channelManager.abortChannelPreemption(partition);
}

private void writeRequest(Channel channel, String partition) {
private void writeRequest(Channel channel) {

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

Channels.setAttribute(channel, future);

if (future.isDone()) {
abortChannelPreemption(partition);
return;
Expand All @@ -79,8 +82,23 @@ private void writeRequest(Channel channel, String partition) {
}

private void onFutureSuccess(final Channel channel) throws ConnectException {
Channels.setAttribute(channel, future);
writeRequest(channel, partition);
SslHandler sslHandler = channel.getPipeline().get(SslHandler.class);

if (sslHandler != null) {
sslHandler.handshake().addListener(new ChannelFutureListener() {

@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess())
writeRequest(channel);
else
onFutureFailure(channel, future.getCause());
}
});

} else {
writeRequest(channel);
}
}

private void onFutureFailure(Channel channel, Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ protected String getTargetContentEncoding(String contentEncoding) throws Excepti
public final void tryToOfferChannelToPool(Channel channel, boolean keepAlive, String partitionId) {
if (channel.isActive() && keepAlive && channel.isActive()) {
LOGGER.debug("Adding key: {} for channel {}", partitionId, channel);
Channels.setDiscard(channel);
channelPool.offer(channel, partitionId);
if (maxConnectionsPerHostEnabled)
channel2KeyPool.putIfAbsent(channel, partitionId);
Channels.setDiscard(channel);
} else {
// not offered
closeChannel(channel);
Expand Down
Loading