mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Refactored so that the correct exceptions are thrown, especially when there is an git repo.
This commit is contained in:
parent
f245e583b4
commit
b5513618d0
29 changed files with 190 additions and 137 deletions
|
@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.bridge;
|
|||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
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.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
||||
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 java.io.File;
|
||||
|
@ -30,7 +29,7 @@ public class WLBridgedProject {
|
|||
this.snapshotDBAPI = snapshotDBAPI;
|
||||
}
|
||||
|
||||
public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException {
|
||||
public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException, FailedConnectionException {
|
||||
if (repository.getObjectDatabase().exists()) {
|
||||
updateRepositoryFromSnapshots(repository);
|
||||
} 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)) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
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.exception.InvalidProjectException;
|
||||
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.SnapshotGetForVersionResult;
|
||||
|
@ -25,13 +27,13 @@ public class SnapshotFetcher {
|
|||
versions = new TreeSet<Integer>();
|
||||
}
|
||||
|
||||
public List<Snapshot> fetchNewSnapshots() throws Throwable {
|
||||
public List<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
|
||||
List<Snapshot> newSnapshots = new LinkedList<Snapshot>();
|
||||
while (getNew(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);
|
||||
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);
|
||||
|
||||
|
@ -59,13 +61,13 @@ public class SnapshotFetcher {
|
|||
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();
|
||||
putFetchedResult(new SnapshotInfo(latestVersionID), fetchedIDs, fetchedSnapshotInfos);
|
||||
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()) {
|
||||
putFetchedResult(snapshotInfo, fetchedIDs, fetchedSnapshotInfos);
|
||||
}
|
||||
|
@ -81,7 +83,7 @@ public class SnapshotFetcher {
|
|||
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()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -90,7 +92,7 @@ public class SnapshotFetcher {
|
|||
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);
|
||||
processResults(fetchedSnapshotInfos, newSnapshots, requests);
|
||||
}
|
||||
|
@ -105,13 +107,13 @@ public class SnapshotFetcher {
|
|||
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) {
|
||||
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();
|
||||
SnapshotData data = result.getSnapshotData();
|
||||
Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data);
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
|||
import uk.ac.ic.wlgitbridge.bridge.RepositorySource;
|
||||
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
|
||||
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.IOException;
|
||||
|
@ -30,9 +31,13 @@ public class SnapshotRepositoryBuilder implements RepositorySource {
|
|||
try {
|
||||
repository = new FileRepositoryBuilder().setWorkTree(repositoryDirectory).build();
|
||||
} catch (IOException e) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
new WLBridgedProject(repository, name, repositoryDirectory, snapshotDBAPI).buildRepository();
|
||||
} catch (FailedConnectionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return repository;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
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 java.util.List;
|
||||
|
@ -10,7 +11,7 @@ import java.util.List;
|
|||
*/
|
||||
public interface SnapshotDBAPI {
|
||||
|
||||
public boolean repositoryExists(String name);
|
||||
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable;
|
||||
public boolean repositoryExists(String name) throws FailedConnectionException;
|
||||
public List<Snapshot> getSnapshotsToAddToProject(String name) throws FailedConnectionException, InvalidProjectException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
import java.io.IOException;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
public interface JSONSource {
|
||||
|
||||
public abstract void fromJSON(JsonElement json);
|
||||
public abstract void fromJSON(JsonElement json) throws FailedConnectionException;
|
||||
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@ import com.ning.http.client.AsyncCompletionHandler;
|
|||
import com.ning.http.client.AsyncHttpClient;
|
||||
import com.ning.http.client.Realm;
|
||||
import com.ning.http.client.Response;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
|
@ -18,17 +20,15 @@ public abstract class Request<T extends Result> {
|
|||
private final String url;
|
||||
|
||||
private Future<T> future;
|
||||
private Throwable throwable;
|
||||
|
||||
private boolean finished;
|
||||
private boolean error;
|
||||
|
||||
public Request(String url) {
|
||||
this.url = url;
|
||||
finished = false;
|
||||
error = false;
|
||||
}
|
||||
|
||||
protected abstract Realm buildRequestRealm();
|
||||
protected abstract T parseResponse(JsonElement json);
|
||||
protected abstract T parseResponse(JsonElement json) throws FailedConnectionException;
|
||||
|
||||
public void request() {
|
||||
AsyncHttpClient client = new AsyncHttpClient();
|
||||
|
@ -42,20 +42,26 @@ public abstract class Request<T extends Result> {
|
|||
|
||||
@Override
|
||||
public void onThrowable(Throwable t) {
|
||||
throwable = t;
|
||||
error = true;
|
||||
}
|
||||
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throwable = e;
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
|
||||
public T getResult() throws Throwable {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
public T getResult() throws FailedConnectionException {
|
||||
if (error) {
|
||||
throw new FailedConnectionException();
|
||||
}
|
||||
try {
|
||||
return future.get();
|
||||
} catch (InterruptedException e) {
|
||||
throw new FailedConnectionException();
|
||||
} catch (ExecutionException e) {
|
||||
throw new FailedConnectionException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
|
@ -10,7 +11,7 @@ public abstract class Result implements JSONSource {
|
|||
private JsonElement json;
|
||||
private final Request request;
|
||||
|
||||
public Result(Request request, JsonElement json) {
|
||||
public Result(Request request, JsonElement json) throws FailedConnectionException {
|
||||
this.request = request;
|
||||
this.json = json;
|
||||
fromJSON(json);
|
||||
|
|
|
@ -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 {
|
||||
}
|
|
@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
|
|||
|
||||
import com.google.gson.JsonElement;
|
||||
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.
|
||||
|
@ -15,7 +16,7 @@ public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResu
|
|||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetDocResult parseResponse(JsonElement json) {
|
||||
protected SnapshotGetDocResult parseResponse(JsonElement json) throws FailedConnectionException {
|
||||
return new SnapshotGetDocResult(this, json);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
|
||||
|
||||
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.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.
|
||||
|
@ -11,16 +14,26 @@ public class SnapshotGetDocResult extends Result {
|
|||
|
||||
private int versionID;
|
||||
|
||||
public SnapshotGetDocResult(Request request, JsonElement json) {
|
||||
private InvalidProjectException invalidProjectException;
|
||||
|
||||
public SnapshotGetDocResult(Request request, JsonElement json) throws FailedConnectionException {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
}
|
|
@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
|||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
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.util.LinkedList;
|
||||
|
@ -20,39 +21,30 @@ public class SnapshotData implements JSONSource {
|
|||
private List<WLFile> srcs;
|
||||
private List<WLFile> atts;
|
||||
|
||||
public SnapshotData(JsonElement json) {
|
||||
public SnapshotData(JsonElement json) throws FailedConnectionException {
|
||||
srcs = new LinkedList<WLFile>();
|
||||
atts = new LinkedList<WLFile>();
|
||||
fromJSON(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
public void fromJSON(JsonElement json) throws FailedConnectionException {
|
||||
populateSrcs(json.getAsJsonObject().get(JSON_KEY_SRCS).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) {
|
||||
srcs.add(new WLFile(json));
|
||||
}
|
||||
}
|
||||
|
||||
private void populateAtts(JsonArray jsonArray) {
|
||||
private void populateAtts(JsonArray jsonArray) throws FailedConnectionException {
|
||||
for (JsonElement json : jsonArray) {
|
||||
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() {
|
||||
return srcs;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
|||
|
||||
import com.google.gson.JsonElement;
|
||||
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.
|
||||
|
@ -18,7 +19,7 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
|
|||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetForVersionResult parseResponse(JsonElement json) {
|
||||
protected SnapshotGetForVersionResult parseResponse(JsonElement json) throws FailedConnectionException {
|
||||
return new SnapshotGetForVersionResult(this, json);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
|
@ -16,12 +12,12 @@ public class SnapshotGetForVersionResult extends Result {
|
|||
|
||||
private SnapshotData snapshotData;
|
||||
|
||||
public SnapshotGetForVersionResult(Request request, JsonElement json) {
|
||||
public SnapshotGetForVersionResult(Request request, JsonElement json) throws FailedConnectionException {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
public void fromJSON(JsonElement json) throws FailedConnectionException {
|
||||
snapshotData = new SnapshotData(json);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,11 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
|||
|
||||
import com.google.gson.JsonArray;
|
||||
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.IOException;
|
||||
|
@ -16,26 +20,29 @@ public class WLAttachment extends WLFile {
|
|||
|
||||
private Future<byte[]> future;
|
||||
|
||||
public WLAttachment(JsonElement json) {
|
||||
public WLAttachment(JsonElement json) throws FailedConnectionException {
|
||||
super(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getContents() throws ExecutionException, InterruptedException {
|
||||
public byte[] getContents() throws FailedConnectionException {
|
||||
try {
|
||||
return future.get();
|
||||
} catch (InterruptedException e) {
|
||||
throw new FailedConnectionException();
|
||||
} catch (ExecutionException e) {
|
||||
throw new FailedConnectionException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void getContentsFromJSON(JsonArray jsonArray) {
|
||||
try {
|
||||
protected void getContentsFromJSON(JsonArray jsonArray) throws FailedConnectionException {
|
||||
fetchContents(jsonArray.get(0).getAsString());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchContents(String url) throws IOException {
|
||||
private void fetchContents(String url) throws FailedConnectionException {
|
||||
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
|
||||
try {
|
||||
future = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
@ -52,6 +59,9 @@ public class WLAttachment extends WLFile {
|
|||
}
|
||||
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw new FailedConnectionException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,10 +2,13 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
|
|||
|
||||
import com.google.gson.JsonArray;
|
||||
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.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;
|
||||
|
||||
/**
|
||||
|
@ -16,18 +19,18 @@ public class WLFile implements JSONSource {
|
|||
protected byte[] contents;
|
||||
private String path;
|
||||
|
||||
public WLFile(JsonElement json) {
|
||||
public WLFile(JsonElement json) throws FailedConnectionException {
|
||||
fromJSON(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
public void fromJSON(JsonElement json) throws FailedConnectionException {
|
||||
JsonArray jsonArray = json.getAsJsonArray();
|
||||
getContentsFromJSON(jsonArray);
|
||||
getPathFromJSON(jsonArray);
|
||||
}
|
||||
|
||||
public byte[] getContents() throws ExecutionException, InterruptedException {
|
||||
public byte[] getContents() throws FailedConnectionException {
|
||||
return contents;
|
||||
}
|
||||
|
||||
|
@ -35,7 +38,7 @@ public class WLFile implements JSONSource {
|
|||
return path;
|
||||
}
|
||||
|
||||
protected void getContentsFromJSON(JsonArray jsonArray) {
|
||||
protected void getContentsFromJSON(JsonArray jsonArray) throws FailedConnectionException {
|
||||
contents = jsonArray.get(0).getAsString().getBytes();
|
||||
}
|
||||
|
||||
|
@ -43,7 +46,7 @@ public class WLFile implements JSONSource {
|
|||
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.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
|
|
|
@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
|||
|
||||
import com.google.gson.JsonElement;
|
||||
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.
|
||||
|
@ -15,7 +16,7 @@ public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetS
|
|||
}
|
||||
|
||||
@Override
|
||||
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) {
|
||||
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) throws FailedConnectionException {
|
||||
return new SnapshotGetSavedVersResult(this, json);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
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.exception.FailedConnectionException;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
@ -16,7 +16,7 @@ public class SnapshotGetSavedVersResult extends Result {
|
|||
|
||||
private List<SnapshotInfo> savedVers;
|
||||
|
||||
public SnapshotGetSavedVersResult(Request request, JsonElement json) {
|
||||
public SnapshotGetSavedVersResult(Request request, JsonElement json) throws FailedConnectionException {
|
||||
super(request, json);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
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.
|
||||
*/
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
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.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.db;
|
||||
|
||||
/**
|
||||
* Created by Winston on 08/11/14.
|
||||
*/
|
||||
public class WLFileNode {
|
||||
}
|
|
@ -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>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
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.WLFile;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
|
||||
|
@ -34,7 +34,7 @@ public class Snapshot {
|
|||
atts = data.getAtts();
|
||||
}
|
||||
|
||||
public void writeToDisk(String basePath) throws InterruptedException, ExecutionException, IOException {
|
||||
public void writeToDisk(String basePath) throws IOException, FailedConnectionException {
|
||||
for (WLFile file : srcs) {
|
||||
file.writeToDisk(basePath);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.model;
|
||||
|
||||
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.List;
|
||||
|
@ -12,25 +16,31 @@ import java.util.Map;
|
|||
public class WLDataModel implements SnapshotDBAPI {
|
||||
|
||||
private final Map<String, WLProject> projects;
|
||||
private final WLFileStore fileStore;
|
||||
|
||||
public WLDataModel() {
|
||||
projects = new HashMap<String, WLProject>();
|
||||
fileStore = new WLFileStore();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean repositoryExists(String name) {
|
||||
if (!projects.containsKey(name)) {
|
||||
projects.put(name, new WLProject(name));
|
||||
public boolean repositoryExists(String name) throws FailedConnectionException {
|
||||
SnapshotGetDocRequest snapshotGetDocRequest = new SnapshotGetDocRequest(name);
|
||||
snapshotGetDocRequest.request();
|
||||
try {
|
||||
snapshotGetDocRequest.getResult().getVersionID();
|
||||
} catch (InvalidProjectException e) {
|
||||
return false;
|
||||
}
|
||||
return projects.containsKey(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable {
|
||||
public List<Snapshot> getSnapshotsToAddToProject(String name) throws FailedConnectionException, InvalidProjectException {
|
||||
return updateProjectWithName(name);
|
||||
}
|
||||
|
||||
private List<Snapshot> updateProjectWithName(String name) throws Throwable {
|
||||
private List<Snapshot> updateProjectWithName(String name) throws FailedConnectionException, InvalidProjectException {
|
||||
if (!projects.containsKey(name)) {
|
||||
projects.put(name, new WLProject(name));
|
||||
}
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.model;
|
||||
|
||||
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.getforversion.SnapshotData;
|
||||
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 uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
|
@ -25,7 +23,7 @@ public class WLProject {
|
|||
snapshotFetcher = new SnapshotFetcher(name, snapshots);
|
||||
}
|
||||
|
||||
public List<Snapshot> fetchNewSnapshots() throws Throwable {
|
||||
public List<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
|
||||
return snapshotFetcher.fetchNewSnapshots();
|
||||
}
|
||||
|
||||
|
|
|
@ -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.getsavedvers.SnapshotGetSavedVersRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package uk.ac.ic.wlgitbridge.test.writelatex.model;
|
||||
|
||||
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 java.io.IOException;
|
||||
|
@ -16,14 +18,10 @@ public class WLProjectTests {
|
|||
WLProject project = new WLProject("1826rqgsdb");
|
||||
try {
|
||||
project.fetchNewSnapshots();
|
||||
} catch (InterruptedException e) {
|
||||
} catch (FailedConnectionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
} catch (InvalidProjectException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue