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 740e6c21fb..faac9fbce8 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 @@ -4,6 +4,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException; +import java.io.IOException; import java.util.List; /** @@ -14,7 +15,7 @@ public interface WriteLatexDataSource { /* Called by request thread. */ public boolean repositoryExists(String projectName) throws FailedConnectionException; public List getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException; - public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException; + public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException; public void expectPostback(String projectName); /* Called by postback thread. */ diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java index 0639a363e9..95d5fd3d95 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java @@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.git.handler.hook; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.PreReceiveHook; import org.eclipse.jgit.transport.ReceiveCommand; +import org.eclipse.jgit.transport.ReceiveCommand.Result; import org.eclipse.jgit.transport.ReceivePack; import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents; import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; @@ -10,6 +11,7 @@ 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.exception.FailedConnectionException; import java.io.IOException; import java.util.Collection; @@ -32,21 +34,24 @@ public class WriteLatexPutHook implements PreReceiveHook { handleReceiveCommand(receivePack.getRepository(), receiveCommand); } catch (IOException e) { receivePack.sendError("IOException"); - receiveCommand.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, "I/O Exception"); + receiveCommand.setResult(Result.REJECTED_OTHER_REASON, "I/O Exception"); + } catch (FailedConnectionException e) { + receivePack.sendError("failed connection"); + receiveCommand.setResult(Result.REJECTED_OTHER_REASON, "failed connection"); } catch (OutOfDateException e) { - receiveCommand.setResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD); + receiveCommand.setResult(Result.REJECTED_NONFASTFORWARD); } catch (SnapshotPostException e) { String message = e.getMessage(); receivePack.sendError(message); for (String line : e.getDescriptionLines()) { receivePack.sendMessage("hint: " + line); } - receiveCommand.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, message); + receiveCommand.setResult(Result.REJECTED_OTHER_REASON, message); } } } - private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws ForcedPushException, IOException, SnapshotPostException { + private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, FailedConnectionException { checkForcedPush(receiveCommand); writeLatexDataSource.putDirectoryContentsToProjectWithName(repository.getWorkTree().getName(), getPushedDirectoryContents(repository, 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 1a6005cc42..3084b8639d 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 @@ -1,8 +1,11 @@ package uk.ac.ic.wlgitbridge.writelatex; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot; import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback; +import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode; import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode; import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; @@ -21,11 +24,33 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot { projectName = project.getName(); this.directoryNode = directoryNode; this.callback = callback; + System.out.println(getJsonRepresentation()); } @Override public JsonElement getJsonRepresentation() { - return null; + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("latestVerId", previousVersionID); + jsonObject.add("files", getFilesAsJson()); + jsonObject.addProperty("postbackUrl", "wsomewhere"); + return jsonObject; + } + + private JsonArray getFilesAsJson() { + JsonArray filesArray = new JsonArray(); + for (FileNode fileNode : directoryNode.getFileNodes()) { + filesArray.add(getFileAsJson(fileNode)); + } + return filesArray; + } + + private JsonObject getFileAsJson(FileNode fileNode) { + JsonObject file = new JsonObject(); + file.addProperty("name", fileNode.getFilePath()); + if (fileNode.isChanged()) { + file.addProperty("url", "url"); + } + return file; } @Override 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 554f24d9fb..86c639740c 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,8 +7,10 @@ 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.SnapshotPushRequest; import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel; +import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -41,8 +43,9 @@ public class WriteLatexAPI implements WriteLatexDataSource { } @Override - public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException { + public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException { CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents); + new SnapshotPushRequest(candidate); throw new SnapshotPostException() { @Override diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequest.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequest.java new file mode 100644 index 0000000000..871a588ec8 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequest.java @@ -0,0 +1,39 @@ +package uk.ac.ic.wlgitbridge.writelatex.api.request.push; + +import com.google.gson.JsonElement; +import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot; +import uk.ac.ic.wlgitbridge.writelatex.api.request.base.HTTPMethod; +import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; + +/** + * Created by Winston on 16/11/14. + */ +public class SnapshotPushRequest extends SnapshotAPIRequest { + + private static final String API_CALL = "/snapshots"; + + private final CandidateSnapshot candidateSnapshot; + + public SnapshotPushRequest(CandidateSnapshot candidateSnapshot) { + super(candidateSnapshot.getProjectName(), API_CALL); + this.candidateSnapshot = candidateSnapshot; + } + + @Override + protected HTTPMethod httpMethod() { + return HTTPMethod.POST; + } + + @Override + protected String getPostBody() { + return candidateSnapshot.getJsonRepresentation().toString(); + } + + @Override + protected SnapshotPushRequestResult parseResponse(JsonElement json) throws FailedConnectionException { + System.out.println(json); + return null; + } + +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequestResult.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequestResult.java new file mode 100644 index 0000000000..2491b833fd --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequestResult.java @@ -0,0 +1,22 @@ +package uk.ac.ic.wlgitbridge.writelatex.api.request.push; + +import com.google.gson.JsonElement; +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; + +/** + * Created by Winston on 16/11/14. + */ +public class SnapshotPushRequestResult extends Result { + + public SnapshotPushRequestResult(Request request, JsonElement json) throws FailedConnectionException { + super(request, json); + } + + @Override + public void fromJSON(JsonElement json) throws FailedConnectionException { + + } + +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/BlobNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/BlobNode.java index 15561871dd..c5fdde6edd 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/BlobNode.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/BlobNode.java @@ -1,10 +1,14 @@ 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.filestore.RepositoryFile; import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.Blob; import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.ByteBlob; import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.RawFileBlob; +import java.io.File; +import java.io.IOException; import java.util.Map; /** @@ -19,6 +23,11 @@ public class BlobNode extends FileNode { blob = new RawFileBlob(rawFile); } + public BlobNode(RepositoryFile repositoryFile, Map fileNodeTable, File projectAttDirectory) throws IOException, FailedConnectionException { + this(repositoryFile, fileNodeTable); + writeChanged(projectAttDirectory); + } + @Override public void handleIndexer(FileNodeIndexer fileNodeIndexer) { fileNodeIndexer.index(this); @@ -29,4 +38,10 @@ public class BlobNode extends FileNode { return blob; } + private void writeChanged(File projectAttDirectory) throws FailedConnectionException, IOException { + if (isChanged()) { + writeToDisk(projectAttDirectory); + } + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java index 4d8bf71f8c..64389f2cbd 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java @@ -16,7 +16,7 @@ import java.util.Map; public abstract class FileNode { private final String filePath; - private final boolean unchanged; + private final boolean changed; public FileNode(RawFile file, Map context) { this(file.getPath(), context); @@ -25,13 +25,17 @@ public abstract class FileNode { public FileNode(String filePath, Map context) { this.filePath = filePath; FileNode currentFileNode = context.get(filePath); - unchanged = currentFileNode != null && equals(currentFileNode); + changed = currentFileNode == null || !equals(currentFileNode); } public String getFilePath() { return filePath; } + public boolean isChanged() { + return changed; + } + public byte[] getContents() throws FailedConnectionException { return getBlob().getContents(); } @@ -56,7 +60,7 @@ public abstract class FileNode { @Override public String toString() { - return String.valueOf(unchanged); + return String.valueOf(changed); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/WLDirectoryNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/WLDirectoryNode.java index cd7085ffed..c7428ed63d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/WLDirectoryNode.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/WLDirectoryNode.java @@ -8,6 +8,8 @@ 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; +import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -19,18 +21,24 @@ import java.util.Map.Entry; */ public class WLDirectoryNode { + private final String projectName; private Map fileNodeTable; private FileIndexStore fileIndexStore; - public WLDirectoryNode() { - this(new HashMap(), new FileIndexStore()); + public WLDirectoryNode(String projectName) { + this(projectName, new HashMap(), new FileIndexStore()); } - public WLDirectoryNode(Map fileNodeTable, FileIndexStore fileIndexStore) { + public WLDirectoryNode(String projectName, Map fileNodeTable, FileIndexStore fileIndexStore) { + this.projectName = projectName; this.fileNodeTable = fileNodeTable; this.fileIndexStore = fileIndexStore; } + public List getFileNodes() { + return new LinkedList(fileNodeTable.values()); + } + public List updateFromSnapshot(Snapshot snapshot) throws FailedConnectionException { Map updatedFileNodeTable = new HashMap(); List srcs = snapshot.getSrcs(); @@ -49,13 +57,13 @@ public class WLDirectoryNode { return fileNodes; } - public WLDirectoryNode createFromRawDirectoryContents(RawDirectoryContents rawDirectoryContents) { + public WLDirectoryNode createFromRawDirectoryContents(RawDirectoryContents rawDirectoryContents, File attachmentDirectory) throws IOException, FailedConnectionException { Map candidateFileNodeTable = new HashMap(); for (Entry fileContents : rawDirectoryContents.getFileContentsTable().entrySet()) { - BlobNode blobNode = new BlobNode(new RepositoryFile(fileContents), fileNodeTable); + BlobNode blobNode = new BlobNode(new RepositoryFile(fileContents), fileNodeTable, new File(attachmentDirectory, projectName)); candidateFileNodeTable.put(blobNode.getFilePath(), blobNode); } - return new WLDirectoryNode(candidateFileNodeTable, + return new WLDirectoryNode(projectName, candidateFileNodeTable, new FileIndexStore(new LinkedList(candidateFileNodeTable.values()))); } 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 99fba081db..0a3dac02af 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 @@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; import java.io.File; +import java.io.IOException; import java.util.*; /** @@ -19,12 +20,15 @@ public class WLFileStore { private final Map fileStore; private final File rootGitDirectory; + private final File attDirectory; public WLFileStore(String rootGitDirectoryPath) { fileStore = new HashMap(); rootGitDirectory = new File(rootGitDirectoryPath); rootGitDirectory.mkdirs(); deleteInDirectory(rootGitDirectory); + attDirectory = new File(rootGitDirectory, ".wlgb/atts"); + attDirectory.mkdirs(); } public static void deleteInDirectory(File directory) { @@ -51,14 +55,14 @@ public class WLFileStore { return writableRepositories; } - public WLDirectoryNode createNextDirectoryNodeInProjectFromContents(WLProject project, RawDirectoryContents directoryContents) { - return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents); + public WLDirectoryNode createNextDirectoryNodeInProjectFromContents(WLProject project, RawDirectoryContents directoryContents) throws IOException, FailedConnectionException { + return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents, attDirectory); } private WLDirectoryNode getDirectoryNodeForProjectName(String projectName) { WLDirectoryNode directoryNode = fileStore.get(projectName); if (directoryNode == null) { - directoryNode = new WLDirectoryNode(); + directoryNode = new WLDirectoryNode(projectName); fileStore.put(projectName, directoryNode); } return directoryNode; 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 ef7955f35f..c2a77bbe5b 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 @@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -42,7 +43,7 @@ public class WLDataModel implements CandidateSnapshotCallback { return project; } - public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException { + public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException { return new WLDirectoryNodeSnapshot(getProjectWithName(projectName), fileStore.createNextDirectoryNodeInProjectFromContents(getProjectWithName(projectName), directoryContents),