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 new file mode 100644 index 0000000000..2f5c579da8 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClient.java @@ -0,0 +1,84 @@ +package uk.ac.ic.wlgitbridge.io.http.ning; + +import com.ning.http.client.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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 { + + private static final Logger log + = LoggerFactory.getLogger(NingHttpClient.class); + + private final AsyncHttpClient http; + + public NingHttpClient(AsyncHttpClient http) { + this.http = http; + } + + @Override + public byte[] get( + String url, + FunctionT handler + ) throws E { + try { + return http + .prepareGet(url) + .execute(new AsyncCompletionHandler() { + + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + + @Override + public STATE onHeadersReceived( + HttpResponseHeaders headers + ) throws E { + return handler.apply(headers) + ? STATE.CONTINUE : STATE.ABORT; + } + + @Override + public STATE onBodyPartReceived( + HttpResponseBodyPart content + ) throws IOException { + bytes.write(content.getBodyPartBytes()); + return STATE.CONTINUE; + } + + @Override + public byte[] onCompleted( + Response response + ) throws IOException { + byte[] ret = bytes.toByteArray(); + bytes.close(); + log.info( + response.getStatusCode() + + " " + + response.getStatusText() + + " (" + + ret.length + + "B) -> " + + url + ); + return ret; + } + + }).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 new file mode 100644 index 0000000000..2d37bff367 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/io/http/ning/NingHttpClientFacade.java @@ -0,0 +1,23 @@ +package uk.ac.ic.wlgitbridge.io.http.ning; + +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 { + + /** + * Performs a GET request + * @param url the target URL + * @param handler handler for the response headers. Returning false + * aborts the request. + * @return + */ + byte[] get( + String url, + FunctionT handler + ) throws E; + +}