Refactored so that the correct exceptions are thrown, especially when there is an git repo.

This commit is contained in:
Winston Li 2014-11-08 12:54:06 +00:00
parent f245e583b4
commit b5513618d0
29 changed files with 190 additions and 137 deletions

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.bridge;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.File; import java.io.File;

View file

@ -1,12 +1,11 @@
package uk.ac.ic.wlgitbridge.bridge; package uk.ac.ic.wlgitbridge.bridge;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
import java.io.File; import java.io.File;
@ -30,7 +29,7 @@ public class WLBridgedProject {
this.snapshotDBAPI = snapshotDBAPI; this.snapshotDBAPI = snapshotDBAPI;
} }
public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException { public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException, FailedConnectionException {
if (repository.getObjectDatabase().exists()) { if (repository.getObjectDatabase().exists()) {
updateRepositoryFromSnapshots(repository); updateRepositoryFromSnapshots(repository);
} else { } else {
@ -53,7 +52,7 @@ public class WLBridgedProject {
} }
} }
private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException, ServiceNotEnabledException { private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException, ServiceNotEnabledException, FailedConnectionException {
if (!snapshotDBAPI.repositoryExists(name)) { if (!snapshotDBAPI.repositoryExists(name)) {
throw new RepositoryNotFoundException(name); throw new RepositoryNotFoundException(name);
} }

View file

@ -1,6 +1,8 @@
package uk.ac.ic.wlgitbridge.writelatex; package uk.ac.ic.wlgitbridge.writelatex;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
@ -25,13 +27,13 @@ public class SnapshotFetcher {
versions = new TreeSet<Integer>(); versions = new TreeSet<Integer>();
} }
public List<Snapshot> fetchNewSnapshots() throws Throwable { public List<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
List<Snapshot> newSnapshots = new LinkedList<Snapshot>(); List<Snapshot> newSnapshots = new LinkedList<Snapshot>();
while (getNew(newSnapshots)); while (getNew(newSnapshots));
return newSnapshots; return newSnapshots;
} }
private boolean getNew(List<Snapshot> newSnapshots) throws Throwable { private boolean getNew(List<Snapshot> newSnapshots) throws FailedConnectionException, InvalidProjectException {
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName); SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName);
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName); SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);
@ -59,13 +61,13 @@ public class SnapshotFetcher {
ids.add(versionID); ids.add(versionID);
} }
private int putLatestDoc(SnapshotGetDocRequest getDoc, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable { private int putLatestDoc(SnapshotGetDocRequest getDoc, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws FailedConnectionException, InvalidProjectException {
int latestVersionID = getDoc.getResult().getVersionID(); int latestVersionID = getDoc.getResult().getVersionID();
putFetchedResult(new SnapshotInfo(latestVersionID), fetchedIDs, fetchedSnapshotInfos); putFetchedResult(new SnapshotInfo(latestVersionID), fetchedIDs, fetchedSnapshotInfos);
return latestVersionID; return latestVersionID;
} }
private void putSavedVers(SnapshotGetSavedVersRequest getSavedVers, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable { private void putSavedVers(SnapshotGetSavedVersRequest getSavedVers, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws FailedConnectionException {
for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) { for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
putFetchedResult(snapshotInfo, fetchedIDs, fetchedSnapshotInfos); putFetchedResult(snapshotInfo, fetchedIDs, fetchedSnapshotInfos);
} }
@ -81,7 +83,7 @@ public class SnapshotFetcher {
return idsToUpdate; return idsToUpdate;
} }
private boolean updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws Throwable { private boolean updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws FailedConnectionException {
if (idsToUpdate.isEmpty()) { if (idsToUpdate.isEmpty()) {
return false; return false;
} }
@ -90,7 +92,7 @@ public class SnapshotFetcher {
return true; return true;
} }
private void fetchVersions(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws Throwable { private void fetchVersions(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws FailedConnectionException {
List<SnapshotGetForVersionRequest> requests = createFiredRequests(idsToUpdate); List<SnapshotGetForVersionRequest> requests = createFiredRequests(idsToUpdate);
processResults(fetchedSnapshotInfos, newSnapshots, requests); processResults(fetchedSnapshotInfos, newSnapshots, requests);
} }
@ -105,13 +107,13 @@ public class SnapshotFetcher {
return requests; return requests;
} }
private void processResults(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, List<SnapshotGetForVersionRequest> requests) throws Throwable { private void processResults(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, List<SnapshotGetForVersionRequest> requests) throws FailedConnectionException {
for (SnapshotGetForVersionRequest request : requests) { for (SnapshotGetForVersionRequest request : requests) {
processResult(fetchedSnapshotInfos, newSnapshots, request); processResult(fetchedSnapshotInfos, newSnapshots, request);
} }
} }
private void processResult(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, SnapshotGetForVersionRequest request) throws Throwable { private void processResult(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, SnapshotGetForVersionRequest request) throws FailedConnectionException {
SnapshotGetForVersionResult result = request.getResult(); SnapshotGetForVersionResult result = request.getResult();
SnapshotData data = result.getSnapshotData(); SnapshotData data = result.getSnapshotData();
Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data); Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data);

View file

@ -7,6 +7,7 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.bridge.RepositorySource; import uk.ac.ic.wlgitbridge.bridge.RepositorySource;
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject; import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -30,9 +31,13 @@ public class SnapshotRepositoryBuilder implements RepositorySource {
try { try {
repository = new FileRepositoryBuilder().setWorkTree(repositoryDirectory).build(); repository = new FileRepositoryBuilder().setWorkTree(repositoryDirectory).build();
} catch (IOException e) { } catch (IOException e) {
throw new RepositoryNotFoundException(name); e.printStackTrace();
}
try {
new WLBridgedProject(repository, name, repositoryDirectory, snapshotDBAPI).buildRepository();
} catch (FailedConnectionException e) {
e.printStackTrace();
} }
new WLBridgedProject(repository, name, repositoryDirectory, snapshotDBAPI).buildRepository();
return repository; return repository;
} }

View file

@ -1,15 +0,0 @@
package uk.ac.ic.wlgitbridge.writelatex.api;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
import java.util.List;
/**
* Created by Winston on 03/11/14.
*/
public interface SnapshotAPI {
public List<Snapshot> getSnapshots();
public void putSnapshots(List<Snapshot> snapshots);
}

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api; package uk.ac.ic.wlgitbridge.writelatex.api;
import org.eclipse.jgit.lib.Repository; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
import java.util.List; import java.util.List;
@ -10,7 +11,7 @@ import java.util.List;
*/ */
public interface SnapshotDBAPI { public interface SnapshotDBAPI {
public boolean repositoryExists(String name); public boolean repositoryExists(String name) throws FailedConnectionException;
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable; public List<Snapshot> getSnapshotsToAddToProject(String name) throws FailedConnectionException, InvalidProjectException;
} }

View file

@ -1,14 +1,13 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.base; package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.IOException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
*/ */
public interface JSONSource { public interface JSONSource {
public abstract void fromJSON(JsonElement json); public abstract void fromJSON(JsonElement json) throws FailedConnectionException;
} }

View file

@ -6,8 +6,10 @@ import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Realm; import com.ning.http.client.Realm;
import com.ning.http.client.Response; import com.ning.http.client.Response;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
/** /**
@ -18,17 +20,15 @@ public abstract class Request<T extends Result> {
private final String url; private final String url;
private Future<T> future; private Future<T> future;
private Throwable throwable; private boolean error;
private boolean finished;
public Request(String url) { public Request(String url) {
this.url = url; this.url = url;
finished = false; error = false;
} }
protected abstract Realm buildRequestRealm(); protected abstract Realm buildRequestRealm();
protected abstract T parseResponse(JsonElement json); protected abstract T parseResponse(JsonElement json) throws FailedConnectionException;
public void request() { public void request() {
AsyncHttpClient client = new AsyncHttpClient(); AsyncHttpClient client = new AsyncHttpClient();
@ -42,20 +42,26 @@ public abstract class Request<T extends Result> {
@Override @Override
public void onThrowable(Throwable t) { public void onThrowable(Throwable t) {
throwable = t; error = true;
} }
}); });
} catch (IOException e) { } catch (IOException e) {
throwable = e; error = true;
} }
} }
public T getResult() throws Throwable { public T getResult() throws FailedConnectionException {
if (throwable != null) { if (error) {
throw throwable; throw new FailedConnectionException();
}
try {
return future.get();
} catch (InterruptedException e) {
throw new FailedConnectionException();
} catch (ExecutionException e) {
throw new FailedConnectionException();
} }
return future.get();
} }
} }

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.base; package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -10,7 +11,7 @@ public abstract class Result implements JSONSource {
private JsonElement json; private JsonElement json;
private final Request request; private final Request request;
public Result(Request request, JsonElement json) { public Result(Request request, JsonElement json) throws FailedConnectionException {
this.request = request; this.request = request;
this.json = json; this.json = json;
fromJSON(json); fromJSON(json);

View file

@ -0,0 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.exception;
/**
* Created by Winston on 08/11/14.
*/
public class FailedConnectionException extends Exception {
}

View file

@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -15,7 +16,7 @@ public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResu
} }
@Override @Override
protected SnapshotGetDocResult parseResponse(JsonElement json) { protected SnapshotGetDocResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetDocResult(this, json); return new SnapshotGetDocResult(this, json);
} }

View file

@ -1,8 +1,11 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc; package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -11,16 +14,26 @@ public class SnapshotGetDocResult extends Result {
private int versionID; private int versionID;
public SnapshotGetDocResult(Request request, JsonElement json) { private InvalidProjectException invalidProjectException;
public SnapshotGetDocResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json); super(request, json);
} }
@Override @Override
public void fromJSON(JsonElement json) { public void fromJSON(JsonElement json) {
versionID = json.getAsJsonObject().get("latestVerId").getAsInt(); JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("status") && jsonObject.get("status").getAsInt() == 404) {
invalidProjectException = new InvalidProjectException();
} else {
versionID = json.getAsJsonObject().get("latestVerId").getAsInt();
}
} }
public int getVersionID() { public int getVersionID() throws InvalidProjectException {
if (invalidProjectException != null) {
throw invalidProjectException;
}
return versionID; return versionID;
} }

