com.github.trepo.dcap.server.rest.Put Maven / Gradle / Ivy
The newest version!
package com.github.trepo.dcap.server.rest;
import com.github.trepo.dcap.store.Store;
import com.github.trepo.vgraph.Commit;
import com.github.trepo.vgraph.exception.VGraphException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Save a commit.
* @author John Clark.
*/
@Path("/")
public class Put {
/**
* Gson instance for serialization/de-serialization.
*/
private Gson gson = new Gson();
/**
* The commit store to use.
*/
private Store store;
/**
* Save a commit to the commit store.
* @param body The Commit.
* @return 201 on success.
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response put(String body) {
if (body == null) {
throw new WebApplicationException("JSON Commit body is required", Response.Status.BAD_REQUEST);
}
Commit commit;
try {
commit = gson.fromJson(body, Commit.class);
} catch (JsonSyntaxException e) {
throw new WebApplicationException("Invalid JSON Commit Syntax", e, Response.Status.BAD_REQUEST);
}
try {
commit.validate();
} catch(VGraphException e) {
throw new WebApplicationException("Invalid Commit: " + e.getMessage(), e, Response.Status.BAD_REQUEST);
}
store.put(commit);
return Response
.status(Response.Status.CREATED)
.build();
}
/**
* Sets the Commit Store to use via Injection.
* @param commitStore The commit store.
*/
@Inject
public void setStore(Store commitStore) {
store = commitStore;
}
}