Skip to content

Commit

Permalink
Json files
Browse files Browse the repository at this point in the history
  • Loading branch information
idemura committed Oct 23, 2019
1 parent 555b082 commit 9d0a80e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 150 deletions.
14 changes: 7 additions & 7 deletions src/main/java/id/jred/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ private void server() throws IOException {
var pid = PidFile.create();
try {
LOG.debug("Reading repo_map");
Map<String, Repo> repoMap = Json.mapper.readValue(
Map<String, JsonTarget> repoMap = Json.mapper.readValue(
new File(Dir.getHome(), "repo_map"),
new TypeReference<HashMap<String, Repo>>() {});
new TypeReference<HashMap<String, JsonTarget>>() {});

LOG.debug("Substitute env vars");
substituteEnvVars(repoMap);
Expand All @@ -140,7 +140,7 @@ private void server() throws IOException {
" port=" + port);
}

private static void substituteEnvVars(Map<String, Repo> repoMap) throws IOException {
private static void substituteEnvVars(Map<String, JsonTarget> repoMap) throws IOException {
var pattern = Pattern.compile("\\$\\{(.+?)}");
var env = System.getenv();
for (var r : repoMap.values()) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
53 changes: 28 additions & 25 deletions src/main/java/id/jred/Handlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
public final class Handlers {
private static final Logger LOG = LoggerFactory.getLogger("jred");

private final Map<String, Repo> repoMap;
private final Map<String, JsonTarget> repoMap;

public static void start(String host, int port, Map<String, Repo> repoMap) {
public static void start(String host, int port, Map<String, JsonTarget> repoMap) {
Spark.ipAddress(host);
Spark.port(port);

Expand All @@ -40,7 +40,7 @@ public static void start(String host, int port, Map<String, Repo> repoMap) {
}).start();
}

private Handlers(Map<String, Repo> repoMap) {
private Handlers(Map<String, JsonTarget> repoMap) {
this.repoMap = repoMap;
}

Expand All @@ -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()) {
Expand All @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/id/jred/JsonCopy.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/id/jred/JsonDiff.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/id/jred/JsonRepo.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/id/jred/JsonStatus.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 9d0a80e

Please sign in to comment.