Testing candidate snapshot approval.

This commit is contained in:
Winston Li 2014-11-16 21:49:16 +00:00
parent 06a7ee6614
commit 48305d963f
9 changed files with 69 additions and 39 deletions

View file

@ -1,11 +1,15 @@
package uk.ac.ic.wlgitbridge.application; 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.Request;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
/** /**
@ -13,27 +17,29 @@ import java.io.IOException;
*/ */
public class SnapshotPushPostbackHandler extends AbstractHandler { public class SnapshotPushPostbackHandler extends AbstractHandler {
private final WriteLatexDataSource writeLatexDataSource;
public SnapshotPushPostbackHandler(WriteLatexDataSource writeLatexDataSource) {
this.writeLatexDataSource = writeLatexDataSource;
}
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// System.out.println("handling"); // System.out.println("handling");
// System.out.println(request.getMethod()); // System.out.println(request.getMethod());
// response.setContentType("text/html;charset=utf-8"); // response.setContentType("text/html;charset=utf-8");
// response.setStatus(HttpServletResponse.SC_OK); // response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(false);
if (request.getMethod().equals("POST") && request.getPathInfo().endsWith("postback")) { 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("<h1>Hello World</h1>");
} }
} }

View file

@ -8,6 +8,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger; 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.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet; import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI; import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI;
@ -67,20 +68,21 @@ public class WLGitBridgeServer {
private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException { private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException {
HandlerCollection handlers = new HandlerCollection(); HandlerCollection handlers = new HandlerCollection();
WriteLatexAPI writeLatexDataSource = new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath));
handlers.setHandlers(new Handler[] { handlers.setHandlers(new Handler[] {
initResourceHandler(), initResourceHandler(),
new SnapshotPushPostbackHandler(), new SnapshotPushPostbackHandler(writeLatexDataSource),
initGitHandler() initGitHandler(writeLatexDataSource)
}); });
jettyServer.setHandler(handlers); jettyServer.setHandler(handlers);
} }
private Handler initGitHandler() throws ServletException, InvalidRootDirectoryPathException { private Handler initGitHandler(WriteLatexDataSource writeLatexDataSource) throws ServletException, InvalidRootDirectoryPathException {
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/"); servletContextHandler.setContextPath("/");
servletContextHandler.addServlet( servletContextHandler.addServlet(
new ServletHolder( new ServletHolder(
new WLGitServlet(servletContextHandler, new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath)), rootGitDirectoryPath)), new WLGitServlet(servletContextHandler, writeLatexDataSource, rootGitDirectoryPath)),
"/*" "/*"
); );
return servletContextHandler; return servletContextHandler;

View file

@ -19,6 +19,7 @@ public interface WriteLatexDataSource {
public void expectPostback(String projectName); public void expectPostback(String projectName);
/* Called by postback thread. */ /* Called by postback thread. */
public void postbackReceived(String projectName); public void postbackReceivedSuccessfully(String projectName);
public void postbackReceivedWithException(String projectName, SnapshotPostException exception);
} }

View file

@ -38,6 +38,10 @@ public class SnapshotFetcher {
return snapshots.get(versions.last()); return snapshots.get(versions.last());
} }
public void putLatestVersion(int versionID) {
versions.add(versionID);
}
private boolean getNew(SortedSet<Snapshot> newSnapshots) throws FailedConnectionException, InvalidProjectException { private boolean getNew(SortedSet<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);

View file

@ -21,7 +21,7 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot {
private final CandidateSnapshotCallback callback; private final CandidateSnapshotCallback callback;
public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, CandidateSnapshotCallback callback) { public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, CandidateSnapshotCallback callback) {
previousVersionID = project.getLatestSnapshot().getVersionID(); previousVersionID = project.getLatestSnapshotID();
projectName = project.getName(); projectName = project.getName();
projectURL = "http://" + hostname + "/" + projectName; projectURL = "http://" + hostname + "/" + projectName;
this.directoryNode = directoryNode; this.directoryNode = directoryNode;

View file

@ -11,8 +11,9 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequest;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel; import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Created by Winston on 16/11/14. * Created by Winston on 16/11/14.
@ -20,9 +21,11 @@ import java.util.List;
public class WriteLatexAPI implements WriteLatexDataSource { public class WriteLatexAPI implements WriteLatexDataSource {
private final WLDataModel dataModel; private final WLDataModel dataModel;
private final Map<String, Object> postbackConds;
public WriteLatexAPI(WLDataModel dataModel) { public WriteLatexAPI(WLDataModel dataModel) {
this.dataModel = dataModel; this.dataModel = dataModel;
postbackConds = new HashMap<String, Object>();
} }
@Override @Override
@ -46,29 +49,31 @@ public class WriteLatexAPI implements WriteLatexDataSource {
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException { public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException {
CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname); CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname);
new SnapshotPushRequest(candidate).request(); new SnapshotPushRequest(candidate).request();
throw new SnapshotPostException() { expectPostback(projectName);
candidate.approveWithVersionID(100);
@Override
public String getMessage() {
return "unimplemented";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList("Currently implemented");
}
};
} }
@Override @Override
public void expectPostback(String projectName) { 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. */ /* Called by postback thread. */
@Override @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);
} }
} }

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.store; 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.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents; import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
@ -59,6 +60,10 @@ public class WLFileStore {
return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents, attDirectory); 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) { private WLDirectoryNode getDirectoryNodeForProjectName(String projectName) {
WLDirectoryNode directoryNode = fileStore.get(projectName); WLDirectoryNode directoryNode = fileStore.get(projectName);
if (directoryNode == null) { if (directoryNode == null) {

View file

@ -53,7 +53,8 @@ public class WLDataModel implements CandidateSnapshotCallback {
@Override @Override
public void approveSnapshot(int versionID, CandidateSnapshot candidateSnapshot) { public void approveSnapshot(int versionID, CandidateSnapshot candidateSnapshot) {
getProjectWithName(candidateSnapshot.getProjectName()).putLatestSnapshot(versionID);
fileStore.approveCandidateSnapshot(versionID, candidateSnapshot);
} }
} }

View file

@ -17,7 +17,7 @@ public class WLProject {
private final Map<Integer, Snapshot> snapshots; private final Map<Integer, Snapshot> snapshots;
private final SnapshotFetcher snapshotFetcher; private final SnapshotFetcher snapshotFetcher;
private Snapshot latestSnapshot; private int latestSnapshotID;
public WLProject(String name) { public WLProject(String name) {
this.name = name; this.name = name;
@ -27,7 +27,7 @@ public class WLProject {
public SortedSet<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException { public SortedSet<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
SortedSet<Snapshot> newSnapshots = snapshotFetcher.fetchNewSnapshots(); SortedSet<Snapshot> newSnapshots = snapshotFetcher.fetchNewSnapshots();
latestSnapshot = snapshotFetcher.getLatestSnapshot(); latestSnapshotID = snapshotFetcher.getLatestSnapshot().getVersionID();
return newSnapshots; return newSnapshots;
} }
@ -35,8 +35,14 @@ public class WLProject {
return name; return name;
} }
public Snapshot getLatestSnapshot() { public int getLatestSnapshotID() {
return latestSnapshot; return latestSnapshotID;
}
public void putLatestSnapshot(int versionID) {
snapshots.put(versionID, null);
snapshotFetcher.putLatestVersion(versionID);
latestSnapshotID = versionID;
} }
} }