View file

@ -0,0 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception;
/**
* Created by Winston on 08/11/14.
*/
public class InvalidProjectException extends Exception {
}

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
@ -20,39 +21,30 @@ public class SnapshotData implements JSONSource {
private List<WLFile> srcs; private List<WLFile> srcs;
private List<WLFile> atts; private List<WLFile> atts;
public SnapshotData(JsonElement json) { public SnapshotData(JsonElement json) throws FailedConnectionException {
srcs = new LinkedList<WLFile>(); srcs = new LinkedList<WLFile>();
atts = new LinkedList<WLFile>(); atts = new LinkedList<WLFile>();
fromJSON(json); fromJSON(json);
} }
@Override @Override
public void fromJSON(JsonElement json) { public void fromJSON(JsonElement json) throws FailedConnectionException {
populateSrcs(json.getAsJsonObject().get(JSON_KEY_SRCS).getAsJsonArray()); populateSrcs(json.getAsJsonObject().get(JSON_KEY_SRCS).getAsJsonArray());
populateAtts(json.getAsJsonObject().get(JSON_KEY_ATTS).getAsJsonArray()); populateAtts(json.getAsJsonObject().get(JSON_KEY_ATTS).getAsJsonArray());
} }
private void populateSrcs(JsonArray jsonArray) { private void populateSrcs(JsonArray jsonArray) throws FailedConnectionException {
for (JsonElement json : jsonArray) { for (JsonElement json : jsonArray) {
srcs.add(new WLFile(json)); srcs.add(new WLFile(json));
} }
} }
private void populateAtts(JsonArray jsonArray) { private void populateAtts(JsonArray jsonArray) throws FailedConnectionException {
for (JsonElement json : jsonArray) { for (JsonElement json : jsonArray) {
atts.add(new WLAttachment(json)); atts.add(new WLAttachment(json));
} }
} }
public void writeAll(String repoDir) throws InterruptedException, ExecutionException, IOException {
for (WLFile src : srcs) {
src.writeToDisk(repoDir);
}
for (WLFile att : atts) {
att.writeToDisk(repoDir);
}
}
public List<WLFile> getSrcs() { public List<WLFile> getSrcs() {
return srcs; return srcs;
} }

View file

@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -18,7 +19,7 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
} }
@Override @Override
protected SnapshotGetForVersionResult parseResponse(JsonElement json) { protected SnapshotGetForVersionResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetForVersionResult(this, json); return new SnapshotGetForVersionResult(this, json);
} }

