| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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 username;
- private String password;
- private String apiBaseURL;
- private String postbackURL;
- private String serviceName;
- 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();
- username = getOptionalString(configObject, "username");
- password = getOptionalString(configObject, "password");
- String apiBaseURL = getElement(configObject, "apiBaseUrl").getAsString();
- if (!apiBaseURL.endsWith("/")) {
- apiBaseURL += "/";
- }
- this.apiBaseURL = apiBaseURL;
- serviceName = getElement(configObject, "serviceName").getAsString();
- postbackURL = getElement(configObject, "postbackUrl").getAsString();
- if (!postbackURL.endsWith("/")) {
- postbackURL += "/";
- }
- }
- public int getPort() {
- return port;
- }
- public String getRootGitDirectory() {
- return rootGitDirectory;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- public String getAPIBaseURL() {
- return apiBaseURL;
- }
- private JsonElement getElement(JsonObject configObject, String name) {
- JsonElement element = configObject.get(name);
- if (element == null) {
- throw new RuntimeException(new InvalidConfigFileException(name));
- }
- 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;
- }
- public String getPostbackURL() {
- return postbackURL;
- }
- }
|