Use shared instance of AsyncHttpClient and close Stream in ResourceFetcher.

This commit is contained in:
Winston Li 2015-02-24 10:30:39 +00:00
parent 77c4576b59
commit 3d53226853
2 changed files with 7 additions and 4 deletions

View file

@ -18,6 +18,8 @@ import java.util.concurrent.Future;
*/
public abstract class Request<T extends Result> {
public static final AsyncHttpClient httpClient = new AsyncHttpClient();
private final String url;
private Future<T> future;
@ -64,12 +66,12 @@ public abstract class Request<T extends Result> {
private void performGetRequest() {
Util.sout("GET -> " + url);
request(new AsyncHttpClient().prepareGet(url));
request(httpClient.prepareGet(url));
}
private void performPostRequest() {
Util.sout("POST -> " + url);
request(new AsyncHttpClient().preparePost(url).setBody(getPostBody()).setHeader("Content-Type", "application/json"));
request(httpClient.preparePost(url).setBody(getPostBody()).setHeader("Content-Type", "application/json"));
}
private void request(BoundRequestBuilder boundRequestBuilder) {

View file

@ -1,13 +1,13 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.Response;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.filestore.RepositoryFile;
@ -44,7 +44,7 @@ public class ResourceFetcher {
byte[] contents;
Util.sout("GET -> " + url);
try {
contents = new AsyncHttpClient().prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
contents = Request.httpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@ -57,6 +57,7 @@ public class ResourceFetcher {
@Override
public byte[] onCompleted(Response response) throws Exception {
byte[] data = bytes.toByteArray();
bytes.close();
Util.sout(response.getStatusCode() + " " + response.getStatusText() + " (" + data.length + "B) -> " + url);
return data;
}