Added handlers for resources and SnapshotPushPostback.

This commit is contained in:
Winston Li 2014-11-16 17:57:53 +00:00
parent 74ac6e2379
commit abc698085a
21 changed files with 189 additions and 62 deletions

View file

@ -0,0 +1,33 @@
package uk.ac.ic.wlgitbridge.application;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Winston on 16/11/14.
*/
public class SnapshotPushPostbackHandler extends AbstractHandler {
@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);
// 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

@ -1,12 +1,15 @@
package uk.ac.ic.wlgitbridge.application;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
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.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
import javax.servlet.ServletException;
@ -61,6 +64,16 @@ public class WLGitBridgeServer {
}
private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException {
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] {
initResourceHandler(),
new SnapshotPushPostbackHandler(),
initGitHandler()
});
jettyServer.setHandler(handlers);
}
private Handler initGitHandler() throws ServletException, InvalidRootDirectoryPathException {
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(
@ -68,7 +81,13 @@ public class WLGitBridgeServer {
new WLGitServlet(servletContextHandler, new WLDataModel(rootGitDirectoryPath), rootGitDirectoryPath)),
"/*"
);
jettyServer.setHandler(servletContextHandler);
return servletContextHandler;
}
private Handler initResourceHandler() {
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(rootGitDirectoryPath);
return resourceHandler;
}
}

View file

@ -0,0 +1,11 @@
package uk.ac.ic.wlgitbridge.bridge;
/**
* Created by Winston on 16/11/14.
*/
public interface RawFile {
public String getPath();
public byte[] getContents();
}

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.model.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import java.util.List;
@ -11,8 +11,8 @@ import java.util.List;
*/
public interface WriteLatexDataSource {
public boolean repositoryExists(String name) throws FailedConnectionException;
public List<WritableRepositoryContents> getWritableRepositories(String name) throws FailedConnectionException, InvalidProjectException;
public void putDirectoryContentsToProjectWithName(String name, RawDirectoryContents directoryContents) throws SnapshotPostException;
public boolean repositoryExists(String projectName) throws FailedConnectionException;
public List<WritableRepositoryContents> getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException;
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException;
}

View file

@ -8,8 +8,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.model.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.model.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import java.io.IOException;
import java.util.Collection;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
import uk.ac.ic.wlgitbridge.writelatex.model.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import java.util.Arrays;
import java.util.List;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
package uk.ac.ic.wlgitbridge.writelatex;
import java.util.List;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
package uk.ac.ic.wlgitbridge.writelatex;
import java.util.List;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
package uk.ac.ic.wlgitbridge.writelatex;
import java.util.List;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
package uk.ac.ic.wlgitbridge.writelatex;
import java.util.List;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
package uk.ac.ic.wlgitbridge.writelatex;
import java.util.List;

View file

