Skip to content

Commit

Permalink
Upgrade to AsyncHttpClient 1.9.29
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Schalanda committed Jul 2, 2015
1 parent 89b3180 commit 5d82d2a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 173 deletions.
Expand Up @@ -62,7 +62,7 @@ public String getMessage() {
sb.append(request.getMethod());
sb.append(' ');
try {
final URI uri = request.getURI();
final URI uri = request.getUri().toJavaNetURI();
final String userInfo = uri.getUserInfo();
String username = "";
if (userInfo != null) {
Expand Down
Expand Up @@ -37,7 +37,6 @@
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.PerRequestConfig;
import com.ning.http.client.Realm;
import com.ning.http.client.Request;
import com.ning.http.client.Response;
Expand Down Expand Up @@ -92,12 +91,12 @@ class ApiClientImpl implements ApiClient {
@Inject
private ApiClientImpl(ServerNodes serverNodes, @Named("Default Timeout") Long defaultTimeout) {
this(serverNodes, defaultTimeout,
new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new GuavaModule())
.registerModule(new JodaModule()));
new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new GuavaModule())
.registerModule(new JodaModule()));
}

private ApiClientImpl(ServerNodes serverNodes, Long defaultTimeout, ObjectMapper objectMapper) {
Expand All @@ -109,7 +108,7 @@ private ApiClientImpl(ServerNodes serverNodes, Long defaultTimeout, ObjectMapper
@Override
public void start() {
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setAllowPoolingConnection(false);
builder.setAllowPoolingConnections(false);
builder.setUserAgent("graylog2-web/" + Version.VERSION);
client = new AsyncHttpClient(builder.build());

Expand Down Expand Up @@ -603,7 +602,7 @@ public Response onCompleted(Response response) throws Exception {
final ListenableFuture<Response> future = context.listenableFuture;
try {
final Response response = future.get(timeoutValue, timeoutUnit);
if(response == null) {
if (response == null) {
LOG.error("Didn't receive response from node {}", node);
node.markFailure();
continue;
Expand Down Expand Up @@ -662,7 +661,7 @@ private AsyncHttpClient.BoundRequestBuilder requestBuilderForUrl(URL url) throws
}

applyBasicAuthentication(requestBuilder, userInfo);
requestBuilder.setPerRequestConfig(new PerRequestConfig(null, (int) timeoutUnit.toMillis(timeoutValue)));
requestBuilder.setRequestTimeout((int) timeoutUnit.toMillis(timeoutValue));

if (body != null) {
if (method != Method.PUT && method != Method.POST) {
Expand Down Expand Up @@ -779,13 +778,12 @@ public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) {
}

@Override
public void onBytesReceived(ByteBuffer buffer) throws IOException {
stream.putBuffer(buffer);
public void onBytesSent(long amount, long current, long total) {
}

@Override
public void onBytesSent(ByteBuffer buffer) {

public void onBytesReceived(byte[] bytes) throws IOException {
stream.putBuffer(ByteBuffer.wrap(bytes));
}

@Override
Expand All @@ -799,10 +797,7 @@ public void onThrowable(Throwable t) {
}
}));
return stream;
} catch (MalformedURLException e) {
LOG.error("Malformed URL", e);
throw new RuntimeException("Malformed URL.", e);
} catch (IOException e) {
} catch (Exception e) {
LOG.error("unhandled IOException", rootCause(e));
target.markFailure();
throw e;
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.uri.Uri;
import org.graylog2.restclient.models.Node;
import org.graylog2.restclient.models.api.responses.EmptyResponse;
import org.junit.After;
Expand Down Expand Up @@ -55,13 +56,13 @@ public void testBuildTarget() throws Exception {

final URL queryParamWithDoubleQuotes = api.get(EmptyResponse.class).path("/some/resource").queryParam("query", " \".+\"").node(node).unauthenticated().prepareUrl(node);
Assert.assertEquals("query param with \" should be escaped",
"query=+%22.%2B%22",
queryParamWithDoubleQuotes.getQuery());
"query=+%22.%2B%22",
queryParamWithDoubleQuotes.getQuery());

final URL urlWithNonAsciiChars = api.get(EmptyResponse.class).node(node).path("/some/resourçe").unauthenticated().prepareUrl(node);
Assert.assertEquals("non-ascii chars are escaped in path",
"/some/resour%C3%A7e",
urlWithNonAsciiChars.getPath());
"/some/resour%C3%A7e",
urlWithNonAsciiChars.getPath());

final URL queryWithAmp = api.get(EmptyResponse.class).node(node).path("/").queryParam("foo", "this&that").prepareUrl(node);
Assert.assertEquals("Query params are escaped", "foo=this%26that", queryWithAmp.getQuery());
Expand All @@ -81,7 +82,8 @@ public void testSingleExecute() throws Exception {
.unauthenticated()
.node(node)
.timeout(1, TimeUnit.SECONDS);
stubHttpProvider.expectResponse(requestBuilder.prepareUrl(node), 200, "{}");
final URL url = requestBuilder.prepareUrl(node);
stubHttpProvider.expectResponse(Uri.create(url.toString()), 200, "{}");
final EmptyResponse response = requestBuilder.execute();

Assert.assertNotNull(response);
Expand All @@ -101,8 +103,8 @@ public void testParallelExecution() throws Exception {
final ApiRequestBuilder<EmptyResponse> requestBuilder = api.get(EmptyResponse.class).path("/some/resource");
final URL url1 = requestBuilder.prepareUrl(node1);
final URL url2 = requestBuilder.prepareUrl(node2);
stubHttpProvider.expectResponse(url1, 200, "{}");
stubHttpProvider.expectResponse(url2, 200, "{}");
stubHttpProvider.expectResponse(Uri.create(url1.toString()), 200, "{}");
stubHttpProvider.expectResponse(Uri.create(url2.toString()), 200, "{}");

final Map<Node, EmptyResponse> responses = requestBuilder.nodes(node1, node2).executeOnAll();
Assert.assertFalse(responses.isEmpty());
Expand All @@ -112,7 +114,7 @@ public void testParallelExecution() throws Exception {
@Before
public void setUp() throws Exception {
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setAllowPoolingConnection(false);
builder.setAllowPoolingConnections(false);
stubHttpProvider = new StubHttpProvider();
client = new AsyncHttpClient(stubHttpProvider, builder.build());
}
Expand Down
Expand Up @@ -30,7 +30,10 @@
import java.util.Map;
import java.util.UUID;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ServerNodesTest extends BaseApiTest {

Expand Down Expand Up @@ -62,7 +65,7 @@ public void testFailureCountSingleExecute() throws Exception {

@Test
public void testFailureCountParallelExecute() throws Exception {
setupNodes(AddressNodeId.create("http://localhost:65535"),AddressNodeId.create("http://localhost:65534"));
setupNodes(AddressNodeId.create("http://localhost:65535"), AddressNodeId.create("http://localhost:65534"));
api.setHttpClient(client);

final Map<Node, EmptyResponse> emptyResponses = api.put().path("/").executeOnAll();
Expand All @@ -71,7 +74,7 @@ public void testFailureCountParallelExecute() throws Exception {
final List<Node> nodes = serverNodes.all(true);
Node failedNode = nodes.get(0);
Node failedNode2 = nodes.get(1);
assertFalse("Node should be inactive" , failedNode.isActive());
assertFalse("Node should be inactive", failedNode.isActive());
assertFalse("Node should be inactive", failedNode2.isActive());
assertEquals(1, failedNode.getFailureCount());
assertEquals(1, failedNode2.getFailureCount());
Expand Down Expand Up @@ -119,7 +122,7 @@ public void testNodeObjectsRememberedByAddress() throws Exception {
@Before
public void setUp() throws Exception {
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setAllowPoolingConnection(false);
builder.setAllowPoolingConnections(false);
client = new AsyncHttpClient(builder.build());
}

Expand Down

0 comments on commit 5d82d2a

Please sign in to comment.