Made username and password optional and added serviceName member to config file.

This commit is contained in:
Winston Li 2014-12-06 00:45:18 +00:00
parent 8b700a8c0c
commit f39199fa7e
10 changed files with 50 additions and 11 deletions

View file

@ -4,5 +4,6 @@
"apiKey": "", "apiKey": "",
"hostname": "https://radiant-wind-3058.herokuapp.com/", "hostname": "https://radiant-wind-3058.herokuapp.com/",
"username": "staging", "username": "staging",
"password": "6kUfbv0R" "password": "6kUfbv0R",
"serviceName": "WriteLatex"
} }

View file

@ -21,6 +21,7 @@ public class Config implements JSONSource {
private String username; private String username;
private String password; private String password;
private String hostname; private String hostname;
private String serviceName;
public Config(String configFilePath) throws InvalidConfigFileException, IOException { public Config(String configFilePath) throws InvalidConfigFileException, IOException {
try { try {
@ -36,9 +37,10 @@ public class Config implements JSONSource {
port = getElement(configObject, "port").getAsInt(); port = getElement(configObject, "port").getAsInt();
rootGitDirectory = getElement(configObject, "rootGitDirectory").getAsString(); rootGitDirectory = getElement(configObject, "rootGitDirectory").getAsString();
apiKey = getElement(configObject, "apiKey").getAsString(); apiKey = getElement(configObject, "apiKey").getAsString();
username = getElement(configObject, "username").getAsString(); username = getOptionalString(configObject, "username");
password = getElement(configObject, "password").getAsString(); password = getOptionalString(configObject, "password");
hostname = getElement(configObject, "hostname").getAsString(); hostname = getElement(configObject, "hostname").getAsString();
serviceName = getElement(configObject, "serviceName").getAsString();
} }
public int getPort() { public int getPort() {
@ -73,4 +75,16 @@ public class Config implements JSONSource {
return element; return element;
} }
private String getOptionalString(JsonObject configObject, String name) {
JsonElement element = configObject.get(name);
if (element == null || !element.isJsonPrimitive()) {
return "";
}
return element.getAsString();
}
public String getServiceName() {
return serviceName;
}
} }

View file

@ -11,6 +11,7 @@ import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource; import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException; import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet; import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI; import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel; import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
@ -31,6 +32,7 @@ public class WLGitBridgeServer {
private final Server jettyServer; private final Server jettyServer;
private final int port; private final int port;
private String rootGitDirectoryPath; private String rootGitDirectoryPath;
private String writeLatexHostname;
/** /**
* Constructs an instance of the server. * Constructs an instance of the server.
@ -50,7 +52,9 @@ public class WLGitBridgeServer {
public WLGitBridgeServer(Config config) throws ServletException, InvalidRootDirectoryPathException { public WLGitBridgeServer(Config config) throws ServletException, InvalidRootDirectoryPathException {
this(config.getPort(), config.getRootGitDirectory(), config.getAPIKey()); this(config.getPort(), config.getRootGitDirectory(), config.getAPIKey());
SnapshotAPIRequest.setBasicAuth(config.getUsername(), config.getPassword()); SnapshotAPIRequest.setBasicAuth(config.getUsername(), config.getPassword());
SnapshotAPIRequest.setBaseURL(config.getHostname()); writeLatexHostname = config.getHostname();
SnapshotAPIRequest.setBaseURL(writeLatexHostname);
Util.setServiceName(config.getServiceName());
} }
/** /**
@ -62,6 +66,7 @@ public class WLGitBridgeServer {
System.out.println(); System.out.println();
System.out.println("WriteLatex-Git Bridge server started"); System.out.println("WriteLatex-Git Bridge server started");
System.out.println("Listening on port: " + port); System.out.println("Listening on port: " + port);
System.out.println("Bridged to: " + writeLatexHostname);
System.out.println("Root git directory path: " + rootGitDirectoryPath); System.out.println("Root git directory path: " + rootGitDirectoryPath);
} catch (BindException e) { } catch (BindException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -5,6 +5,7 @@ import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.transport.resolver.UploadPackFactory; import org.eclipse.jgit.transport.resolver.UploadPackFactory;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -15,7 +16,7 @@ public class WLUploadPackFactory implements UploadPackFactory<HttpServletRequest
@Override @Override
public UploadPack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException { public UploadPack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException {
UploadPack uploadPack = new UploadPack(repository); UploadPack uploadPack = new UploadPack(repository);
uploadPack.sendMessage("Downloading files from WriteLatex"); uploadPack.sendMessage("Downloading files from " + Util.getServiceName());
return uploadPack; return uploadPack;
} }
} }

View file

@ -8,6 +8,8 @@ import java.io.IOException;
*/ */
public class Util { public class Util {
private static String SERVICE_NAME;
public static String entries(int entries) { public static String entries(int entries) {
if (entries == 1) { if (entries == 1) {
return "entry"; return "entry";
@ -54,4 +56,12 @@ public class Util {
return sb.toString(); return sb.toString();
} }
public static void setServiceName(String serviceName) {
SERVICE_NAME = serviceName;
}
public static String getServiceName() {
return SERVICE_NAME;
}
} }

View file

@ -31,6 +31,9 @@ public abstract class SnapshotAPIRequest<T extends Result> extends Request<T> {
} }
public static void setBaseURL(String baseURL) { public static void setBaseURL(String baseURL) {
if (!(baseURL.endsWith("/"))) {
baseURL += "/";
}
BASE_URL = baseURL; BASE_URL = baseURL;
} }

View file

@ -1,5 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers; package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import uk.ac.ic.wlgitbridge.util.Util;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
*/ */
@ -12,7 +14,7 @@ public class SnapshotInfo {
public SnapshotInfo(int versionID, String createdAt, String name, String email) { public SnapshotInfo(int versionID, String createdAt, String name, String email) {
this.versionId = versionID; this.versionId = versionID;
comment = "Update on WriteLatex.com."; comment = "Update on " + Util.getServiceName() + ".";
user = new WLUser(name, email); user = new WLUser(name, email);
this.createdAt = createdAt; this.createdAt = createdAt;
} }

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.util.Util;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -32,7 +33,7 @@ public class InvalidFilesException extends SnapshotPostException {
public void fromJSON(JsonElement json) { public void fromJSON(JsonElement json) {
descriptionLines = new LinkedList<String>(); descriptionLines = new LinkedList<String>();
JsonArray errors = json.getAsJsonObject().get("errors").getAsJsonArray(); JsonArray errors = json.getAsJsonObject().get("errors").getAsJsonArray();
descriptionLines.add("You have " + errors.size() + " invalid files in your WriteLatex project:"); descriptionLines.add("You have " + errors.size() + " invalid files in your " + Util.getServiceName() + " project:");
for (JsonElement error : errors) { for (JsonElement error : errors) {
descriptionLines.add(describeError(error.getAsJsonObject())); descriptionLines.add(describeError(error.getAsJsonObject()));
} }

View file

@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.util.Util;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -12,8 +13,8 @@ import java.util.List;
public class UnexpectedErrorException extends SnapshotPostException { public class UnexpectedErrorException extends SnapshotPostException {
private static final String[] DESCRIPTION_LINES = { private static final String[] DESCRIPTION_LINES = {
"There was an internal error with the WriteLatex server.", "There was an internal error with the " + Util.getServiceName() + " server.",
"Please contact WriteLatex." "Please contact " + Util.getServiceName() + "."
}; };
public UnexpectedErrorException(JsonObject json) { public UnexpectedErrorException(JsonObject json) {
@ -22,7 +23,7 @@ public class UnexpectedErrorException extends SnapshotPostException {
@Override @Override
public String getMessage() { public String getMessage() {
return "writelatex error"; return Util.getServiceName() + " error";
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.model; package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotAttachment; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotAttachment;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
@ -49,7 +50,7 @@ public class Snapshot implements Comparable<Snapshot> {
this.versionID = versionID; this.versionID = versionID;
comment = "Most recent update"; comment = "Most recent update";
userName = "Anonymous"; userName = "Anonymous";
userEmail = "anonymous@writelatex.com"; userEmail = "anonymous@" + Util.getServiceName() + ".com";
createdAt = new Date(); createdAt = new Date();
srcs = new LinkedList<SnapshotFile>(); srcs = new LinkedList<SnapshotFile>();