mirror of
https://github.com/overleaf/overleaf.git
synced 2025-01-30 19:00:48 +00:00
Decouple http client from UrlResourceCache
This commit is contained in:
parent
ae65212f19
commit
4e7636c94a
3 changed files with 28 additions and 78 deletions
|
@ -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<byte[]>() {
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public STATE onHeadersReceived(
|
||||
HttpResponseHeaders headers
|
||||
) throws SizeLimitExceededException {
|
||||
List<String> 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<String> 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) {
|
||||
|
|
|
@ -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 <E extends Exception> byte[] get(
|
||||
String url,
|
||||
FunctionT<HttpResponseHeaders, Boolean, E> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
|||
<E extends Exception> byte[] get(
|
||||
String url,
|
||||
FunctionT<HttpResponseHeaders, Boolean, E> handler
|
||||
) throws E;
|
||||
) throws ExecutionException;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue