Added exception if the push request returned instantly with an error.

This commit is contained in:
Winston Li 2014-11-17 10:41:36 +00:00
parent c9d773b6e8
commit da5b661858
18 changed files with 63 additions and 35 deletions

View file

@ -4,9 +4,10 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostExceptionBuilder;
/**
* Created by Winston on 17/11/14.
@ -37,7 +38,7 @@ public class SnapshotPushPostbackContents implements JSONSource {
setResult(responseObject, code);
}
public void sendPostback() throws UnexpectedPostbackException {
public void processPostback() throws UnexpectedPostbackException {
if (exception == null) {
writeLatexDataSource.postbackReceivedSuccessfully(projectName, versionID);
} else {

View file

@ -33,7 +33,7 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
String projectName = request.getRequestURI().split("/")[1];
SnapshotPushPostbackContents postbackContents = new SnapshotPushPostbackContents(writeLatexDataSource, projectName, contents);
try {
postbackContents.sendPostback();
postbackContents.processPostback();
} catch (UnexpectedPostbackException e) {
throw new ServletException();
}

View file

@ -2,7 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge;
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.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException;
import java.io.IOException;

View file

@ -9,8 +9,8 @@ import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.writelatex.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.IOException;
@ -51,7 +51,6 @@ public class WriteLatexPutHook implements PreReceiveHook {
receiveCommand.setResult(Result.REJECTED_OTHER_REASON, message);
}
}
System.out.println("success");
}
private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, FailedConnectionException {

View file

@ -1,7 +1,7 @@
package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import java.util.Arrays;
import java.util.List;

View file

@ -9,7 +9,10 @@ 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.push.PostbackManager;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequestResult;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
import java.io.IOException;
@ -48,8 +51,14 @@ public class WriteLatexAPI implements WriteLatexDataSource {
@Override
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException {
CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname);
new SnapshotPushRequest(candidate).request();
candidate.approveWithVersionID(postbackManager.getVersionID(projectName));
SnapshotPushRequest snapshotPushRequest = new SnapshotPushRequest(candidate);
snapshotPushRequest.request();
SnapshotPushRequestResult result = snapshotPushRequest.getResult();
if (result.wasSuccessful()) {
candidate.approveWithVersionID(postbackManager.getVersionID(projectName));
} else {
throw new OutOfDateException();
}
}
/* Called by postback thread. */

View file

@ -2,7 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import java.util.List;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.push;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
/**
* Created by Winston on 17/11/14.
@ -13,6 +13,7 @@ public class PostbackContents {
public PostbackContents() {
received = false;
exception = null;
}
public synchronized int waitForPostback() throws SnapshotPostException {

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.push;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import java.util.HashMap;
import java.util.Map;

View file

@ -32,8 +32,7 @@ public class SnapshotPushRequest extends SnapshotAPIRequest<SnapshotPushRequestR
@Override
protected SnapshotPushRequestResult parseResponse(JsonElement json) throws FailedConnectionException {
System.out.println(json);
return null;
return new SnapshotPushRequestResult(this, json);
}
}

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.push;
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;
@ -10,13 +11,27 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc
*/
public class SnapshotPushRequestResult extends Result {
private boolean success;
public SnapshotPushRequestResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json);
}
public boolean wasSuccessful() {
return success;
}
@Override
public void fromJSON(JsonElement json) {
JsonObject responseObject = json.getAsJsonObject();
String code = responseObject.get("code").getAsString();
if (code.equals("accepted")) {
success = true;
} else if (code.equals("outOfDate")) {
success = false;
} else {
throw new RuntimeException();
}
}
}

View file

@ -1,8 +1,9 @@
package uk.ac.ic.wlgitbridge.writelatex;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.List;
/**
@ -16,12 +17,12 @@ public class InvalidFilesException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
return "invalid files";
}
@Override
public List<String> getDescriptionLines() {
return null;
return Arrays.asList("invalid files, change ext or something");
}
@Override

View file

@ -1,7 +1,8 @@
package uk.ac.ic.wlgitbridge.writelatex;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
import java.util.Arrays;
import java.util.List;
/**
@ -15,12 +16,12 @@ public class InvalidProjectException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
return "invalid project";
}
@Override
public List<String> getDescriptionLines() {
return null;
return Arrays.asList("your project is too big");
}
@Override

View file

@ -1,8 +1,9 @@
package uk.ac.ic.wlgitbridge.writelatex;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.List;
/**
@ -14,14 +15,18 @@ public class OutOfDateException extends SnapshotPostException {
super(json);
}
public OutOfDateException() {
}
@Override
public String getMessage() {
return null;
return "out of date";
}
@Override
public List<String> getDescriptionLines() {
return null;
return Arrays.asList("out of date (shouldn't print this)");
}
@Override

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;

View file

@ -1,10 +1,6 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.InvalidFilesException;
import uk.ac.ic.wlgitbridge.writelatex.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.UnexpectedErrorException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException;

View file

@ -1,8 +1,9 @@
package uk.ac.ic.wlgitbridge.writelatex;
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.List;
/**
@ -16,12 +17,12 @@ public class UnexpectedErrorException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
return "unexpected error";
}
@Override
public List<String> getDescriptionLines() {
return null;
return Arrays.asList("writelatex error");
}
@Override

View file

@ -4,7 +4,7 @@ import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.WLDirectoryNodeSnapshot;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;