From b7ebc7a766ef01eab67c9069986633ac295ec253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esm=C3=A9=20Cowles?= Date: Mon, 19 May 2014 15:40:00 -0700 Subject: [PATCH 1/3] Adding check for existing destination of a MOVE, and adding IT to check it --- .../java/org/fcrepo/http/api/FedoraNodes.java | 4 ++++ .../integration/http/api/FedoraNodesIT.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java index efbc447fe3..a29eaf6114 100644 --- a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java +++ b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java @@ -821,6 +821,10 @@ public Response moveObject(@PathParam("path") final List pathList, return status(SC_BAD_GATEWAY).entity("Destination was not a valid resource path").build(); } + if (nodeService.exists(session, destination)) { + return status(SC_PRECONDITION_FAILED).entity("Destination resource already exists").build(); + } + nodeService.moveObject(session, path, destination); session.save(); versionService.nodeUpdated(session, destination); diff --git a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java index 01c67584f9..7a97d902ca 100644 --- a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java +++ b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java @@ -37,6 +37,7 @@ import static javax.ws.rs.core.Response.Status.NOT_MODIFIED; import static javax.ws.rs.core.Response.Status.NO_CONTENT; import static javax.ws.rs.core.Response.Status.OK; +import static javax.ws.rs.core.Response.Status.PRECONDITION_FAILED; import static nu.validator.htmlparser.common.DoctypeExpectation.NO_DOCTYPE_ERRORS; import static nu.validator.htmlparser.common.XmlViolationPolicy.ALLOW; import static org.apache.http.impl.client.cache.CacheConfig.DEFAULT; @@ -1069,6 +1070,21 @@ public void testMove() throws Exception { assertEquals(NOT_FOUND.getStatusCode(), originalResult.getStatusLine().getStatusCode()); } + @Test + public void testMoveDestExists() throws Exception { + + final HttpResponse response1 = createObject(""); + final String location1 = response1.getFirstHeader("Location").getValue(); + final HttpResponse response2 = createObject(""); + final String location2 = response2.getFirstHeader("Location").getValue(); + + final HttpMove request = new HttpMove(location1); + request.addHeader("Destination", location2); + final HttpResponse result = client.execute(request); + + assertEquals(PRECONDITION_FAILED.getStatusCode(), result.getStatusLine().getStatusCode()); + } + @Test public void testMoveWithBadEtag() throws Exception { From ca593fef1e5484c94018b9bb63ab7e6a762433f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esm=C3=A9=20Cowles?= Date: Mon, 19 May 2014 16:04:58 -0700 Subject: [PATCH 2/3] Also fixing COPY method --- .../java/org/fcrepo/http/api/FedoraNodes.java | 7 ++++--- .../integration/http/api/FedoraNodesIT.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java index a29eaf6114..baaba434a4 100644 --- a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java +++ b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java @@ -767,8 +767,11 @@ public Response copyObject(@PathParam("path") final List path, if (destination == null) { return status(SC_BAD_GATEWAY).entity("Destination was not a valid resource path").build(); + } else if (nodeService.exists(session, destination)) { + return status(SC_PRECONDITION_FAILED).entity("Destination resource already exists").build(); } + nodeService.copyObject(session, toPath(path), destination); session.save(); versionService.nodeUpdated(session, destination); @@ -819,9 +822,7 @@ public Response moveObject(@PathParam("path") final List pathList, if (destination == null) { return status(SC_BAD_GATEWAY).entity("Destination was not a valid resource path").build(); - } - - if (nodeService.exists(session, destination)) { + } else if (nodeService.exists(session, destination)) { return status(SC_PRECONDITION_FAILED).entity("Destination resource already exists").build(); } diff --git a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java index 7a97d902ca..ac05efa5d2 100644 --- a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java +++ b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java @@ -1050,6 +1050,21 @@ public void testCopy() throws Exception { assertEquals(OK.getStatusCode(), originalResult.getStatusLine().getStatusCode()); } + @Test + public void testCopyDestExists() throws Exception { + + final HttpResponse response1 = createObject(""); + final String location1 = response1.getFirstHeader("Location").getValue(); + final HttpResponse response2 = createObject(""); + final String location2 = response2.getFirstHeader("Location").getValue(); + + final HttpCopy request = new HttpCopy(location1); + request.addHeader("Destination", location2); + final HttpResponse result = client.execute(request); + + assertEquals(PRECONDITION_FAILED.getStatusCode(), result.getStatusLine().getStatusCode()); + } + @Test public void testMove() throws Exception { From 3dbcac2eb9db2d399c5b7d5508b13360e7a6c7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esm=C3=A9=20Cowles?= Date: Tue, 20 May 2014 05:13:29 -0700 Subject: [PATCH 3/3] PUT requires either new resource or RDF to update properties, adding ITs for PUT and POST/slug to existing resources --- .../java/org/fcrepo/http/api/FedoraNodes.java | 10 ++++++--- .../integration/http/api/FedoraNodesIT.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java index baaba434a4..fe3eac08f5 100644 --- a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java +++ b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java @@ -394,7 +394,7 @@ public Response updateSparql(@PathParam("path") } /** - * Replace triples with triples from a new model + * Create a resource at a specified path, or replace triples with provided RDF. * @param pathList * @param uriInfo * @param requestContentType @@ -411,8 +411,7 @@ public Response createOrReplaceObjectRdf( @HeaderParam("Content-Type") final MediaType requestContentType, final InputStream requestBodyStream, - @Context - final Request request, + @Context final Request request, @Context final HttpServletResponse servletResponse) throws RepositoryException, ParseException, IOException, InvalidChecksumException, URISyntaxException { final String path = toPath(pathList); @@ -425,9 +424,11 @@ public Response createOrReplaceObjectRdf( final MediaType contentType = getSimpleContentType(requestContentType); + final boolean preexisting; if (nodeService.exists(session, path)) { resource = nodeService.getObject(session, path); response = noContent(); + preexisting = true; } else { final MediaType effectiveContentType = requestBodyStream == null || requestContentType == null ? null : contentType; @@ -438,6 +439,7 @@ public Response createOrReplaceObjectRdf( final URI location = new URI(idTranslator.getSubject(resource.getNode().getPath()).getURI()); response = created(location).entity(location.toString()); + preexisting = false; } evaluateRequestPreconditions(request, resource); @@ -455,6 +457,8 @@ public Response createOrReplaceObjectRdf( resource.replaceProperties(graphSubjects, inputModel); + } else if (preexisting) { + return status(SC_CONFLICT).entity("No RDF provided and the resource already exists!").build(); } session.save(); diff --git a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java index ac05efa5d2..fd97919581 100644 --- a/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java +++ b/fcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java @@ -214,6 +214,17 @@ public void testIngestWithSlug() throws Exception { getStatus(new HttpGet(location))); } + @Test + public void testIngestWithRepeatedSlug() throws Exception { + final String pid = getRandomUniquePid(); + final HttpPut put = new HttpPut(serverAddress + pid); + assertEquals(201, getStatus(put)); + + final HttpPost method = postObjMethod(""); + method.addHeader("Slug", pid); + assertEquals(409, getStatus(method)); + } + @Test public void testIngestWithBinary() throws Exception { final HttpPost method = postObjMethod(""); @@ -740,6 +751,16 @@ public void testUpdateObjectGraphWithProblems() throws Exception { } + @Test + public void testRepeatedPut() throws Exception { + final String pid = getRandomUniquePid(); + final HttpPut firstPut = new HttpPut(serverAddress + pid); + assertEquals(201, getStatus(firstPut)); + + final HttpPut secondPut = new HttpPut(serverAddress + pid); + assertEquals(409, getStatus(secondPut)); + } + @Test public void testFilteredLDPTypes() throws Exception { final String pid = getRandomUniquePid();