View file

@ -1,13 +1,9 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion; package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -16,12 +12,12 @@ public class SnapshotGetForVersionResult extends Result {
private SnapshotData snapshotData; private SnapshotData snapshotData;
public SnapshotGetForVersionResult(Request request, JsonElement json) { public SnapshotGetForVersionResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json); super(request, json);
} }
@Override @Override
public void fromJSON(JsonElement json) { public void fromJSON(JsonElement json) throws FailedConnectionException {
snapshotData = new SnapshotData(json); snapshotData = new SnapshotData(json);
} }

View file

@ -2,7 +2,11 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.ning.http.client.*; 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 uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -16,42 +20,48 @@ public class WLAttachment extends WLFile {
private Future<byte[]> future; private Future<byte[]> future;
public WLAttachment(JsonElement json) { public WLAttachment(JsonElement json) throws FailedConnectionException {
super(json); super(json);
} }
@Override @Override
public byte[] getContents() throws ExecutionException, InterruptedException { public byte[] getContents() throws FailedConnectionException {
return future.get();
}
@Override
protected void getContentsFromJSON(JsonArray jsonArray) {
try { try {
fetchContents(jsonArray.get(0).getAsString()); return future.get();
} catch (IOException e) { } catch (InterruptedException e) {
throw new RuntimeException(); throw new FailedConnectionException();
} catch (ExecutionException e) {
throw new FailedConnectionException();
} }
} }
private void fetchContents(String url) throws IOException { @Override
protected void getContentsFromJSON(JsonArray jsonArray) throws FailedConnectionException {
fetchContents(jsonArray.get(0).getAsString());
}
private void fetchContents(String url) throws FailedConnectionException {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
future = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() { try {
future = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@Override @Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
bytes.write(bodyPart.getBodyPartBytes()); bytes.write(bodyPart.getBodyPartBytes());
return STATE.CONTINUE; return STATE.CONTINUE;
} }
@Override @Override
public byte[] onCompleted(Response response) throws Exception { public byte[] onCompleted(Response response) throws Exception {
return bytes.toByteArray(); return bytes.toByteArray();
} }
}); });
} catch (IOException e) {
throw new FailedConnectionException();
}
} }
} }

