diff --git a/src/main/java/id/jred/App.java b/src/main/java/id/jred/App.java index 627a405..b62429d 100644 --- a/src/main/java/id/jred/App.java +++ b/src/main/java/id/jred/App.java @@ -122,9 +122,9 @@ private void server() throws IOException { var pid = PidFile.create(); try { LOG.debug("Reading repo_map"); - Map repoMap = Json.mapper.readValue( + Map repoMap = Json.mapper.readValue( new File(Dir.getHome(), "repo_map"), - new TypeReference>() {}); + new TypeReference>() {}); LOG.debug("Substitute env vars"); substituteEnvVars(repoMap); @@ -140,7 +140,7 @@ private void server() throws IOException { " port=" + port); } - private static void substituteEnvVars(Map repoMap) throws IOException { + private static void substituteEnvVars(Map repoMap) throws IOException { var pattern = Pattern.compile("\\$\\{(.+?)}"); var env = System.getenv(); for (var r : repoMap.values()) { @@ -189,7 +189,7 @@ private void submit() throw new IOException("Path to .git not found in " + Dir.getCurrent()); } LOG.debug("repoDir={}", repoDir.toString()); - var repo = Protocol.repo( + var repo = new JsonRepo( repoDir.getName(), Script.run("git/revision", repoDir).trim()); LOG.debug("Repo name={} revision={}", repo.getName(), repo.getRevision()); @@ -224,14 +224,14 @@ private void submit() try (var stream = new FileInputStream(f)) { data = new String(stream.readAllBytes()); } - post(buildUrl("/copy"), Protocol.copy( + post(buildUrl("/copy"), new JsonCopy( repo, repoDir.toPath().relativize(f.toPath()).toString(), data)); } var diff = Script.run("git/diff", repoDir); - post(buildUrl("/diff"), Protocol.diff(repo, diff)); + post(buildUrl("/diff"), new JsonDiff(repo, diff)); } private void update() throws IOException { @@ -277,7 +277,7 @@ private static byte[] post(URL url, Object request) throws IOException { try (var es = connection.getErrorStream()) { String msg; if (es != null) { - msg = Json.read(Protocol.Error.class, es).getMessage(); + msg = Json.read(JsonStatus.class, es).getMessage(); } else { msg = "HTTP error code: " + connection.getResponseCode(); } diff --git a/src/main/java/id/jred/Handlers.java b/src/main/java/id/jred/Handlers.java index 48ecb3b..8cbb513 100644 --- a/src/main/java/id/jred/Handlers.java +++ b/src/main/java/id/jred/Handlers.java @@ -16,9 +16,9 @@ public final class Handlers { private static final Logger LOG = LoggerFactory.getLogger("jred"); - private final Map repoMap; + private final Map repoMap; - public static void start(String host, int port, Map repoMap) { + public static void start(String host, int port, Map repoMap) { Spark.ipAddress(host); Spark.port(port); @@ -40,7 +40,7 @@ public static void start(String host, int port, Map repoMap) { }).start(); } - private Handlers(Map repoMap) { + private Handlers(Map repoMap) { this.repoMap = repoMap; } @@ -57,49 +57,52 @@ private Object root(Request req, Response response) { } return os.toString(); } catch (IOException ex) { - return respondCatch(response, ex); + return respondUnexpected(response, ex); } } private Object copy(Request req, Response response) { LOG.debug("Handle /copy"); try { - var copyRequest = Json.read(Protocol.Copy.class, req.bodyAsBytes()); + var copyRequest = Json.read(JsonCopy.class, req.bodyAsBytes()); var repo = repoMap.get(copyRequest.getRepo().getName()); if (repo == null) { - return respondError(response, 400, - "Repo not found: " + copyRequest.getRepo().getName()); + return respondBadRequest( + response, + "Repo not found: " + copyRequest.getRepo().getName()); } var repoPath = new File(repo.getPath()).getAbsoluteFile().getCanonicalFile(); var destPath = new File(repoPath, copyRequest.getFile()).getCanonicalFile(); if (!destPath.toPath().startsWith(repoPath.toPath())) { - return respondError(response, 400, - "File must belong to repo directory tree: " + copyRequest.getFile()); + return respondBadRequest( + response, + "File must belong to repo directory tree: " + copyRequest.getFile()); } destPath.getParentFile().mkdirs(); try (var stream = new FileOutputStream(destPath)) { stream.write(copyRequest.getData().getBytes()); } - return respond200(response); + return respondOK(response); } catch (IOException ex) { - return respondCatch(response, ex); + return respondUnexpected(response, ex); } } private Object diff(Request req, Response response) { LOG.debug("Handle /diff"); try { - var diffRequest = Json.read(Protocol.Diff.class, req.bodyAsBytes()); + var diffRequest = Json.read(JsonDiff.class, req.bodyAsBytes()); var repo = repoMap.get(diffRequest.getRepo().getName()); if (repo == null) { - return respondError(response, 400, - "Repo not found: " + diffRequest.getRepo().getName()); + return respondBadRequest( + response, + "Repo not found: " + diffRequest.getRepo().getName()); } var repoPath = new File(repo.getPath()).getAbsoluteFile().getCanonicalFile(); var revision = Script.run("git/revision", repoPath).trim(); if (!revision.equals(diffRequest.getRepo().getRevision())) { - return respondError(response, 400, - "Revision mismatch: server " + revision + + return respondBadRequest(response, + "Revision mismatch: server " + revision + ", client " + diffRequest.getRepo().getRevision()); } if (!diffRequest.getDiff().isEmpty()) { @@ -108,29 +111,29 @@ private Object diff(Request req, Response response) { repoPath, diffRequest.getDiff()); } - return respond200(response); + return respondOK(response); } catch (InterruptedException | IOException ex) { - return respondCatch(response, ex); + return respondUnexpected(response, ex); } } - private static String respond200(Response response) + private static String respondOK(Response response) throws IOException { response.status(200); - return renderJson(response, Protocol.error()); + return renderJson(response, new JsonStatus()); } - private static String respondError(Response response, int status, String message) + private static String respondBadRequest(Response response, String message) throws IOException { - response.status(status); - return renderJson(response, Protocol.error(message)); + response.status(400); + return renderJson(response, new JsonStatus(message)); } - private static String respondCatch(Response response, Exception cause) { + private static String respondUnexpected(Response response, Exception cause) { LOG.error("Error: {}", cause.getMessage()); response.status(500); try { - return renderJson(response, Protocol.error(cause.getMessage())); + return renderJson(response, new JsonStatus(cause.getMessage())); } catch (IOException ex) { LOG.error("Fatal: {}", ex.getMessage()); return null; diff --git a/src/main/java/id/jred/JsonCopy.java b/src/main/java/id/jred/JsonCopy.java new file mode 100644 index 0000000..544fbc3 --- /dev/null +++ b/src/main/java/id/jred/JsonCopy.java @@ -0,0 +1,32 @@ +package id.jred; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public final class JsonCopy { + private JsonRepo repo; + private String file; + private String data; + + public JsonCopy() {} + + public JsonCopy(JsonRepo repo, String file, String data) { + this.repo = repo; + this.file = file; + this.data = data; + } + + @JsonProperty("repo") + public JsonRepo getRepo() { + return repo; + } + + @JsonProperty("file") + public String getFile() { + return file; + } + + @JsonProperty("data") + public String getData() { + return data; + } +} diff --git a/src/main/java/id/jred/JsonDiff.java b/src/main/java/id/jred/JsonDiff.java new file mode 100644 index 0000000..2bf6f17 --- /dev/null +++ b/src/main/java/id/jred/JsonDiff.java @@ -0,0 +1,25 @@ +package id.jred; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public final class JsonDiff { + private JsonRepo repo; + private String diff; + + public JsonDiff() {} + + public JsonDiff(JsonRepo repo, String diff) { + this.repo = repo; + this.diff = diff; + } + + @JsonProperty("repo") + public JsonRepo getRepo() { + return repo; + } + + @JsonProperty("diff") + public String getDiff() { + return diff; + } +} diff --git a/src/main/java/id/jred/JsonRepo.java b/src/main/java/id/jred/JsonRepo.java new file mode 100644 index 0000000..c9ff6a1 --- /dev/null +++ b/src/main/java/id/jred/JsonRepo.java @@ -0,0 +1,25 @@ +package id.jred; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public final class JsonRepo { + private String name; + private String revision; + + public JsonRepo() {} + + public JsonRepo(String name, String revision) { + this.name = name; + this.revision = revision; + } + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("revision") + public String getRevision() { + return revision; + } +} diff --git a/src/main/java/id/jred/JsonStatus.java b/src/main/java/id/jred/JsonStatus.java new file mode 100644 index 0000000..e00cb72 --- /dev/null +++ b/src/main/java/id/jred/JsonStatus.java @@ -0,0 +1,20 @@ +package id.jred; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public final class JsonStatus { + private String message; + + public JsonStatus() { + this(""); + } + + public JsonStatus(String message) { + this.message = message; + } + + @JsonProperty("message") + public String getMessage() { + return message; + } +} diff --git a/src/main/java/id/jred/Repo.java b/src/main/java/id/jred/JsonTarget.java similarity index 88% rename from src/main/java/id/jred/Repo.java rename to src/main/java/id/jred/JsonTarget.java index 5004ad7..1766814 100644 --- a/src/main/java/id/jred/Repo.java +++ b/src/main/java/id/jred/JsonTarget.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; -public final class Repo { +public final class JsonTarget { private String path; public void setPath(String path) { diff --git a/src/main/java/id/jred/Protocol.java b/src/main/java/id/jred/Protocol.java deleted file mode 100644 index e55f206..0000000 --- a/src/main/java/id/jred/Protocol.java +++ /dev/null @@ -1,117 +0,0 @@ -package id.jred; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public final class Protocol { - private Protocol() {} - - public static final class Repo { - private String name; - private String revision; - - public Repo() {} - - public Repo(String name, String revision) { - this.name = name; - this.revision = revision; - } - - @JsonProperty("name") - public String getName() { - return name; - } - - @JsonProperty("revision") - public String getRevision() { - return revision; - } - } - - public static final class Copy { - private Repo repo; - private String file; - private String data; - - public Copy() {} - - public Copy(Repo repo, String file, String data) { - this.repo = repo; - this.file = file; - this.data = data; - } - - @JsonProperty("repo") - public Repo getRepo() { - return repo; - } - - @JsonProperty("file") - public String getFile() { - return file; - } - - @JsonProperty("data") - public String getData() { - return data; - } - } - - public static final class Diff { - private Repo repo; - private String diff; - - public Diff() {} - - public Diff(Repo repo, String diff) { - this.repo = repo; - this.diff = diff; - } - - @JsonProperty("repo") - public Repo getRepo() { - return repo; - } - - @JsonProperty("diff") - public String getDiff() { - return diff; - } - } - - public static final class Error { - private String message; - - public Error() { - this(""); - } - - public Error(String message) { - this.message = message; - } - - @JsonProperty("message") - public String getMessage() { - return message; - } - } - - public static Repo repo(String name, String revision) { - return new Repo(name, revision); - } - - public static Copy copy(Repo repo, String file, String data) { - return new Copy(repo, file, data); - } - - public static Diff diff(Repo repo, String diff) { - return new Diff(repo, diff); - } - - public static Error error() { - return new Error(); - } - - public static Error error(String message) { - return new Error(message); - } -}