Replace com.ning/async-http-client with new package

Notice at:
https://github.com/ning/async-http-client
This commit is contained in:
Michael Walker 2018-02-09 14:43:18 +00:00
parent 7a01c054da
commit 9bb7576f0b
9 changed files with 34 additions and 90 deletions

View file

@ -80,11 +80,11 @@
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ning/async-http-client -->
<!-- https://mvnrepository.com/artifact/org.asynchttpclient/async-http-client -->
<dependency>
<groupId>com.ning</groupId>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>1.9.40</version>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
<dependency>

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.bridge.resource;
import com.ning.http.client.AsyncHttpClient;
import static org.asynchttpclient.Dsl.*;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
@ -31,7 +31,7 @@ public class UrlResourceCache implements ResourceCache {
}
public UrlResourceCache(DBStore dbStore) {
this(dbStore, new NingHttpClient(new AsyncHttpClient()));
this(dbStore, new NingHttpClient(asyncHttpClient()));
}
@Override
@ -82,8 +82,7 @@ public class UrlResourceCache implements ResourceCache {
Log.info("GET -> " + url);
try {
contents = http.get(url, hs -> {
List<String> contentLengths
= hs.getHeaders().get("Content-Length");
List<String> contentLengths = hs.getAll("Content-Length");
if (!maxFileSize.isPresent()) {
return true;
}

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.io.http.ning;
import com.ning.http.client.*;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ic.wlgitbridge.util.FunctionT;
@ -23,7 +24,7 @@ public class NingHttpClient implements NingHttpClientFacade {
@Override
public <E extends Exception> byte[] get(
String url,
FunctionT<HttpResponseHeaders, Boolean, E> handler
FunctionT<HttpHeaders, Boolean, E> handler
) throws ExecutionException {
try {
return http
@ -33,19 +34,19 @@ public class NingHttpClient implements NingHttpClientFacade {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@Override
public STATE onHeadersReceived(
HttpResponseHeaders headers
public State onHeadersReceived(
HttpHeaders headers
) throws E {
return handler.apply(headers)
? STATE.CONTINUE : STATE.ABORT;
? State.CONTINUE : State.ABORT;
}
@Override
public STATE onBodyPartReceived(
public State onBodyPartReceived(
HttpResponseBodyPart content
) throws IOException {
bytes.write(content.getBodyPartBytes());
return STATE.CONTINUE;
return State.CONTINUE;
}
@Override

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.io.http.ning;
import com.ning.http.client.HttpResponseHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import uk.ac.ic.wlgitbridge.util.FunctionT;
import java.util.concurrent.ExecutionException;
@ -16,7 +16,7 @@ public interface NingHttpClientFacade {
*/
<E extends Exception> byte[] get(
String url,
FunctionT<HttpResponseHeaders, Boolean, E> handler
FunctionT<HttpHeaders, Boolean, E> handler
) throws ExecutionException;
}

View file

@ -1,46 +0,0 @@
package uk.ac.ic.wlgitbridge.io.http.ning;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.HttpResponseHeaders;
import java.util.*;
public class NingHttpHeaders extends HttpResponseHeaders {
private final FluentCaseInsensitiveStringsMap map;
private NingHttpHeaders(FluentCaseInsensitiveStringsMap map) {
this.map = map;
}
public static NingHttpHeadersBuilder builder() {
return new NingHttpHeadersBuilder();
}
@Override
public FluentCaseInsensitiveStringsMap getHeaders() {
return map;
}
public static class NingHttpHeadersBuilder {
private final Map<String, Collection<String>> map;
private NingHttpHeadersBuilder() {
map = new HashMap<>();
}
public NingHttpHeadersBuilder addHeader(String key, String... values) {
map.computeIfAbsent(key, __ -> new ArrayList<>())
.addAll(Arrays.asList(values));
return this;
}
public NingHttpHeaders build() {
return new NingHttpHeaders(
new FluentCaseInsensitiveStringsMap(map));
}
}
}

View file

@ -3,7 +3,8 @@ package uk.ac.ic.wlgitbridge.snapshot.base;
import com.google.api.client.http.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ning.http.client.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClient;
import static org.asynchttpclient.Dsl.*;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.util.Instance;
import uk.ac.ic.wlgitbridge.util.Log;
@ -17,7 +18,7 @@ import java.util.concurrent.*;
*/
public abstract class Request<T extends Result> {
public static final AsyncHttpClient httpClient = new AsyncHttpClient();
public static final AsyncHttpClient httpClient = asyncHttpClient();
private static final Executor executor = Executors.newCachedThreadPool();

View file

@ -2,7 +2,7 @@ package uk.ac.ic.wlgitbridge.snapshot.servermock.server;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ning.http.client.AsyncHttpClient;
import static org.asynchttpclient.Dsl.*;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.IOException;
@ -30,17 +30,9 @@ public class PostbackThread extends Thread {
@Override
public void run() {
try {
new AsyncHttpClient().preparePost(
asyncHttpClient().preparePost(
url
).setBody(postback).execute().get().getResponseBody();
} catch (IOException e) {
Log.warn(
"IOException on postback, url: " +
url +
", postback: " +
postback,
e
);
} catch (InterruptedException e) {
Log.warn(
"Interrupted on postback, url: " +

View file

@ -1,9 +1,9 @@
package uk.ac.ic.wlgitbridge.application;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import static org.asynchttpclient.Dsl.*;
import org.asynchttpclient.*;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.Before;
import org.junit.Rule;
@ -613,22 +613,22 @@ public class WLGitBridgeIntegrationTest {
// With no key, we should get a 404.
String url = "http://127.0.0.1:" + gitBridgePort + "/api/testproj/push.tex";
Response response = new AsyncHttpClient().prepareGet(url).execute().get();
Response response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(404, response.getStatusCode());
// With an invalid project and no key, we should get a 404.
url = "http://127.0.0.1:" + gitBridgePort + "/api/notavalidproject/push.tex";
response = new AsyncHttpClient().prepareGet(url).execute().get();
response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(404, response.getStatusCode());
// With a bad key for a valid project, we should get a 404.
url = "http://127.0.0.1:" + gitBridgePort + "/api/testproj/push.tex?key=notavalidkey";
response = new AsyncHttpClient().prepareGet(url).execute().get();
response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(404, response.getStatusCode());
// With a bad key for an invalid project, we should get a 404.
url = "http://127.0.0.1:" + gitBridgePort + "/api/notavalidproject/push.tex?key=notavalidkey";
response = new AsyncHttpClient().prepareGet(url).execute().get();
response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(404, response.getStatusCode());
wlgb.stop();
@ -719,14 +719,14 @@ public class WLGitBridgeIntegrationTest {
// With an invalid project and no key, we should get a 404,
// which is rendered by our custom error handler.
String url = "http://127.0.0.1:" + gitBridgePort + "/api/notavalidproject/main.tex";
Response response = new AsyncHttpClient().prepareGet(url).execute().get();
Response response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(404, response.getStatusCode());
assertEquals("{\"message\":\"HTTP error 404\"}", response.getResponseBody());
// With an unsupported URL outside the api, we should get a 500,
// which is rendered by our custom error handler.
url = "http://127.0.0.1:" + gitBridgePort + "/foo";
response = new AsyncHttpClient().prepareGet(url).execute().get();
response = asyncHttpClient().prepareGet(url).execute().get();
assertEquals(500, response.getStatusCode());
assertEquals("{\"message\":\"HTTP error 500\"}", response.getResponseBody());

View file

@ -1,12 +1,12 @@
package uk.ac.ic.wlgitbridge.bridge.resource;
import com.ning.http.client.HttpResponseHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.bridge.db.noop.NoopDbStore;
import uk.ac.ic.wlgitbridge.bridge.util.CastUtil;
import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClientFacade;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpHeaders;
import uk.ac.ic.wlgitbridge.util.FunctionT;
import java.io.IOException;
@ -31,11 +31,8 @@ public class UrlResourceCacheTest {
private final UrlResourceCache cache
= new UrlResourceCache(new NoopDbStore(), http);
private static HttpResponseHeaders withContentLength(long cl) {
return NingHttpHeaders
.builder()
.addHeader("Content-Length", String.valueOf(cl))
.build();
private static HttpHeaders withContentLength(long cl) {
return new DefaultHttpHeaders().add("Content-Length", String.valueOf(cl));
}
private void respondWithContentLength(long cl, long actual)
@ -44,7 +41,7 @@ public class UrlResourceCacheTest {
Object[] args = invoc.getArguments();
//noinspection unchecked
((FunctionT<
HttpResponseHeaders, Boolean, SizeLimitExceededException
HttpHeaders, Boolean, SizeLimitExceededException
>) args[1]).apply(withContentLength(cl));
return new byte[CastUtil.assumeInt(actual)];
});