View file

@ -2,10 +2,13 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.*; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
/** /**
@ -16,18 +19,18 @@ public class WLFile implements JSONSource {
protected byte[] contents; protected byte[] contents;
private String path; private String path;
public WLFile(JsonElement json) { public WLFile(JsonElement json) throws FailedConnectionException {
fromJSON(json); fromJSON(json);
} }
@Override @Override
public void fromJSON(JsonElement json) { public void fromJSON(JsonElement json) throws FailedConnectionException {
JsonArray jsonArray = json.getAsJsonArray(); JsonArray jsonArray = json.getAsJsonArray();
getContentsFromJSON(jsonArray); getContentsFromJSON(jsonArray);
getPathFromJSON(jsonArray); getPathFromJSON(jsonArray);
} }
public byte[] getContents() throws ExecutionException, InterruptedException { public byte[] getContents() throws FailedConnectionException {
return contents; return contents;
} }
@ -35,7 +38,7 @@ public class WLFile implements JSONSource {
return path; return path;
} }
protected void getContentsFromJSON(JsonArray jsonArray) { protected void getContentsFromJSON(JsonArray jsonArray) throws FailedConnectionException {
contents = jsonArray.get(0).getAsString().getBytes(); contents = jsonArray.get(0).getAsString().getBytes();
} }
@ -43,7 +46,7 @@ public class WLFile implements JSONSource {
path = jsonArray.get(1).getAsString(); path = jsonArray.get(1).getAsString();
} }
public void writeToDisk(String repoDir) throws IOException, ExecutionException, InterruptedException { public void writeToDisk(String repoDir) throws FailedConnectionException, IOException {
File file = new File(repoDir, path); File file = new File(repoDir, path);
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
file.createNewFile(); file.createNewFile();

View file

@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -15,7 +16,7 @@ public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetS
} }
@Override @Override
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) { protected SnapshotGetSavedVersResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetSavedVersResult(this, json); return new SnapshotGetSavedVersResult(this, json);
} }

View file

@ -1,10 +1,10 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers; package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -16,7 +16,7 @@ public class SnapshotGetSavedVersResult extends Result {
private List<SnapshotInfo> savedVers; private List<SnapshotInfo> savedVers;
public SnapshotGetSavedVersResult(Request request, JsonElement json) { public SnapshotGetSavedVersResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json); super(request, json);
} }

View file

@ -1,8 +1,5 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers; package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
*/ */

