Просмотр исходного кода

Added data structures for SnapshotGetForVersionRequest.

Winston Li 12 лет назад
Родитель
Сommit
0f4e99361f

+ 2 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/base/JSONSource.java

@@ -2,6 +2,8 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
 
 import com.google.gson.JsonElement;
 
+import java.io.IOException;
+
 /**
  * Created by Winston on 06/11/14.
  */

+ 47 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java

@@ -0,0 +1,47 @@
+package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Created by Winston on 06/11/14.
+ */
+public class SnapshotData implements JSONSource {
+
+    public static final String JSON_KEY_SRCS = "srcs";
+    public static final String JSON_KEY_ATTS = "atts";
+
+    private List<WLFile> srcs;
+    private List<WLFile> atts;
+
+    public SnapshotData(JsonElement json) {
+        srcs = new LinkedList<WLFile>();
+        atts = new LinkedList<WLFile>();
+        fromJSON(json);
+    }
+
+    @Override
+    public void fromJSON(JsonElement json) {
+        populateSrcs(json.getAsJsonObject().get(JSON_KEY_SRCS).getAsJsonArray());
+        populateAtts(json.getAsJsonObject().get(JSON_KEY_ATTS).getAsJsonArray());
+    }
+
+    private void populateSrcs(JsonArray jsonArray) {
+        for (JsonElement json : jsonArray) {
+            srcs.add(new WLFile(json));
+        }
+        System.out.println(srcs);
+    }
+
+    private void populateAtts(JsonArray jsonArray) {
+        for (JsonElement json : jsonArray) {
+            atts.add(new WLAttachment(json));
+        }
+    }
+
+}

+ 8 - 1
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotGetForVersionResult.java

@@ -1,21 +1,28 @@
 package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
 
+import com.google.gson.Gson;
 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 java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
 /**
  * Created by Winston on 06/11/14.
  */
 public class SnapshotGetForVersionResult extends Result {
 
+    private SnapshotData snapshotData;
+
     public SnapshotGetForVersionResult(Request request, JsonElement json) {
         super(request, json);
     }
 
     @Override
     public void fromJSON(JsonElement json) {
-
+        snapshotData = new SnapshotData(json);
     }
 
 }

+ 57 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLAttachment.java

@@ -0,0 +1,57 @@
+package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.ning.http.client.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+/**
+ * Created by Winston on 06/11/14.
+ */
+public class WLAttachment extends WLFile {
+
+    private Future<byte[]> future;
+
+    public WLAttachment(JsonElement json) {
+        super(json);
+    }
+
+    @Override
+    public byte[] getContents() throws ExecutionException, InterruptedException {
+        return future.get();
+    }
+
+    @Override
+    protected void getContentsFromJSON(JsonArray jsonArray) {
+        try {
+            fetchContents(jsonArray.get(0).getAsString());
+        } catch (IOException e) {
+            throw new RuntimeException();
+        }
+    }
+
+    private void fetchContents(String url) throws IOException {
+        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
+        future = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
+
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+
+            @Override
+            public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
+                bytes.write(bodyPart.getBodyPartBytes());
+                return STATE.CONTINUE;
+            }
+
+            @Override
+            public byte[] onCompleted(Response response) throws Exception {
+                return bytes.toByteArray();
+            }
+
+        });
+    }
+
+}

+ 46 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLFile.java

@@ -0,0 +1,46 @@
+package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
+
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * Created by Winston on 06/11/14.
+ */
+public class WLFile implements JSONSource {
+
+    protected byte[] contents;
+    private String path;
+
+    public WLFile(JsonElement json) {
+        fromJSON(json);
+    }
+
+    @Override
+    public void fromJSON(JsonElement json) {
+        JsonArray jsonArray = json.getAsJsonArray();
+        getContentsFromJSON(jsonArray);
+        getPathFromJSON(jsonArray);
+    }
+
+    public byte[] getContents() throws ExecutionException, InterruptedException {
+        return contents;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    protected void getContentsFromJSON(JsonArray jsonArray) {
+        contents = jsonArray.get(0).getAsString().getBytes();
+    }
+
+    protected void getPathFromJSON(JsonArray jsonArray) {
+        path = jsonArray.get(1).getAsString();
+    }
+
+}

+ 14 - 3
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getsavedvers/SnapshotInfo.java

@@ -6,16 +6,27 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
 /**
  * Created by Winston on 06/11/14.
  */
-public class SnapshotInfo implements JSONSource {
+public class SnapshotInfo {
 
     private int versionId;
     private String comment;
     private WLUser user;
     private String createdAt;
 
-    @Override
-    public void fromJSON(JsonElement json) {
+    public int getVersionId() {
+        return versionId;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public WLUser getUser() {
+        return user;
+    }
 
+    public String getCreatedAt() {
+        return createdAt;
     }
 
 }

+ 6 - 3
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getsavedvers/WLUser.java

@@ -6,14 +6,17 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
 /**
  * Created by Winston on 06/11/14.
  */
-public class WLUser implements JSONSource {
+public class WLUser {
 
     private String name;
     private String email;
 
-    @Override
-    public void fromJSON(JsonElement json) {
+    public String getName() {
+        return name;
+    }
 
+    public String getEmail() {
+        return email;
     }
 
 }