From e31b4fead5db72894406f0387e96b589a2f9a038 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Thu, 4 Dec 2014 23:34:02 +0000 Subject: [PATCH] Decoding postback GETs. --- .../application/AttsResourceHandler.java | 20 ++++++++++++++++++- .../SnapshotPushPostbackContents.java | 2 +- .../SnapshotPushPostbackHandler.java | 2 +- .../application/WLGitBridgeServer.java | 6 +++--- .../bridge/WriteLatexDataSource.java | 5 ++++- .../wlgitbridge/writelatex/WriteLatexAPI.java | 8 +++++++- .../api/request/push/PostbackContents.java | 7 +++++++ .../api/request/push/PostbackManager.java | 6 ++++++ .../push/UnexpectedPostbackException.java | 7 ------- .../InvalidPostbackKeyException.java | 7 +++++++ .../SnapshotPostExceptionBuilder.java | 1 - .../UnexpectedPostbackException.java | 7 +++++++ .../UnexpectedPostbackExceptionTest.java | 2 +- 13 files changed, 63 insertions(+), 17 deletions(-) delete mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackException.java create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/InvalidPostbackKeyException.java create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackException.java rename services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/{ => exception}/UnexpectedPostbackExceptionTest.java (64%) diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/AttsResourceHandler.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/AttsResourceHandler.java index 966844b5df..25b1ef1a36 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/AttsResourceHandler.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/AttsResourceHandler.java @@ -1,7 +1,10 @@ package uk.ac.ic.wlgitbridge.application; +import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.util.MultiMap; +import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -13,9 +16,24 @@ import java.io.IOException; */ public class AttsResourceHandler extends ResourceHandler { + private final WriteLatexAPI writeLatexDataSource; + + public AttsResourceHandler(WriteLatexAPI writeLatexDataSource) { + this.writeLatexDataSource = writeLatexDataSource; + } + @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - System.out.println(baseRequest.getMethod() + " " + baseRequest.getUri()); + HttpURI uri = baseRequest.getUri(); + System.out.println(baseRequest.getMethod() + " " + uri); + System.out.println(uri.getPath()); + MultiMap multimap = new MultiMap(); + uri.decodeQueryTo(multimap); + System.out.println(multimap); + + if (false) { + throw new ServletException(); + } super.handle(target, baseRequest, request, response); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackContents.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackContents.java index 13f65dd4ac..41e5b3abf1 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackContents.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackContents.java @@ -6,7 +6,7 @@ import com.google.gson.JsonObject; import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; 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.UnexpectedPostbackException; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostExceptionBuilder; /** diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java index 5e598f29f1..c77637f924 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java @@ -3,7 +3,7 @@ package uk.ac.ic.wlgitbridge.application; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; -import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException; +import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java index c6695c5d31..d1c2201408 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java @@ -71,7 +71,7 @@ public class WLGitBridgeServer { HandlerCollection handlers = new HandlerCollection(); WriteLatexAPI writeLatexDataSource = new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath)); handlers.setHandlers(new Handler[] { - initResourceHandler(), + initResourceHandler(writeLatexDataSource), new SnapshotPushPostbackHandler(writeLatexDataSource), initGitHandler(writeLatexDataSource) }); @@ -89,8 +89,8 @@ public class WLGitBridgeServer { return servletContextHandler; } - private Handler initResourceHandler() { - ResourceHandler resourceHandler = new AttsResourceHandler(); + private Handler initResourceHandler(WriteLatexAPI writeLatexDataSource) { + ResourceHandler resourceHandler = new AttsResourceHandler(writeLatexDataSource); resourceHandler.setResourceBase(new File(rootGitDirectoryPath, ".wlgb/atts").getAbsolutePath()); return resourceHandler; } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java index d6fd44d412..075f923dcf 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java @@ -2,8 +2,9 @@ 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.api.request.push.exception.InvalidPostbackKeyException; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException; -import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException; +import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException; import java.io.IOException; import java.util.List; @@ -22,6 +23,8 @@ public interface WriteLatexDataSource { public List getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException; public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException; + void checkPostbackKey(String projectName, String postbackKey) throws InvalidPostbackKeyException; + /* Called by postback thread. */ public void postbackReceivedSuccessfully(String projectName, String postbackKey, int versionID) throws UnexpectedPostbackException; public void postbackReceivedWithException(String projectName, String postbackKey, SnapshotPostException exception) throws UnexpectedPostbackException; diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java index dca244b241..bbbe7fda0d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java @@ -7,10 +7,11 @@ import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; 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.push.exception.InvalidPostbackKeyException; 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.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; @@ -93,6 +94,11 @@ public class WriteLatexAPI implements WriteLatexDataSource { } } + @Override + public void checkPostbackKey(String projectName, String postbackKey) throws InvalidPostbackKeyException { + postbackManager.checkPostbackKey(projectName, postbackKey); + } + /* Called by postback thread. */ @Override public void postbackReceivedSuccessfully(String projectName, String postbackKey, int versionID) throws UnexpectedPostbackException { diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java index 9b01fbf9cf..20f854e621 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java @@ -1,5 +1,6 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push; +import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.InvalidPostbackKeyException; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException; /** @@ -49,4 +50,10 @@ public class PostbackContents { } } + public void checkPostbackKey(String postbackKey) throws InvalidPostbackKeyException { + if (!postbackKey.equals(this.postbackKey)) { + throw new InvalidPostbackKeyException(); + } + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java index 39bb425c43..cef0b2f5d3 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java @@ -1,6 +1,8 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push; +import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.InvalidPostbackKeyException; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException; +import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException; import java.math.BigInteger; import java.security.SecureRandom; @@ -49,6 +51,10 @@ public class PostbackManager { return key; } + public void checkPostbackKey(String projectName, String postbackKey) throws InvalidPostbackKeyException { + postbackContentsTable.get(projectName).checkPostbackKey(postbackKey); + } + private String randomString() { return new BigInteger(130, random).toString(32); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackException.java deleted file mode 100644 index c7ddaaa591..0000000000 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackException.java +++ /dev/null @@ -1,7 +0,0 @@ -package uk.ac.ic.wlgitbridge.writelatex.api.request.push; - -/** - * Created by Winston on 17/11/14. - */ -public class UnexpectedPostbackException extends Throwable { -} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/InvalidPostbackKeyException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/InvalidPostbackKeyException.java new file mode 100644 index 0000000000..7197e1b03a --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/InvalidPostbackKeyException.java @@ -0,0 +1,7 @@ +package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception; + +/** + * Created by Winston on 04/12/14. + */ +public class InvalidPostbackKeyException extends Exception { +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/SnapshotPostExceptionBuilder.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/SnapshotPostExceptionBuilder.java index 8409c7faad..a11fff1eef 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/SnapshotPostExceptionBuilder.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/SnapshotPostExceptionBuilder.java @@ -2,7 +2,6 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception; import com.google.gson.JsonObject; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; -import uk.ac.ic.wlgitbridge.writelatex.api.request.push.UnexpectedPostbackException; /** * Created by Winston on 17/11/14. diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackException.java new file mode 100644 index 0000000000..e5d9515007 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackException.java @@ -0,0 +1,7 @@ +package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception; + +/** + * Created by Winston on 17/11/14. + */ +public class UnexpectedPostbackException extends Exception { +} diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackExceptionTest.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackExceptionTest.java similarity index 64% rename from services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackExceptionTest.java rename to services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackExceptionTest.java index 2af23f2f20..e06dcf8e9b 100644 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/UnexpectedPostbackExceptionTest.java +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/writelatex/api/request/push/exception/UnexpectedPostbackExceptionTest.java @@ -1,4 +1,4 @@ -package uk.ac.ic.wlgitbridge.writelatex.api.request.push; +package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception; import org.junit.Test;