Refactor with deletion and tests.

This commit is contained in:
Winston Li
2015-02-22 23:42:01 +00:00
parent 8993702f69
commit 5a00c84359
33 changed files with 189 additions and 212 deletions
@@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge;
import uk.ac.ic.wlgitbridge.application.WLGitBridgeApplication;
import uk.ac.ic.wlgitbridge.application.GitBridgeApp;
/**
* Created by Winston on 01/11/14.
@@ -8,7 +8,7 @@ import uk.ac.ic.wlgitbridge.application.WLGitBridgeApplication;
public class Main {
public static void main(String[] args) {
new WLGitBridgeApplication(args).run();
new GitBridgeApp(args).run();
}
}
@@ -4,7 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import uk.ac.ic.wlgitbridge.application.exception.InvalidConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.ConfigFileException;
import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
import java.io.FileReader;
@@ -23,7 +23,7 @@ public class Config implements JSONSource {
private String postbackURL;
private String serviceName;
public Config(String configFilePath) throws InvalidConfigFileException, IOException {
public Config(String configFilePath) throws ConfigFileException, IOException {
try {
fromJSON(new Gson().fromJson(new FileReader(configFilePath), JsonElement.class));
} catch (JsonParseException e) {
@@ -73,7 +73,7 @@ public class Config implements JSONSource {
private JsonElement getElement(JsonObject configObject, String name) {
JsonElement element = configObject.get(name);
if (element == null) {
throw new RuntimeException(new InvalidConfigFileException(name));
throw new RuntimeException(new ConfigFileException(name));
}
return element;
}
@@ -1,8 +1,9 @@
package uk.ac.ic.wlgitbridge.application;
import uk.ac.ic.wlgitbridge.application.exception.InvalidConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.InvalidProgramArgumentsException;
import uk.ac.ic.wlgitbridge.application.exception.ConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.ArgsException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.server.GitBridgeServer;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
@@ -15,27 +16,27 @@ import java.io.IOException;
/**
* Class that represents the application. Parses arguments and gives them to the server, or dies with a usage message.
*/
public class WLGitBridgeApplication {
public class GitBridgeApp implements Runnable {
public static final int EXIT_CODE_FAILED = 1;
private static final String USAGE_MESSAGE = "usage: writelatex-git-bridge config_file";
private String configFilePath;
private Config config;
private WLGitBridgeServer server;
private GitBridgeServer server;
/**
* Constructs an instance of the WriteLatex-Git Bridge application.
* @param args args from main, which should be in the format [config_file]
*/
public WLGitBridgeApplication(String[] args) {
public GitBridgeApp(String[] args) {
try {
parseArguments(args);
loadConfigFile();
} catch (InvalidProgramArgumentsException e) {
} catch (ArgsException e) {
printUsage();
System.exit(EXIT_CODE_FAILED);
} catch (InvalidConfigFileException e) {
} catch (ConfigFileException e) {
System.err.println("The property for " + e.getMissingMember() + " is invalid. Check your config file.");
System.exit(EXIT_CODE_FAILED);
} catch (IOException e) {
@@ -43,7 +44,7 @@ public class WLGitBridgeApplication {
System.exit(EXIT_CODE_FAILED);
}
try {
server = new WLGitBridgeServer(config);
server = new GitBridgeServer(config);
} catch (ServletException e) {
Util.printStackTrace(e);
} catch (InvalidRootDirectoryPathException e) {
@@ -55,6 +56,7 @@ public class WLGitBridgeApplication {
/**
* Starts the server with the port number and root directory path given in the command-line arguments.
*/
@Override
public void run() {
server.start();
}
@@ -65,22 +67,22 @@ public class WLGitBridgeApplication {
/* Helper methods */
private void parseArguments(String[] args) throws InvalidProgramArgumentsException {
private void parseArguments(String[] args) throws ArgsException {
checkArgumentsLength(args);
parseConfigFilePath(args);
}
private void checkArgumentsLength(String[] args) throws InvalidProgramArgumentsException {
private void checkArgumentsLength(String[] args) throws ArgsException {
if (args.length < 1) {
throw new InvalidProgramArgumentsException();
throw new ArgsException();
}
}
private void parseConfigFilePath(String[] args) throws InvalidProgramArgumentsException {
private void parseConfigFilePath(String[] args) throws ArgsException {
configFilePath = args[0];
}
private void loadConfigFile() throws InvalidConfigFileException, IOException {
private void loadConfigFile() throws ConfigFileException, IOException {
config = new Config(configFilePath);
}
@@ -3,5 +3,5 @@ package uk.ac.ic.wlgitbridge.application.exception;
/**
* Created by Winston on 03/11/14.
*/
public class InvalidProgramArgumentsException extends Throwable {
public class ArgsException extends Exception {
}
@@ -3,12 +3,11 @@ package uk.ac.ic.wlgitbridge.application.exception;
/**
* Created by Winston on 05/12/14.
*/
public class InvalidConfigFileException extends Exception {
public class ConfigFileException extends Exception {
private final String missingMember;
public InvalidConfigFileException(String missingMember) {
public ConfigFileException(String missingMember) {
this.missingMember = missingMember;
}
@@ -9,11 +9,11 @@ import uk.ac.ic.wlgitbridge.data.ShutdownHook;
import uk.ac.ic.wlgitbridge.data.model.DataStore;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.snapshot.push.PostbackManager;
import uk.ac.ic.wlgitbridge.snapshot.push.SnapshotPushRequest;
import uk.ac.ic.wlgitbridge.snapshot.push.SnapshotPushRequestResult;
import uk.ac.ic.wlgitbridge.snapshot.push.PushRequest;
import uk.ac.ic.wlgitbridge.snapshot.push.PushResult;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.*;
import uk.ac.ic.wlgitbridge.util.Util;
@@ -24,12 +24,12 @@ import java.io.IOException;
*/
public class BridgeAPI {
private final DataStore dataModel;
private final DataStore dataStore;
private final PostbackManager postbackManager;
private final ProjectLock mainProjectLock;
public BridgeAPI(DataStore dataModel) {
this.dataModel = dataModel;
public BridgeAPI(String rootGitDirectoryPath) {
dataStore = new DataStore(rootGitDirectoryPath);
postbackManager = new PostbackManager();
mainProjectLock = new ProjectLock();
Runtime.getRuntime().addShutdownHook(new ShutdownHook(mainProjectLock));
@@ -45,10 +45,10 @@ public class BridgeAPI {
public boolean repositoryExists(String projectName) throws ServiceMayNotContinueException {
lockForProject(projectName);
SnapshotGetDocRequest snapshotGetDocRequest = new SnapshotGetDocRequest(projectName);
snapshotGetDocRequest.request();
GetDocRequest getDocRequest = new GetDocRequest(projectName);
getDocRequest.request();
try {
snapshotGetDocRequest.getResult().getVersionID();
getDocRequest.getResult().getVersionID();
} catch (InvalidProjectException e) {
return false;
} catch (FailedConnectionException e) {
@@ -63,7 +63,7 @@ public class BridgeAPI {
public void getWritableRepositories(String projectName, Repository repository) throws IOException, SnapshotPostException, GitAPIException {
Util.sout("Fetching project: " + projectName);
dataModel.updateProjectWithName(projectName, repository);
dataStore.updateProjectWithName(projectName, repository);
}
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents, String hostname) throws SnapshotPostException, IOException {
@@ -72,12 +72,12 @@ public class BridgeAPI {
try {
Util.sout("Pushing project: " + projectName);
String postbackKey = postbackManager.makeKeyForProject(projectName);
candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, oldDirectoryContents);
SnapshotPushRequest snapshotPushRequest = new SnapshotPushRequest(candidate, postbackKey);
snapshotPushRequest.request();
SnapshotPushRequestResult result = snapshotPushRequest.getResult();
candidate = dataStore.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, oldDirectoryContents);
PushRequest pushRequest = new PushRequest(candidate, postbackKey);
pushRequest.request();
PushResult result = pushRequest.getResult();
if (result.wasSuccessful()) {
dataModel.approveSnapshot(postbackManager.getVersionID(projectName), candidate);
dataStore.approveSnapshot(postbackManager.getVersionID(projectName), candidate);
} else {
throw new OutOfDateException();
}
@@ -8,12 +8,10 @@ import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
public class ServletFile extends RawFile {
private final RawFile file;
private final RawFile oldFile;
private final boolean changed;
public ServletFile(RawFile file, RawFile oldFile) {
this.file = file;
this.oldFile = oldFile;
changed = !equals(oldFile);
}
@@ -1,11 +1,11 @@
package uk.ac.ic.wlgitbridge.data;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.SnapshotGetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotGetSavedVersRequest;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.GetForVersionRequest;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.GetSavedVersRequest;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotInfo;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.data.model.Snapshot;
@@ -26,11 +26,11 @@ public class SnapshotFetcher {
private List<SnapshotInfo> getSnapshotInfosAfterVersion(String projectName, int version) throws FailedConnectionException, SnapshotPostException {
SortedSet<SnapshotInfo> versions = new TreeSet<SnapshotInfo>();
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName);
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);
GetDocRequest getDoc = new GetDocRequest(projectName);
GetSavedVersRequest getSavedVers = new GetSavedVersRequest(projectName);
getDoc.request();
getSavedVers.request();
SnapshotGetDocResult latestDoc = getDoc.getResult();
GetDocResult latestDoc = getDoc.getResult();
int latest = latestDoc.getVersionID();
if (latest > version) {
for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
@@ -45,18 +45,18 @@ public class SnapshotFetcher {
}
private List<SnapshotData> getMatchingSnapshotData(String projectName, List<SnapshotInfo> snapshotInfos) throws FailedConnectionException {
List<SnapshotGetForVersionRequest> firedRequests = fireDataRequests(projectName, snapshotInfos);
List<GetForVersionRequest> firedRequests = fireDataRequests(projectName, snapshotInfos);
List<SnapshotData> snapshotDataList = new LinkedList<SnapshotData>();
for (SnapshotGetForVersionRequest fired : firedRequests) {
for (GetForVersionRequest fired : firedRequests) {
snapshotDataList.add(fired.getResult().getSnapshotData());
}
return snapshotDataList;
}
private List<SnapshotGetForVersionRequest> fireDataRequests(String projectName, List<SnapshotInfo> snapshotInfos) {
List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
private List<GetForVersionRequest> fireDataRequests(String projectName, List<SnapshotInfo> snapshotInfos) {
List<GetForVersionRequest> requests = new LinkedList<GetForVersionRequest>();
for (SnapshotInfo snapshotInfo : snapshotInfos) {
SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(projectName, snapshotInfo.getVersionId());
GetForVersionRequest request = new GetForVersionRequest(projectName, snapshotInfo.getVersionId());
requests.add(request);
request.request();
}
@@ -6,6 +6,7 @@ import com.ning.http.client.Response;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
import uk.ac.ic.wlgitbridge.data.model.db.PersistentStore;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
@@ -20,14 +21,14 @@ import java.util.concurrent.ExecutionException;
*/
public class ResourceFetcher {
private final URLIndexStore urlIndexStore;
private final PersistentStore persistentStore;
public ResourceFetcher(URLIndexStore urlIndexStore) {
this.urlIndexStore = urlIndexStore;
public ResourceFetcher(PersistentStore persistentStore) {
this.persistentStore = persistentStore;
}
public RawFile get(String projectName, String url, String newPath, Repository repository) throws IOException {
String path = urlIndexStore.getPathForURLInProject(projectName, url);
String path = persistentStore.getPathForURLInProject(projectName, url);
byte[] contents;
if (path == null) {
path = newPath;
@@ -70,7 +71,7 @@ public class ResourceFetcher {
Util.printStackTrace(e);
throw new FailedConnectionException();
}
urlIndexStore.addURLIndexForProject(projectName, url, path);
persistentStore.addURLIndexForProject(projectName, url, path);
return contents;
}
@@ -1,14 +1,14 @@
package uk.ac.ic.wlgitbridge.data.model;
import org.joda.time.DateTime;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotAttachment;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotFile;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotInfo;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.WLUser;
import java.util.*;
import java.util.Date;
import java.util.List;
/**
* Created by Winston on 03/11/14.
@@ -36,17 +36,6 @@ public class Snapshot implements Comparable<Snapshot> {
atts = data.getAtts();
}
public Snapshot(int versionID) {
this.versionID = versionID;
comment = "Most recent update";
userName = "Anonymous";
userEmail = "anonymous@" + Util.getServiceName() + ".com";
createdAt = new Date();
srcs = new LinkedList<SnapshotFile>();
atts = new LinkedList<SnapshotAttachment>();
}
public int getVersionID() {
return versionID;
}
@@ -1,11 +0,0 @@
package uk.ac.ic.wlgitbridge.data.model;
/**
* Created by Winston on 21/02/15.
*/
public interface URLIndexStore {
public void addURLIndexForProject(String projectName, String url, String path);
public String getPathForURLInProject(String projectName, String url);
}
@@ -1,6 +1,5 @@
package uk.ac.ic.wlgitbridge.data.model.db;
import uk.ac.ic.wlgitbridge.data.model.URLIndexStore;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLiteWLDatabase;
import java.io.File;
@@ -10,7 +9,7 @@ import java.util.List;
/**
* Created by Winston on 19/11/14.
*/
public class PersistentStore implements URLIndexStore {
public class PersistentStore {
private final SQLiteWLDatabase database;
@@ -47,7 +46,6 @@ public class PersistentStore implements URLIndexStore {
}
}
@Override
public void addURLIndexForProject(String projectName, String url, String path) {
try {
database.addURLIndex(projectName, url, path);
@@ -63,8 +61,6 @@ public class PersistentStore implements URLIndexStore {
throw new RuntimeException(e);
}
}
@Override
public String getPathForURLInProject(String projectName, String url) {
try {
return database.getPathForURLInProject(projectName, url);
@@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
@@ -16,11 +16,11 @@ import java.io.IOException;
/**
* Created by Winston on 04/12/14.
*/
public class AttsResourceHandler extends ResourceHandler {
public class FileServlet extends ResourceHandler {
private final BridgeAPI writeLatexDataSource;
public AttsResourceHandler(BridgeAPI writeLatexDataSource) {
public FileServlet(BridgeAPI writeLatexDataSource) {
this.writeLatexDataSource = writeLatexDataSource;
}
@@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
@@ -7,13 +7,13 @@ 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.Config;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.snapshot.base.SnapshotAPIRequest;
import uk.ac.ic.wlgitbridge.data.model.DataStore;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
import java.io.File;
@@ -26,12 +26,15 @@ import java.net.BindException;
/**
* Class for the actual server.
*/
public class WLGitBridgeServer {
public class GitBridgeServer {
private final BridgeAPI bridgeAPI;
private final Server jettyServer;
private final int port;
private String rootGitDirectoryPath;
private String writeLatexHostname;
private String apiBaseURL;
/**
* Constructs an instance of the server.
@@ -39,19 +42,20 @@ public class WLGitBridgeServer {
* @param rootGitDirectoryPath the root directory path containing the git repositories
* @throws ServletException if the servlet throws an exception
*/
private WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
private GitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
Log.setLog(new NullLogger());
this.port = port;
this.rootGitDirectoryPath = rootGitDirectoryPath;
Log.setLog(new NullLogger());
bridgeAPI = new BridgeAPI(rootGitDirectoryPath);
jettyServer = new Server(port);
configureJettyServer();
}
public WLGitBridgeServer(Config config) throws ServletException, InvalidRootDirectoryPathException {
public GitBridgeServer(Config config) throws ServletException, InvalidRootDirectoryPathException {
this(config.getPort(), config.getRootGitDirectory());
SnapshotAPIRequest.setBasicAuth(config.getUsername(), config.getPassword());
writeLatexHostname = config.getAPIBaseURL();
SnapshotAPIRequest.setBaseURL(writeLatexHostname);
apiBaseURL = config.getAPIBaseURL();
SnapshotAPIRequest.setBaseURL(apiBaseURL);
Util.setServiceName(config.getServiceName());
Util.setPostbackURL(config.getPostbackURL());
Util.setPort(config.getPort());
@@ -65,7 +69,7 @@ public class WLGitBridgeServer {
jettyServer.start();
Util.sout(Util.getServiceName() + "-Git Bridge server started");
Util.sout("Listening on port: " + port);
Util.sout("Bridged to: " + writeLatexHostname);
Util.sout("Bridged to: " + apiBaseURL);
Util.sout("Postback base URL: " + Util.getPostbackURL());
Util.sout("Root git directory path: " + rootGitDirectoryPath);
} catch (BindException e) {
@@ -85,16 +89,15 @@ public class WLGitBridgeServer {
private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException {
HandlerCollection handlers = new HandlerCollection();
BridgeAPI writeLatexDataSource = new BridgeAPI(new DataStore(rootGitDirectoryPath));
handlers.setHandlers(new Handler[] {
initResourceHandler(writeLatexDataSource),
new SnapshotPushPostbackHandler(writeLatexDataSource),
initGitHandler(writeLatexDataSource)
initResourceHandler(),
new PostbackHandler(bridgeAPI),
initGitHandler()
});
jettyServer.setHandler(handlers);
}
private Handler initGitHandler(BridgeAPI bridgeAPI) throws ServletException, InvalidRootDirectoryPathException {
private Handler initGitHandler() throws ServletException, InvalidRootDirectoryPathException {
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(
@@ -105,8 +108,8 @@ public class WLGitBridgeServer {
return servletContextHandler;
}
private Handler initResourceHandler(BridgeAPI writeLatexDataSource) {
ResourceHandler resourceHandler = new AttsResourceHandler(writeLatexDataSource);
private Handler initResourceHandler() {
ResourceHandler resourceHandler = new FileServlet(bridgeAPI);
resourceHandler.setResourceBase(new File(rootGitDirectoryPath, ".wlgb/atts").getAbsolutePath());
return resourceHandler;
}
@@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.server;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
@@ -12,7 +12,7 @@ import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostExceptionBuilder
/**
* Created by Winston on 17/11/14.
*/
public class SnapshotPushPostbackContents implements JSONSource {
public class PostbackContents implements JSONSource {
private static final String CODE_SUCCESS = "upToDate";
@@ -25,7 +25,7 @@ public class SnapshotPushPostbackContents implements JSONSource {
private int versionID;
private SnapshotPostException exception;
public SnapshotPushPostbackContents(BridgeAPI bridgeAPI, String projectName, String postbackKey, String contents) {
public PostbackContents(BridgeAPI bridgeAPI, String projectName, String postbackKey, String contents) {
this.bridgeAPI = bridgeAPI;
this.projectName = projectName;
this.postbackKey = postbackKey;
@@ -1,12 +1,12 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.server;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -16,11 +16,11 @@ import java.io.IOException;
/**
* Created by Winston on 16/11/14.
*/
public class SnapshotPushPostbackHandler extends AbstractHandler {
public class PostbackHandler extends AbstractHandler {
private final BridgeAPI bridgeAPI;
public SnapshotPushPostbackHandler(BridgeAPI bridgeAPI) {
public PostbackHandler(BridgeAPI bridgeAPI) {
this.bridgeAPI = bridgeAPI;
}
@@ -38,7 +38,7 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
String projectName = parts[1];
String postbackKey = parts[2];
Util.sout(baseRequest.getMethod() + " <- " + baseRequest.getUri());
SnapshotPushPostbackContents postbackContents = new SnapshotPushPostbackContents(bridgeAPI, projectName, postbackKey, contents);
PostbackContents postbackContents = new PostbackContents(bridgeAPI, projectName, postbackKey, contents);
JsonObject body = new JsonObject();
try {
@@ -8,11 +8,11 @@ import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResult> {
public class GetDocRequest extends SnapshotAPIRequest<GetDocResult> {
public static final String API_CALL = "";
public SnapshotGetDocRequest(String projectName) {
public GetDocRequest(String projectName) {
super(projectName, API_CALL);
}
@@ -22,8 +22,8 @@ public class SnapshotGetDocRequest extends SnapshotAPIRequest<SnapshotGetDocResu
}
@Override
protected SnapshotGetDocResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetDocResult(this, json);
protected GetDocResult parseResponse(JsonElement json) throws FailedConnectionException {
return new GetDocResult(this, json);
}
}
@@ -12,7 +12,7 @@ import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.ProtectedProjectException;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetDocResult extends Result {
public class GetDocResult extends Result {
private int error;
private int versionID;
@@ -22,11 +22,11 @@ public class SnapshotGetDocResult extends Result {
private SnapshotPostException exception;
public SnapshotGetDocResult(Request request, JsonElement json) throws FailedConnectionException {
public GetDocResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json);
}
public SnapshotGetDocResult(JsonElement error, int versionID, String createdAt, String email, String name) {
public GetDocResult(JsonElement error, int versionID, String createdAt, String email, String name) {
if (error == null) {
this.error = -1;
} else {
@@ -8,13 +8,13 @@ import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGetForVersionResult> {
public class GetForVersionRequest extends SnapshotAPIRequest<GetForVersionResult> {
public static final String API_CALL = "/snapshots";
private int versionID;
public SnapshotGetForVersionRequest(String projectName, int versionID) {
public GetForVersionRequest(String projectName, int versionID) {
super(projectName, API_CALL + "/" + versionID);
this.versionID = versionID;
}
@@ -25,8 +25,8 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
}
@Override
protected SnapshotGetForVersionResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetForVersionResult(this, json);
protected GetForVersionResult parseResponse(JsonElement json) throws FailedConnectionException {
return new GetForVersionResult(this, json);
}
public int getVersionID() {
@@ -7,15 +7,15 @@ import uk.ac.ic.wlgitbridge.snapshot.base.Result;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetForVersionResult extends Result {
public class GetForVersionResult extends Result {
private SnapshotData snapshotData;
public SnapshotGetForVersionResult(Request request, JsonElement json) {
public GetForVersionResult(Request request, JsonElement json) {
super(request, json);
}
public SnapshotGetForVersionResult(SnapshotData snapshotData) {
public GetForVersionResult(SnapshotData snapshotData) {
this.snapshotData = snapshotData;
}
@@ -8,11 +8,11 @@ import uk.ac.ic.wlgitbridge.snapshot.base.HTTPMethod;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetSavedVersResult> {
public class GetSavedVersRequest extends SnapshotAPIRequest<GetSavedVersResult> {
public static final String API_CALL = "/saved_vers";
public SnapshotGetSavedVersRequest(String projectName) {
public GetSavedVersRequest(String projectName) {
super(projectName, API_CALL);
}
@@ -22,8 +22,8 @@ public class SnapshotGetSavedVersRequest extends SnapshotAPIRequest<SnapshotGetS
}
@Override
protected SnapshotGetSavedVersResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotGetSavedVersResult(this, json);
protected GetSavedVersResult parseResponse(JsonElement json) throws FailedConnectionException {
return new GetSavedVersResult(this, json);
}
}
@@ -14,15 +14,15 @@ import java.util.List;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetSavedVersResult extends Result {
public class GetSavedVersResult extends Result {
private List<SnapshotInfo> savedVers;
public SnapshotGetSavedVersResult(Request request, JsonElement json) throws FailedConnectionException {
public GetSavedVersResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json);
}
public SnapshotGetSavedVersResult(List<SnapshotInfo> savedVers) {
public GetSavedVersResult(List<SnapshotInfo> savedVers) {
this.savedVers = savedVers;
}
@@ -15,11 +15,11 @@ import java.util.Map;
public class PostbackManager {
private final SecureRandom random;
private final Map<String, PostbackContents> postbackContentsTable;
private final Map<String, PostbackPromise> postbackContentsTable;
public PostbackManager() {
random = new SecureRandom();
postbackContentsTable = new HashMap<String, PostbackContents>();
postbackContentsTable = new HashMap<String, PostbackPromise>();
}
public int getVersionID(String projectName) throws SnapshotPostException {
@@ -40,8 +40,8 @@ public class PostbackManager {
getPostbackForProject(projectName).receivedException(exception, postbackKey);
}
private PostbackContents getPostbackForProject(String projectName) throws UnexpectedPostbackException {
PostbackContents contents = postbackContentsTable.remove(projectName);
private PostbackPromise getPostbackForProject(String projectName) throws UnexpectedPostbackException {
PostbackPromise contents = postbackContentsTable.remove(projectName);
if (contents == null) {
throw new UnexpectedPostbackException();
}
@@ -50,7 +50,7 @@ public class PostbackManager {
public String makeKeyForProject(String projectName) {
String key = System.currentTimeMillis() + randomString();
PostbackContents contents = new PostbackContents(key);
PostbackPromise contents = new PostbackPromise(key);
postbackContentsTable.put(projectName, contents);
return key;
}
@@ -12,7 +12,7 @@ import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Winston on 17/11/14.
*/
public class PostbackContents {
public class PostbackPromise {
private final String postbackKey;
private final ReentrantLock lock;
@@ -22,7 +22,7 @@ public class PostbackContents {
private int versionID;
private SnapshotPostException exception;
public PostbackContents(String postbackKey) {
public PostbackPromise(String postbackKey) {
this.postbackKey = postbackKey;
lock = new ReentrantLock();
cond = lock.newCondition();
@@ -9,14 +9,14 @@ import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
/**
* Created by Winston on 16/11/14.
*/
public class SnapshotPushRequest extends SnapshotAPIRequest<SnapshotPushRequestResult> {
public class PushRequest extends SnapshotAPIRequest<PushResult> {
private static final String API_CALL = "/snapshots";
private final CandidateSnapshot candidateSnapshot;
private final String postbackKey;
public SnapshotPushRequest(CandidateSnapshot candidateSnapshot, String postbackKey) {
public PushRequest(CandidateSnapshot candidateSnapshot, String postbackKey) {
super(candidateSnapshot.getProjectName(), API_CALL);
this.candidateSnapshot = candidateSnapshot;
this.postbackKey = postbackKey;
@@ -33,8 +33,8 @@ public class SnapshotPushRequest extends SnapshotAPIRequest<SnapshotPushRequestR
}
@Override
protected SnapshotPushRequestResult parseResponse(JsonElement json) throws FailedConnectionException {
return new SnapshotPushRequestResult(this, json);
protected PushResult parseResponse(JsonElement json) throws FailedConnectionException {
return new PushResult(this, json);
}
}
@@ -9,11 +9,11 @@ import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
/**
* Created by Winston on 16/11/14.
*/
public class SnapshotPushRequestResult extends Result {
public class PushResult extends Result {
private boolean success;
public SnapshotPushRequestResult(Request request, JsonElement json) throws FailedConnectionException {
public PushResult(Request request, JsonElement json) throws FailedConnectionException {
super(request, json);
}
@@ -1,16 +1,16 @@
package uk.ac.ic.wlgitbridge.snapshot.servermock.response.getdoc;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponse;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.SnapshotGetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult;
/**
* Created by Winston on 09/01/15.
*/
public class SnapshotGetDocResponse extends SnapshotResponse {
private final SnapshotGetDocResult state;
private final GetDocResult state;
public SnapshotGetDocResponse(SnapshotGetDocResult state) {
public SnapshotGetDocResponse(GetDocResult state) {
this.state = state;
}
@@ -1,16 +1,16 @@
package uk.ac.ic.wlgitbridge.snapshot.servermock.response.getforver;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponse;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotGetForVersionResult;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.GetForVersionResult;
/**
* Created by Winston on 09/01/15.
*/
public class SnapshotGetForVerResponse extends SnapshotResponse {
private final SnapshotGetForVersionResult state;
private final GetForVersionResult state;
public SnapshotGetForVerResponse(SnapshotGetForVersionResult state) {
public SnapshotGetForVerResponse(GetForVersionResult state) {
this.state = state;
}
@@ -1,16 +1,16 @@
package uk.ac.ic.wlgitbridge.snapshot.servermock.response.getsavedver;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponse;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotGetSavedVersResult;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.GetSavedVersResult;
/**
* Created by Winston on 09/01/15.
*/
public class SnapshotGetSavedVersResponse extends SnapshotResponse {
private final SnapshotGetSavedVersResult state;
private final GetSavedVersResult state;
public SnapshotGetSavedVersResponse(SnapshotGetSavedVersResult state) {
public SnapshotGetSavedVersResponse(GetSavedVersResult state) {
this.state = state;
}
File diff suppressed because one or more lines are too long
@@ -9,12 +9,12 @@ import uk.ac.ic.wlgitbridge.snapshot.servermock.response.push.data.SnapshotPushR
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.push.data.SnapshotPushResultSuccess;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.push.postback.*;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.push.postback.invalidfile.InvalidFileError;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.SnapshotGetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotAttachment;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotFile;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotGetForVersionResult;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotGetSavedVersResult;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.GetForVersionResult;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.GetSavedVersResult;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotInfo;
import java.io.InputStream;
@@ -31,9 +31,9 @@ public class SnapshotAPIStateBuilder {
private final JsonArray projects;
private Map<String, SnapshotGetDocResult> getDoc = new HashMap<String, SnapshotGetDocResult>();
private Map<String, SnapshotGetSavedVersResult> getSavedVers = new HashMap<String, SnapshotGetSavedVersResult>();
private Map<String, Map<Integer, SnapshotGetForVersionResult>> getForVers = new HashMap<String, Map<Integer, SnapshotGetForVersionResult>>();
private Map<String, GetDocResult> getDoc = new HashMap<String, GetDocResult>();
private Map<String, GetSavedVersResult> getSavedVers = new HashMap<String, GetSavedVersResult>();
private Map<String, Map<Integer, GetForVersionResult>> getForVers = new HashMap<String, Map<Integer, GetForVersionResult>>();
private Map<String, SnapshotPushResult> push = new HashMap<String, SnapshotPushResult>();
private Map<String, SnapshotPostbackRequest> postback = new HashMap<String, SnapshotPostbackRequest>();
@@ -59,7 +59,7 @@ public class SnapshotAPIStateBuilder {
private void addGetDocForProject(String projectName, JsonObject jsonGetDoc) {
getDoc.put(projectName,
new SnapshotGetDocResult(jsonGetDoc.get("error"),
new GetDocResult(jsonGetDoc.get("error"),
jsonGetDoc.get("versionID").getAsInt(),
jsonGetDoc.get("createdAt").getAsString(),
jsonGetDoc.get("email").getAsString(),
@@ -71,7 +71,7 @@ public class SnapshotAPIStateBuilder {
for (JsonElement ver : jsonGetSavedVers) {
savedVers.add(getSnapshotInfo(ver.getAsJsonObject()));
}
getSavedVers.put(projectName, new SnapshotGetSavedVersResult(savedVers));
getSavedVers.put(projectName, new GetSavedVersResult(savedVers));
}
private SnapshotInfo getSnapshotInfo(JsonObject jsonSnapshotInfo) {
@@ -83,11 +83,11 @@ public class SnapshotAPIStateBuilder {
}
private void addGetForVersForProject(String projectName, JsonArray jsonGetForVers) {
Map<Integer, SnapshotGetForVersionResult> forVers = new HashMap<Integer, SnapshotGetForVersionResult>();
Map<Integer, GetForVersionResult> forVers = new HashMap<Integer, GetForVersionResult>();
for (JsonElement forVer : jsonGetForVers) {
JsonObject forVerObj = forVer.getAsJsonObject();
forVers.put(forVerObj.get("versionID").getAsInt(),
new SnapshotGetForVersionResult(new SnapshotData(getSrcs(forVerObj.get("srcs").getAsJsonArray()),
new GetForVersionResult(new SnapshotData(getSrcs(forVerObj.get("srcs").getAsJsonArray()),
getAtts(forVerObj.get("atts").getAsJsonArray()))));
}
getForVers.put(projectName, forVers);