From 4e7636c94ad7ebc4a71cecbdd1ff71c1a735209b Mon Sep 17 00:00:00 2001 From: Winston Li Date: Mon, 7 Aug 2017 23:42:31 +0100 Subject: [PATCH] Decouple http client from UrlResourceCache --- .../bridge/resource/UrlResourceCache.java | 92 ++++++------------- .../io/http/ning/NingHttpClient.java | 11 +-- .../io/http/ning/NingHttpClientFacade.java | 3 +- 3 files changed, 28 insertions(+), 78 deletions(-) diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/resource/UrlResourceCache.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/resource/UrlResourceCache.java index bde1d1f4b9..9a2c72a548 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/resource/UrlResourceCache.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/resource/UrlResourceCache.java @@ -1,15 +1,15 @@ package uk.ac.ic.wlgitbridge.bridge.resource; -import com.ning.http.client.*; +import com.ning.http.client.AsyncHttpClient; import uk.ac.ic.wlgitbridge.bridge.db.DBStore; import uk.ac.ic.wlgitbridge.data.filestore.RawFile; import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile; import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException; -import uk.ac.ic.wlgitbridge.snapshot.base.Request; +import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClient; +import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClientFacade; import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.util.Log; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; @@ -23,8 +23,15 @@ public class UrlResourceCache implements ResourceCache { private final DBStore dbStore; - public UrlResourceCache(DBStore dbStore) { + private final NingHttpClientFacade http; + + UrlResourceCache(DBStore dbStore, NingHttpClientFacade http) { this.dbStore = dbStore; + this.http = http; + } + + public UrlResourceCache(DBStore dbStore) { + this(dbStore, new NingHttpClient(new AsyncHttpClient())); } @Override @@ -74,71 +81,24 @@ public class UrlResourceCache implements ResourceCache { byte[] contents; Log.info("GET -> " + url); try { - contents = Request.httpClient.prepareGet(url).execute( - new AsyncCompletionHandler() { - - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - - @Override - public STATE onHeadersReceived( - HttpResponseHeaders headers - ) throws SizeLimitExceededException { - List contentLengths - = headers.getHeaders().get("Content-Length"); - if (!maxFileSize.isPresent()) { - return STATE.CONTINUE; - } - if (contentLengths.isEmpty()) { - return STATE.CONTINUE; - } - long contentLength = Long.parseLong(contentLengths.get(0)); - long maxFileSize_ = maxFileSize.get(); - if (contentLength <= maxFileSize_) { - return STATE.CONTINUE; - } - throw new SizeLimitExceededException( - Optional.of(path), contentLength, maxFileSize_ - ); + contents = http.get(url, hs -> { + List contentLengths + = hs.getHeaders().get("Content-Length"); + if (!maxFileSize.isPresent()) { + return true; } - - @Override - public STATE onBodyPartReceived( - HttpResponseBodyPart bodyPart - ) throws Exception { - bytes.write(bodyPart.getBodyPartBytes()); - return STATE.CONTINUE; + if (contentLengths.isEmpty()) { + return true; } - - @Override - public byte[] onCompleted( - Response response - ) throws Exception { - byte[] data = bytes.toByteArray(); - bytes.close(); - Log.info( - response.getStatusCode() - + " " - + response.getStatusText() - + " (" - + data.length - + "B) -> " - + url - ); - return data; + long contentLength = Long.parseLong(contentLengths.get(0)); + long maxFileSize_ = maxFileSize.get(); + if (contentLength <= maxFileSize_) { + return true; } - - }).get(); - } catch (InterruptedException e) { - Log.warn( - "Interrupted when fetching project: " + - projectName + - ", url: " + - url + - ", path: " + - path, - e - ); - throw new FailedConnectionException(); + throw new SizeLimitExceededException( + Optional.of(path), contentLength, maxFileSize_ + ); + }); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof SizeLimitExceededException) { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClient.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClient.java index 2f5c579da8..b9f3d215ff 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClient.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClient.java @@ -8,7 +8,6 @@ import uk.ac.ic.wlgitbridge.util.FunctionT; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.concurrent.ExecutionException; -import java.util.function.Function; public class NingHttpClient implements NingHttpClientFacade { @@ -25,7 +24,7 @@ public class NingHttpClient implements NingHttpClientFacade { public byte[] get( String url, FunctionT handler - ) throws E { + ) throws ExecutionException { try { return http .prepareGet(url) @@ -70,14 +69,6 @@ public class NingHttpClient implements NingHttpClientFacade { }).get(); } catch (InterruptedException e) { throw new RuntimeException(e); - } catch (ExecutionException e) { - try { - /* No clean way to do this */ - //noinspection unchecked - throw (E) e.getCause(); - } catch (ClassCastException cce) { - throw new RuntimeException(cce); - } } } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClientFacade.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClientFacade.java index 2d37bff367..f0bf4c3770 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClientFacade.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClientFacade.java @@ -4,7 +4,6 @@ import com.ning.http.client.HttpResponseHeaders; import uk.ac.ic.wlgitbridge.util.FunctionT; import java.util.concurrent.ExecutionException; -import java.util.function.Function; public interface NingHttpClientFacade { @@ -18,6 +17,6 @@ public interface NingHttpClientFacade { byte[] get( String url, FunctionT handler - ) throws E; + ) throws ExecutionException; }