From 71de277c2d372fe4b867d5e317b430f729e4b3d8 Mon Sep 17 00:00:00 2001 From: Michael Ritter Date: Thu, 9 Mar 2017 17:51:13 -0500 Subject: [PATCH 1/2] Basic models for the Compare api controller --- .../umiacs/ace/rest/models/CompareFile.java | 30 +++++++++ .../ace/rest/models/CompareRequest.java | 24 +++++++ .../ace/rest/models/CompareResponse.java | 66 +++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareFile.java create mode 100644 ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareRequest.java create mode 100644 ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareResponse.java diff --git a/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareFile.java b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareFile.java new file mode 100644 index 0000000..bdcbf40 --- /dev/null +++ b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareFile.java @@ -0,0 +1,30 @@ +package edu.umiacs.ace.rest.models; + +/** + * Individual item in a Compare Request + * + * Created by shake on 3/9/17. + */ +public class CompareFile { + + private String path; + private String digest; + + public String getPath() { + return path; + } + + public String getDigest() { + return digest; + } + + public CompareFile setPath(String path) { + this.path = path; + return this; + } + + public CompareFile setDigest(String digest) { + this.digest = digest; + return this; + } +} diff --git a/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareRequest.java b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareRequest.java new file mode 100644 index 0000000..80a3947 --- /dev/null +++ b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareRequest.java @@ -0,0 +1,24 @@ +package edu.umiacs.ace.rest.models; + +import edu.umiacs.ace.rest.CompareController; + +import java.util.List; + +/** + * The request body sent to the {@link CompareController} + * + * Created by shake on 3/9/17. + */ +public class CompareRequest { + + private List comparisons; + + public List getComparisons() { + return comparisons; + } + + public CompareRequest setComparisons(List comparisons) { + this.comparisons = comparisons; + return this; + } +} diff --git a/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareResponse.java b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareResponse.java new file mode 100644 index 0000000..10b2600 --- /dev/null +++ b/ace-am/src/main/java/edu/umiacs/ace/rest/models/CompareResponse.java @@ -0,0 +1,66 @@ +package edu.umiacs.ace.rest.models; + +import edu.umiacs.ace.rest.CompareController; + +import java.util.HashSet; +import java.util.Set; + +/** + * Response object returned by the {@link CompareController} + * + * Created by shake on 3/9/17. + */ +public class CompareResponse { + + private Set diff; + private Set match; + private Set notFound; + + public CompareResponse() { + this.diff = new HashSet<>(); + this.match = new HashSet<>(); + this.notFound = new HashSet<>(); + } + + public CompareResponse addDiff(String path) { + diff.add(path); + return this; + } + + public Set getDiff() { + return diff; + } + + public CompareResponse setDiff(Set diff) { + this.diff = diff; + return this; + } + + public CompareResponse addMatch(String path) { + match.add(path); + return this; + } + + public Set getMatch() { + return match; + } + + public CompareResponse setMatch(Set match) { + this.match = match; + return this; + } + + public CompareResponse addNotFound(String path) { + notFound.add(path); + return this; + } + + public Set getNotFound() { + return notFound; + } + + public CompareResponse setNotFound(Set notFound) { + this.notFound = notFound; + return this; + } +} -- GitLab From 4712098a4b05a2ccf3ee4473c8268f9e7b25c91f Mon Sep 17 00:00:00 2001 From: Michael Ritter Date: Thu, 9 Mar 2017 17:53:50 -0500 Subject: [PATCH 2/2] Add initial implementation of a comparison method --- .../umiacs/ace/rest/CompareController.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 ace-am/src/main/java/edu/umiacs/ace/rest/CompareController.java diff --git a/ace-am/src/main/java/edu/umiacs/ace/rest/CompareController.java b/ace-am/src/main/java/edu/umiacs/ace/rest/CompareController.java new file mode 100644 index 0000000..c783480 --- /dev/null +++ b/ace-am/src/main/java/edu/umiacs/ace/rest/CompareController.java @@ -0,0 +1,71 @@ +package edu.umiacs.ace.rest; + +import com.google.common.collect.ImmutableList; +import edu.umiacs.ace.monitor.core.Collection; +import edu.umiacs.ace.monitor.core.MonitoredItem; +import edu.umiacs.ace.rest.models.CompareFile; +import edu.umiacs.ace.rest.models.CompareRequest; +import edu.umiacs.ace.rest.models.CompareResponse; +import edu.umiacs.ace.util.PersistUtil; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.Objects; + +/** + * Compare files to what ACE knows about a collection + * + * Created by shake on 3/9/17. + */ +@Path("/") +public class CompareController { + + /** + * API method for comparing files and their digests to a given collection + * + * todo: It would be nice to not have "compare/id" and instead be more along the lines of "collection/id/compare" + * todo: BadRequest if a collection == null? + * + * @param id the id of the collection + * @param request the request containing the comparisons + * @return the result of the comparison + */ + @POST + @Path("compare/{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public CompareResponse compare(@PathParam("id") Long id, CompareRequest request) { + if (request == null) { + request = new CompareRequest(); + request.setComparisons(ImmutableList.of()); + } + + EntityManager em = PersistUtil.getEntityManager(); + CompareResponse response = new CompareResponse(); + Collection collection = em.find(Collection.class, id); // can this throw an exception? + for (CompareFile file : request.getComparisons()) { + TypedQuery query = em.createNamedQuery("MonitoredItem.getItemByPath", MonitoredItem.class); + query.setParameter("path", file.getPath()); + query.setParameter("coll", collection); + try { + MonitoredItem result = query.getSingleResult(); + if (Objects.equals(result.getFileDigest(), file.getDigest())) { + response.addMatch(file.getPath()); + } else { + response.addDiff(file.getPath()); + } + } catch (Exception e) { + response.addNotFound(file.getPath()); + } + } + + return response; + } + +} -- GitLab