com.github.trepo.dcap.server.rest.Search Maven / Gradle / Ivy
The newest version!
package com.github.trepo.dcap.server.rest;
import com.github.trepo.dcap.store.Query;
import com.github.trepo.dcap.store.Store;
import com.github.trepo.vgraph.Commit;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
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;
import java.util.List;
/**
* Search for commits.
* @author John Clark.
*/
@Path("/_search")
public class Search {
/**
* Gson instance for serialization/de-serialization.
*/
private Gson gson = new Gson();
/**
* The commit store to use.
*/
private Store store;
/**
* Search for commits.
* @param body The query to use.
* @return A list of commits.
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response search(String body) {
if (body == null) {
throw new WebApplicationException("JSON Query body is required", Response.Status.BAD_REQUEST);
}
Query query;
try {
query = gson.fromJson(body, Query.class);
} catch (JsonSyntaxException e) {
throw new WebApplicationException("Invalid JSON Query Syntax", e, Response.Status.BAD_REQUEST);
}
List commits = store.query(query);
return Response
.status(Response.Status.OK)
.entity(gson.toJson(commits))
.build();
}
/**
* Sets the Commit Store to use via Injection.
* @param commitStore The commit store.
*/
@Inject
public void setStore(Store commitStore) {
store = commitStore;
}
}