@ -2,19 +2,14 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotFile implements JSONSource {
public class SnapshotFile implements JSONSource, RawFile {
protected byte[] contents;
private String path;
@ -24,18 +19,20 @@ public class SnapshotFile implements JSONSource {
}
@Override
public void fromJSON(JsonElement json) throws FailedConnectionException {
JsonArray jsonArray = json.getAsJsonArray();
getContentsFromJSON(jsonArray);
getPathFromJSON(jsonArray);
public String getPath() {
return path;
}
@Override
public byte[] getContents() {
return contents;
}
public String getPath() {
return path;
@Override
public void fromJSON(JsonElement json) throws FailedConnectionException {
JsonArray jsonArray = json.getAsJsonArray();
getContentsFromJSON(jsonArray);
getPathFromJSON(jsonArray);
}
protected void getContentsFromJSON(JsonArray jsonArray) throws FailedConnectionException {

View file

@ -0,0 +1,28 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
import java.util.Map.Entry;
/**
* Created by Winston on 16/11/14.
*/
public class RepositoryFile implements RawFile {
private final Entry<String, byte[]> fileContents;
public RepositoryFile(Entry<String, byte[]> fileContents) {
this.fileContents = fileContents;
}
@Override
public String getPath() {
return fileContents.getKey();
}
@Override
public byte[] getContents() {
return fileContents.getValue();
}
}

View file

@ -3,7 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex.filestore.blob;
/**
* Created by Winston on 14/11/14.
*/
public abstract class ByteBlob extends Blob {
public class ByteBlob extends Blob {
private final byte[] contents;

View file

@ -0,0 +1,14 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.blob;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
/**
* Created by Winston on 14/11/14.
*/
public class RawFileBlob extends ByteBlob {
public RawFileBlob(RawFile rawFile) {
super(rawFile.getContents());
}
}

View file

@ -1,14 +0,0 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.blob;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
/**
* Created by Winston on 14/11/14.
*/
public class SnapshotFileBlob extends ByteBlob {
public SnapshotFileBlob(SnapshotFile snapshotFile) {
super(snapshotFile.getContents());
}
}

View file

@ -1,9 +1,9 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.node;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.Blob;
import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.SnapshotFileBlob;
import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.ByteBlob;
import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.RawFileBlob;
import java.util.Map;
@ -12,11 +12,11 @@ import java.util.Map;
*/
public class BlobNode extends FileNode {
private SnapshotFileBlob blob;
private ByteBlob blob;
public BlobNode(SnapshotFile snapshotFile, Map<String, FileNode> context) throws FailedConnectionException {
super(snapshotFile, context);
blob = new SnapshotFileBlob(snapshotFile);
public BlobNode(RawFile rawFile, Map<String, FileNode> context) {
super(rawFile, context);
blob = new RawFileBlob(rawFile);
}
@Override

View file

@ -1,7 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.node;
import uk.ac.ic.wlgitbridge.bridge.RawFile;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.Blob;
import java.io.File;
@ -18,8 +18,12 @@ public abstract class FileNode {
private final String filePath;
private final boolean unchanged;
public FileNode(SnapshotFile snapshotFile, Map<String, FileNode> context) throws FailedConnectionException {
filePath = snapshotFile.getPath();
public FileNode(RawFile file, Map<String, FileNode> context) {
this(file.getPath(), context);
}
public FileNode(String filePath, Map<String, FileNode> context) {
this.filePath = filePath;
FileNode currentFileNode = context.get(filePath);
unchanged = currentFileNode != null && equals(currentFileNode);
}
@ -45,4 +49,14 @@ public abstract class FileNode {
protected abstract Blob getBlob();
@Override
public boolean equals(Object obj) {
return obj instanceof FileNode && filePath.equals(((FileNode) obj).filePath);
}
@Override
public String toString() {
return String.valueOf(unchanged);
}
}

View file

@ -1,8 +1,10 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.node;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotAttachment;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
import uk.ac.ic.wlgitbridge.writelatex.filestore.RepositoryFile;
import uk.ac.ic.wlgitbridge.writelatex.filestore.store.FileIndexStore;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
@ -10,6 +12,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Created by Winston on 08/11/14.
@ -20,8 +23,12 @@ public class WLDirectoryNode {
private FileIndexStore fileIndexStore;
public WLDirectoryNode() {
fileNodeTable = new HashMap<String, FileNode>();
fileIndexStore = new FileIndexStore();
this(new HashMap<String, FileNode>(), new FileIndexStore());
}
public WLDirectoryNode(Map<String, FileNode> fileNodeTable, FileIndexStore fileIndexStore) {
this.fileNodeTable = fileNodeTable;
this.fileIndexStore = fileIndexStore;
}
public List<FileNode> updateFromSnapshot(Snapshot snapshot) throws FailedConnectionException {
@ -36,12 +43,22 @@ public class WLDirectoryNode {
AttachmentNode attachmentNode = new AttachmentNode(att, fileNodeTable, fileIndexStore);
updatedFileNodeTable.put(attachmentNode.getFilePath(), attachmentNode);
}
fileNodeTable = updatedFileNodeTable;
LinkedList<FileNode> fileNodes = new LinkedList<FileNode>(updatedFileNodeTable.values());
fileNodeTable = updatedFileNodeTable;
fileIndexStore = new FileIndexStore(fileNodes);
return fileNodes;
}
public WLDirectoryNode createFromRawDirectoryContents(RawDirectoryContents rawDirectoryContents) {
Map<String, FileNode> candidateFileNodeTable = new HashMap<String, FileNode>();
for (Entry<String, byte[]> fileContents : rawDirectoryContents.getFileContentsTable().entrySet()) {
BlobNode blobNode = new BlobNode(new RepositoryFile(fileContents), fileNodeTable);
candidateFileNodeTable.put(blobNode.getFilePath(), blobNode);
}
return new WLDirectoryNode(candidateFileNodeTable,
new FileIndexStore(new LinkedList<FileNode>(candidateFileNodeTable.values())));
}
@Override
public String toString() {
return fileNodeTable.toString();

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.filestore.store;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
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.getdoc.exception.InvalidProjectException;
@ -50,6 +51,10 @@ public class WLFileStore {
return writableRepositories;
}
public WLDirectoryNode createCandidateDirectoryNodeForProjectWithContents(WLProject project, RawDirectoryContents directoryContents) {
return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents);
}
private WLDirectoryNode getDirectoryNodeForProjectName(String projectName) {
WLDirectoryNode directoryNode = fileStore.get(projectName);
if (directoryNode == null) {

View file

@ -3,9 +3,11 @@ package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
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.filestore.node.WLDirectoryNode;
import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
import java.util.Arrays;
@ -27,8 +29,8 @@ public class WLDataModel implements WriteLatexDataSource {
}
@Override
public boolean repositoryExists(String name) throws FailedConnectionException {
SnapshotGetDocRequest snapshotGetDocRequest = new SnapshotGetDocRequest(name);
public boolean repositoryExists(String projectName) throws FailedConnectionException {
SnapshotGetDocRequest snapshotGetDocRequest = new SnapshotGetDocRequest(projectName);
snapshotGetDocRequest.request();
try {
snapshotGetDocRequest.getResult().getVersionID();
@ -39,14 +41,15 @@ public class WLDataModel implements WriteLatexDataSource {
}
@Override
public List<WritableRepositoryContents> getWritableRepositories(String name) throws FailedConnectionException, InvalidProjectException {
return updateProjectWithName(name);
public List<WritableRepositoryContents> getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException {
return updateProjectWithName(projectName);
}
@Override
public void putDirectoryContentsToProjectWithName(String name, RawDirectoryContents directoryContents) throws SnapshotPostException {
System.out.println("Pushing project with name: " + name);
System.out.println(directoryContents.getFileContentsTable());
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException {
WLDirectoryNode dn = fileStore.createCandidateDirectoryNodeForProjectWithContents(getProjectWithName(projectName), directoryContents);
System.out.println("Pushing project with name: " + projectName);
System.out.println(dn);
throw new SnapshotPostException() {
@Override