From 48305d963fce6ba9beacbee3a953b1ad6a80af39 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Sun, 16 Nov 2014 21:49:16 +0000 Subject: [PATCH] Testing candidate snapshot approval. --- .../SnapshotPushPostbackHandler.java | 32 ++++++++++------- .../application/WLGitBridgeServer.java | 10 +++--- .../bridge/WriteLatexDataSource.java | 3 +- .../writelatex/SnapshotFetcher.java | 4 +++ .../writelatex/WLDirectoryNodeSnapshot.java | 2 +- .../wlgitbridge/writelatex/WriteLatexAPI.java | 35 +++++++++++-------- .../filestore/store/WLFileStore.java | 5 +++ .../writelatex/model/WLDataModel.java | 3 +- .../writelatex/model/WLProject.java | 14 +++++--- 9 files changed, 69 insertions(+), 39 deletions(-) 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 fff9757011..55188da04b 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 @@ -1,11 +1,15 @@ package uk.ac.ic.wlgitbridge.application; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; +import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; import java.io.IOException; /** @@ -13,27 +17,29 @@ import java.io.IOException; */ public class SnapshotPushPostbackHandler extends AbstractHandler { + private final WriteLatexDataSource writeLatexDataSource; + + public SnapshotPushPostbackHandler(WriteLatexDataSource writeLatexDataSource) { + this.writeLatexDataSource = writeLatexDataSource; + } + @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // System.out.println("handling"); // System.out.println(request.getMethod()); // response.setContentType("text/html;charset=utf-8"); // response.setStatus(HttpServletResponse.SC_OK); - baseRequest.setHandled(false); if (request.getMethod().equals("POST") && request.getPathInfo().endsWith("postback")) { - System.out.println(request.getHeaderNames()); + BufferedReader reader = request.getReader(); + StringBuilder sb = new StringBuilder(); + for (String line; (line = reader.readLine()) != null; ) { + sb.append(line); + } + String data = sb.toString(); + JsonObject dataObj = new Gson().fromJson(data, JsonObject.class); + writeLatexDataSource.postbackReceivedSuccessfully(request.getRequestURI().split("/")[0]); + baseRequest.setHandled(true); } - - System.out.println(request.getRemoteAddr()); - System.out.println(request.getLocalName()); - System.out.println("method: " + request.getMethod()); - System.out.println("pathInfo: " + request.getPathInfo()); - System.out.println("contextPath: " + request.getContextPath()); - System.out.println("pathtranslated: " + request.getPathTranslated()); - System.out.println("queryString: " + request.getQueryString()); - System.out.println("remoteUser: " + request.getRemoteUser()); - System.out.println("requestURI: " + request.getRequestURI()); -// response.getWriter().println("

Hello World

"); } } 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 b03edad7c0..1fd50a3b24 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 @@ -8,6 +8,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.util.log.Log; import uk.ac.ic.wlgitbridge.application.jetty.NullLogger; +import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException; import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet; import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI; @@ -67,20 +68,21 @@ public class WLGitBridgeServer { private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException { HandlerCollection handlers = new HandlerCollection(); + WriteLatexAPI writeLatexDataSource = new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath)); handlers.setHandlers(new Handler[] { initResourceHandler(), - new SnapshotPushPostbackHandler(), - initGitHandler() + new SnapshotPushPostbackHandler(writeLatexDataSource), + initGitHandler(writeLatexDataSource) }); jettyServer.setHandler(handlers); } - private Handler initGitHandler() throws ServletException, InvalidRootDirectoryPathException { + private Handler initGitHandler(WriteLatexDataSource writeLatexDataSource) throws ServletException, InvalidRootDirectoryPathException { final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletContextHandler.setContextPath("/"); servletContextHandler.addServlet( new ServletHolder( - new WLGitServlet(servletContextHandler, new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath)), rootGitDirectoryPath)), + new WLGitServlet(servletContextHandler, writeLatexDataSource, rootGitDirectoryPath)), "/*" ); return servletContextHandler; 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 4804a8a7e3..9780950529 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 @@ -19,6 +19,7 @@ public interface WriteLatexDataSource { public void expectPostback(String projectName); /* Called by postback thread. */ - public void postbackReceived(String projectName); + public void postbackReceivedSuccessfully(String projectName); + public void postbackReceivedWithException(String projectName, SnapshotPostException exception); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java index 95dad68591..df9ca55f93 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java @@ -38,6 +38,10 @@ public class SnapshotFetcher { return snapshots.get(versions.last()); } + public void putLatestVersion(int versionID) { + versions.add(versionID); + } + private boolean getNew(SortedSet newSnapshots) throws FailedConnectionException, InvalidProjectException { SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName); SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java index 89d02d65c1..027740a03c 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java @@ -21,7 +21,7 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot { private final CandidateSnapshotCallback callback; public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, CandidateSnapshotCallback callback) { - previousVersionID = project.getLatestSnapshot().getVersionID(); + previousVersionID = project.getLatestSnapshotID(); projectName = project.getName(); projectURL = "http://" + hostname + "/" + projectName; this.directoryNode = directoryNode; 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 2b06165839..8b6f652a73 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 @@ -11,8 +11,9 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequest; import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel; import java.io.IOException; -import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Created by Winston on 16/11/14. @@ -20,9 +21,11 @@ import java.util.List; public class WriteLatexAPI implements WriteLatexDataSource { private final WLDataModel dataModel; + private final Map postbackConds; public WriteLatexAPI(WLDataModel dataModel) { this.dataModel = dataModel; + postbackConds = new HashMap(); } @Override @@ -46,29 +49,31 @@ public class WriteLatexAPI implements WriteLatexDataSource { public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException { CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname); new SnapshotPushRequest(candidate).request(); - throw new SnapshotPostException() { - - @Override - public String getMessage() { - return "unimplemented"; - } - - @Override - public List getDescriptionLines() { - return Arrays.asList("Currently implemented"); - } - }; + expectPostback(projectName); + candidate.approveWithVersionID(100); } @Override public void expectPostback(String projectName) { - + Object value = new Object(); + postbackConds.put(projectName, value); + try { + value.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } /* Called by postback thread. */ @Override - public void postbackReceived(String projectName) { + public void postbackReceivedSuccessfully(String projectName) { + System.out.println("successfully received postback for " + projectName); + postbackConds.get(projectName).notifyAll(); + } + @Override + public void postbackReceivedWithException(String projectName, SnapshotPostException exception) { + postbackReceivedSuccessfully(projectName); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java index 0a3dac02af..1ea36ab204 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java @@ -1,5 +1,6 @@ package uk.ac.ic.wlgitbridge.writelatex.filestore.store; +import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot; import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents; import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; @@ -59,6 +60,10 @@ public class WLFileStore { return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents, attDirectory); } + public void approveCandidateSnapshot(int versionID, CandidateSnapshot candidateSnapshot) { + fileStore.put(candidateSnapshot.getProjectName(), candidateSnapshot.getDirectoryNode()); + } + private WLDirectoryNode getDirectoryNodeForProjectName(String projectName) { WLDirectoryNode directoryNode = fileStore.get(projectName); if (directoryNode == null) { diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java index 836b697cf6..ca6c53b641 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java @@ -53,7 +53,8 @@ public class WLDataModel implements CandidateSnapshotCallback { @Override public void approveSnapshot(int versionID, CandidateSnapshot candidateSnapshot) { - + getProjectWithName(candidateSnapshot.getProjectName()).putLatestSnapshot(versionID); + fileStore.approveCandidateSnapshot(versionID, candidateSnapshot); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java index a508ed26fb..2600253682 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java @@ -17,7 +17,7 @@ public class WLProject { private final Map snapshots; private final SnapshotFetcher snapshotFetcher; - private Snapshot latestSnapshot; + private int latestSnapshotID; public WLProject(String name) { this.name = name; @@ -27,7 +27,7 @@ public class WLProject { public SortedSet fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException { SortedSet newSnapshots = snapshotFetcher.fetchNewSnapshots(); - latestSnapshot = snapshotFetcher.getLatestSnapshot(); + latestSnapshotID = snapshotFetcher.getLatestSnapshot().getVersionID(); return newSnapshots; } @@ -35,8 +35,14 @@ public class WLProject { return name; } - public Snapshot getLatestSnapshot() { - return latestSnapshot; + public int getLatestSnapshotID() { + return latestSnapshotID; + } + + public void putLatestSnapshot(int versionID) { + snapshots.put(versionID, null); + snapshotFetcher.putLatestVersion(versionID); + latestSnapshotID = versionID; } }