mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-06 23:59:01 +02:00
Changed program arguments to only take config file.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package uk.ac.ic.wlgitbridge.application;
|
||||
|
||||
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.writelatex.api.request.base.JSONSource;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 05/12/14.
|
||||
*/
|
||||
public class Config implements JSONSource {
|
||||
|
||||
private int port;
|
||||
private String rootGitDirectory;
|
||||
private String apiKey;
|
||||
|
||||
public Config(String configFilePath) throws InvalidConfigFileException, IOException {
|
||||
try {
|
||||
fromJSON(new Gson().fromJson(new FileReader(configFilePath), JsonElement.class));
|
||||
} catch (JsonParseException e) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJSON(JsonElement json) {
|
||||
JsonObject configObject = json.getAsJsonObject();
|
||||
port = getElement(configObject, "port").getAsInt();
|
||||
rootGitDirectory = getElement(configObject, "rootGitDirectory").getAsString();
|
||||
apiKey = getElement(configObject, "apiKey").getAsString();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getRootGitDirectory() {
|
||||
return rootGitDirectory;
|
||||
}
|
||||
|
||||
public String getAPIKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
private JsonElement getElement(JsonObject configObject, String name) {
|
||||
JsonElement element = configObject.get(name);
|
||||
if (element == null) {
|
||||
throw new RuntimeException(new InvalidConfigFileException(name));
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-10
@@ -3,12 +3,12 @@ package uk.ac.ic.wlgitbridge.application;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
|
||||
import uk.ac.ic.wlgitbridge.util.Util;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
@@ -25,7 +25,7 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
if (request.getMethod().equals("POST") && request.getPathInfo().endsWith("postback")) {
|
||||
String contents = getContentsOfReader(request.getReader());
|
||||
String contents = Util.getContentsOfReader(request.getReader());
|
||||
String[] parts = request.getRequestURI().split("/");
|
||||
if (parts.length < 4) {
|
||||
throw new ServletException();
|
||||
@@ -43,12 +43,4 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static String getContentsOfReader(BufferedReader reader) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-16
@@ -1,9 +1,11 @@
|
||||
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.git.exception.InvalidRootDirectoryPathException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 02/11/14.
|
||||
@@ -15,10 +17,10 @@ import javax.servlet.ServletException;
|
||||
public class WLGitBridgeApplication {
|
||||
|
||||
public static final int EXIT_CODE_FAILED = 1;
|
||||
private static final String USAGE_MESSAGE = "usage: writelatex-git-bridge port root_git_directory_path";
|
||||
private static final String USAGE_MESSAGE = "usage: writelatex-git-bridge config_file";
|
||||
|
||||
private int port;
|
||||
private String rootGitDirectoryPath;
|
||||
private String configFilePath;
|
||||
private Config config;
|
||||
|
||||
/**
|
||||
* Constructs an instance of the WriteLatex-Git Bridge application.
|
||||
@@ -27,9 +29,16 @@ public class WLGitBridgeApplication {
|
||||
public WLGitBridgeApplication(String[] args) {
|
||||
try {
|
||||
parseArguments(args);
|
||||
loadConfigFile();
|
||||
} catch (InvalidProgramArgumentsException e) {
|
||||
printUsage();
|
||||
System.exit(EXIT_CODE_FAILED);
|
||||
} catch (InvalidConfigFileException e) {
|
||||
System.out.println("The property for " + e.getMissingMember() + " is invalid. Check your config file.");
|
||||
System.exit(EXIT_CODE_FAILED);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Invalid config file. Check the file path.");
|
||||
System.exit(EXIT_CODE_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +47,11 @@ public class WLGitBridgeApplication {
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
new WLGitBridgeServer(port, rootGitDirectoryPath).start();
|
||||
new WLGitBridgeServer(config.getPort(), config.getRootGitDirectory(), config.getAPIKey()).start();
|
||||
} catch (ServletException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidRootDirectoryPathException e) {
|
||||
printUsage();
|
||||
System.out.println("Invalid root git directory path. Check your config file.");
|
||||
System.exit(EXIT_CODE_FAILED);
|
||||
}
|
||||
}
|
||||
@@ -51,26 +60,21 @@ public class WLGitBridgeApplication {
|
||||
|
||||
private void parseArguments(String[] args) throws InvalidProgramArgumentsException {
|
||||
checkArgumentsLength(args);
|
||||
parsePortNumber(args);
|
||||
parseRootGitDirectoryPath(args);
|
||||
parseConfigFilePath(args);
|
||||
}
|
||||
|
||||
private void checkArgumentsLength(String[] args) throws InvalidProgramArgumentsException {
|
||||
if (args.length < 2) {
|
||||
if (args.length < 1) {
|
||||
throw new InvalidProgramArgumentsException();
|
||||
}
|
||||
}
|
||||
|
||||
private void parsePortNumber(String[] args) throws InvalidProgramArgumentsException {
|
||||
try {
|
||||
port = Integer.parseInt(args[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidProgramArgumentsException();
|
||||
}
|
||||
private void parseConfigFilePath(String[] args) throws InvalidProgramArgumentsException {
|
||||
configFilePath = args[0];
|
||||
}
|
||||
|
||||
private void parseRootGitDirectoryPath(String[] args) {
|
||||
rootGitDirectoryPath = args[1];
|
||||
private void loadConfigFile() throws InvalidConfigFileException, IOException {
|
||||
config = new Config(configFilePath);
|
||||
}
|
||||
|
||||
private void printUsage() {
|
||||
|
||||
@@ -35,9 +35,10 @@ public class WLGitBridgeServer {
|
||||
* Constructs an instance of the server.
|
||||
* @param port the port number to listen on
|
||||
* @param rootGitDirectoryPath the root directory path containing the git repositories
|
||||
* @param apiKey
|
||||
* @throws ServletException if the servlet throws an exception
|
||||
*/
|
||||
public WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
|
||||
public WLGitBridgeServer(final int port, String rootGitDirectoryPath, String apiKey) throws ServletException, InvalidRootDirectoryPathException {
|
||||
this.port = port;
|
||||
this.rootGitDirectoryPath = rootGitDirectoryPath;
|
||||
Log.setLog(new NullLogger());
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package uk.ac.ic.wlgitbridge.application.exception;
|
||||
|
||||
/**
|
||||
* Created by Winston on 05/12/14.
|
||||
*/
|
||||
public class InvalidConfigFileException extends Exception {
|
||||
|
||||
private final String missingMember;
|
||||
|
||||
public InvalidConfigFileException(String missingMember) {
|
||||
|
||||
this.missingMember = missingMember;
|
||||
}
|
||||
|
||||
public String getMissingMember() {
|
||||
return missingMember;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -3,5 +3,6 @@ package uk.ac.ic.wlgitbridge.git.exception;
|
||||
/**
|
||||
* Created by Winston on 03/11/14.
|
||||
*/
|
||||
public class InvalidRootDirectoryPathException extends Throwable {
|
||||
public class InvalidRootDirectoryPathException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package uk.ac.ic.wlgitbridge.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 19/11/14.
|
||||
*/
|
||||
@@ -43,4 +46,12 @@ public class Util {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getContentsOfReader(BufferedReader reader) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package uk.ac.ic.wlgitbridge.application;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConfigTest {
|
||||
|
||||
@Test
|
||||
public void isReadingConfigCorrectly() throws IOException {
|
||||
// Config config = new Config("/Users/Roxy/Code/java/writelatex-git-bridge/bin/config.json");
|
||||
// Assert.assertEquals(80, config.getPort());
|
||||
// Assert.assertEquals("/var/wlgb/git", config.getRootGitDirectory());
|
||||
// Assert.assertEquals("", config.getAPIKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPort() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRootGitDirectory() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAPIKey() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package uk.ac.ic.wlgitbridge.application.exception;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class InvalidConfigFileExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testGetMissingMember() throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user