View file

@ -1,8 +1,5 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers; package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
*/ */

View file

@ -0,0 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.db;
/**
* Created by Winston on 08/11/14.
*/
public class WLFileNode {
}

View file

@ -0,0 +1,19 @@
package uk.ac.ic.wlgitbridge.writelatex.db;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Winston on 08/11/14.
*/
public class WLFileStore {
private final Map<String, WLFileNode> fileStore;
public WLFileStore() {
fileStore = new HashMap<String, WLFileNode>();
}
}

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.model; package uk.ac.ic.wlgitbridge.writelatex.model;
import com.google.gson.JsonElement; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.WLFile; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.WLFile;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo; import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
@ -34,7 +34,7 @@ public class Snapshot {
atts = data.getAtts(); atts = data.getAtts();
} }
public void writeToDisk(String basePath) throws InterruptedException, ExecutionException, IOException { public void writeToDisk(String basePath) throws IOException, FailedConnectionException {
for (WLFile file : srcs) { for (WLFile file : srcs) {
file.writeToDisk(basePath); file.writeToDisk(basePath);
} }

View file

@ -1,6 +1,10 @@
package uk.ac.ic.wlgitbridge.writelatex.model; package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.db.WLFileStore;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -12,25 +16,31 @@ import java.util.Map;
public class WLDataModel implements SnapshotDBAPI { public class WLDataModel implements SnapshotDBAPI {
private final Map<String, WLProject> projects; private final Map<String, WLProject> projects;
private final WLFileStore fileStore;
public WLDataModel() { public WLDataModel() {
projects = new HashMap<String, WLProject>(); projects = new HashMap<String, WLProject>();
fileStore = new WLFileStore();
} }
@Override @Override
public boolean repositoryExists(String name) { public boolean repositoryExists(String name) throws FailedConnectionException {
if (!projects.containsKey(name)) { SnapshotGetDocRequest snapshotGetDocRequest = new SnapshotGetDocRequest(name);
projects.put(name, new WLProject(name)); snapshotGetDocRequest.request();
try {
snapshotGetDocRequest.getResult().getVersionID();
} catch (InvalidProjectException e) {
return false;
} }
return projects.containsKey(name); return true;
} }
@Override @Override
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable { public List<Snapshot> getSnapshotsToAddToProject(String name) throws FailedConnectionException, InvalidProjectException {
return updateProjectWithName(name); return updateProjectWithName(name);
} }
private List<Snapshot> updateProjectWithName(String name) throws Throwable { private List<Snapshot> updateProjectWithName(String name) throws FailedConnectionException, InvalidProjectException {
if (!projects.containsKey(name)) { if (!projects.containsKey(name)) {
projects.put(name, new WLProject(name)); projects.put(name, new WLProject(name));
} }

View file

@ -1,14 +1,12 @@
package uk.ac.ic.wlgitbridge.writelatex.model; package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher; import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -25,7 +23,7 @@ public class WLProject {
snapshotFetcher = new SnapshotFetcher(name, snapshots); snapshotFetcher = new SnapshotFetcher(name, snapshots);
} }
public List<Snapshot> fetchNewSnapshots() throws Throwable { public List<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
return snapshotFetcher.fetchNewSnapshots(); return snapshotFetcher.fetchNewSnapshots();
} }

View file

@ -6,9 +6,6 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
*/ */

View file

@ -1,6 +1,8 @@
package uk.ac.ic.wlgitbridge.test.writelatex.model; package uk.ac.ic.wlgitbridge.test.writelatex.model;
import org.junit.Test; import org.junit.Test;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; import uk.ac.ic.wlgitbridge.writelatex.model.WLProject;
import java.io.IOException; import java.io.IOException;
@ -16,14 +18,10 @@ public class WLProjectTests {
WLProject project = new WLProject("1826rqgsdb"); WLProject project = new WLProject("1826rqgsdb");
try { try {
project.fetchNewSnapshots(); project.fetchNewSnapshots();
} catch (InterruptedException e) { } catch (FailedConnectionException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ExecutionException e) { } catch (InvalidProjectException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable throwable) {
throwable.printStackTrace();
} }
} }