Added POST support to Request class.

This commit is contained in:
Winston Li 2014-11-16 18:56:00 +00:00
parent f96a29bc45
commit 05d222ab00
12 changed files with 178 additions and 35 deletions

View file

@ -9,7 +9,9 @@ import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode;
public interface CandidateSnapshot {
public JsonElement getJsonRepresentation();
public int getPreviousVersionID();
public void approveWithVersionID(int versionID);
public String getProjectName();
public WLDirectoryNode getDirectoryNode();
}

View file

@ -0,0 +1,10 @@
package uk.ac.ic.wlgitbridge.bridge;
/**
* Created by Winston on 16/11/14.
*/
public interface CandidateSnapshotCallback {
public void approveSnapshot(int versionID, CandidateSnapshot candidateSnapshot);
}

View file

@ -11,8 +11,13 @@ import java.util.List;
*/
public interface WriteLatexDataSource {
/* Called by request thread. */
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;
public void expectPostback(String projectName);
/* Called by postback thread. */
public void postbackReceived(String projectName);
}

View file

@ -0,0 +1,51 @@
package uk.ac.ic.wlgitbridge.writelatex;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode;
import uk.ac.ic.wlgitbridge.writelatex.model.WLProject;
/**
* Created by Winston on 16/11/14.
*/
public class WLDirectoryNodeSnapshot implements CandidateSnapshot {
private final int previousVersionID;
private final String projectName;
private final WLDirectoryNode directoryNode;
private final CandidateSnapshotCallback callback;
public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, CandidateSnapshotCallback callback) {
previousVersionID = project.getLatestSnapshot().getVersionID();
projectName = project.getName();
this.directoryNode = directoryNode;
this.callback = callback;
}
@Override
public JsonElement getJsonRepresentation() {
return null;
}
@Override
public int getPreviousVersionID() {
return previousVersionID;
}
@Override
public void approveWithVersionID(int versionID) {
callback.approveSnapshot(versionID, this);
}
@Override
public String getProjectName() {
return projectName;
}
@Override
public WLDirectoryNode getDirectoryNode() {
return directoryNode;
}
}

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex;
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.bridge.WriteLatexDataSource;
@ -8,6 +9,7 @@ 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.model.WLDataModel;
import java.util.Arrays;
import java.util.List;
/**
@ -40,7 +42,30 @@ public class WriteLatexAPI implements WriteLatexDataSource {
@Override
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException {
dataModel.put(projectName, directoryContents);
CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents);
throw new SnapshotPostException() {
@Override
public String getMessage() {
return "unimplemented";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList("Currently implemented");
}
};
}
@Override
public void expectPostback(String projectName) {
}
/* Called by postback thread. */
@Override
public void postbackReceived(String projectName) {
}
}

View file

@ -0,0 +1,11 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.base;
/**
* Created by Winston on 16/11/14.
*/
public enum HTTPMethod {
POST,
GET
}

View file

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.Realm;
import com.ning.http.client.Response;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
@ -27,13 +28,51 @@ public abstract class Request<T extends Result> {
error = false;
}
public void request() {
switch (httpMethod()) {
case GET:
performGetRequest();
break;
case POST:
performPostRequest();
break;
default:
break;
}
}
public T getResult() throws FailedConnectionException {
if (error) {
throw new FailedConnectionException();
}
try {
return future.get();
} catch (InterruptedException e) {
throw new FailedConnectionException();
} catch (ExecutionException e) {
throw new FailedConnectionException();
}
}
protected abstract HTTPMethod httpMethod();
protected abstract Realm buildRequestRealm();
protected abstract T parseResponse(JsonElement json) throws FailedConnectionException;
public void request() {
AsyncHttpClient client = new AsyncHttpClient();
protected String getPostBody() {
return "";
}
private void performGetRequest() {
request(new AsyncHttpClient().prepareGet(url));
}
private void performPostRequest() {
request(new AsyncHttpClient().preparePost(url).setBody(getPostBody()));
}
private void request(BoundRequestBuilder boundRequestBuilder) {
try {
future = client.prepareGet(url).setRealm(buildRequestRealm()).execute(new AsyncCompletionHandler<T>() {
future = boundRequestBuilder.setRealm(buildRequestRealm()).execute(new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
@ -51,17 +90,4 @@ public abstract class Request<T extends Result> {
}
}
public T getResult() throws FailedConnectionException {
if (error) {
throw new FailedConnectionException();
}
try {
return future.get();
} catch (InterruptedException e) {
throw new FailedConnectionException();
} catch (ExecutionException e) {
throw new FailedConnectionException();
}
}
}

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc;
import com.google.gson.JsonElement;
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;
@ -15,6 +16,11 @@ public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResu
super(projectName, API_CALL);
}
@Override
protected HTTPMethod httpMethod() {
return HTTPMethod.GET;
}
@Override
protected SnapshotGetDocResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetDocResult(this, json);

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion;
import com.google.gson.JsonElement;
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;
@ -18,6 +19,11 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
this.versionID = versionID;
}
@Override
protected HTTPMethod httpMethod() {
return HTTPMethod.GET;
}
@Override
protected SnapshotGetForVersionResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetForVersionResult(this, json);

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.JsonElement;
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;
@ -15,6 +16,11 @@ public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetS
super(projectName, API_CALL);
}
@Override
protected HTTPMethod httpMethod() {
return HTTPMethod.GET;
}
@Override
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetSavedVersResult(this, json);

View file

@ -51,7 +51,7 @@ public class WLFileStore {
return writableRepositories;
}
public WLDirectoryNode createCandidateDirectoryNodeForProjectWithContents(WLProject project, RawDirectoryContents directoryContents) {
public WLDirectoryNode createNextDirectoryNodeInProjectFromContents(WLProject project, RawDirectoryContents directoryContents) {
return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents);
}

View file

@ -1,14 +1,15 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.WLDirectoryNodeSnapshot;
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.filestore.node.WLDirectoryNode;
import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -16,7 +17,7 @@ import java.util.Map;
/**
* Created by Winston on 06/11/14.
*/
public class WLDataModel {
public class WLDataModel implements CandidateSnapshotCallback {
private final Map<String, WLProject> projects;
private final WLFileStore fileStore;
@ -41,22 +42,16 @@ public class WLDataModel {
return project;
}
public void put(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() {
public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException {
return new WLDirectoryNodeSnapshot(getProjectWithName(projectName),
fileStore.createNextDirectoryNodeInProjectFromContents(getProjectWithName(projectName),
directoryContents),
this);
}
@Override
public String getMessage() {
return "unimplemented";
}
@Override
public void approveSnapshot(int versionID, CandidateSnapshot candidateSnapshot) {
@Override
public List<String> getDescriptionLines() {
return Arrays.asList("Not currently implemented");
}
};
}
}