Skip to content

Commit

Permalink
Handling errors with sparql update queries that reference non-existen…
Browse files Browse the repository at this point in the history
…t resources, fixing empty query error handling, adding ITs
  • Loading branch information
escowles committed May 27, 2014
1 parent 4da6bb1 commit a90de16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
16 changes: 11 additions & 5 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -33,6 +33,7 @@
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.OK;
import static org.apache.commons.lang.ArrayUtils.contains;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.http.HttpStatus.SC_BAD_GATEWAY;
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
import static org.apache.http.HttpStatus.SC_CONFLICT;
Expand Down Expand Up @@ -354,17 +355,15 @@ public Response updateSparql(@PathParam("path")
LOGGER.debug("Attempting to update path: {}", path);

try {

if (requestBodyStream != null) {

final String requestBody = IOUtils.toString(requestBodyStream);
if (requestBodyStream != null && !isBlank(requestBody) ) {
final FedoraResource resource =
nodeService.getObject(session, path);

evaluateRequestPreconditions(request, resource);

final Dataset properties = resource.updatePropertiesDataset(new HttpIdentifierTranslator(
session, FedoraNodes.class, uriInfo), IOUtils
.toString(requestBodyStream));
session, FedoraNodes.class, uriInfo), requestBody);


final Model problems = properties.getNamedModel(PROBLEMS_MODEL_NAME);
Expand All @@ -387,6 +386,13 @@ public Response updateSparql(@PathParam("path")
return status(SC_BAD_REQUEST).entity(
"SPARQL-UPDATE requests must have content!").build();

} catch ( RuntimeException ex ) {
final Throwable cause = ex.getCause();
if ( cause != null && cause instanceof PathNotFoundException ) {
// the sparql update referred to a repository resource that doesn't exist
return status(SC_BAD_REQUEST).entity(cause.getMessage()).build();
}
throw ex;
} finally {
session.logout();
}
Expand Down
Expand Up @@ -32,6 +32,7 @@
import static org.apache.jena.riot.WebContent.contentTypeNTriples;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.NOT_MODIFIED;
Expand Down Expand Up @@ -669,6 +670,29 @@ public void testGetObjectGraphByUUID() throws Exception {

}

@Test
public void testLinkToNonExistent() throws Exception {
final HttpResponse createResponse = createObject("");
final String subjectURI = createResponse.getFirstHeader("Location").getValue();
final HttpPatch patch = new HttpPatch(subjectURI);
patch.addHeader("Content-Type", "application/sparql-update");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("INSERT { <> <http://fedora.info/definitions/v4/rels-ext#isMemberOfCollection> " +
"<" + serverAddress + "non-existant> } WHERE {}").getBytes()));
patch.setEntity(e);
assertEquals(BAD_REQUEST.getStatusCode(), getStatus(patch));
}

@Test
public void testEmtpyPatch() throws Exception {
final HttpResponse createResponse = createObject("");
final String subjectURI = createResponse.getFirstHeader("Location").getValue();
final HttpPatch patch = new HttpPatch(subjectURI);
patch.addHeader("Content-Type", "application/sparql-update");
assertEquals(BAD_REQUEST.getStatusCode(), getStatus(patch));
}

@Test
public void testUpdateObjectGraph() throws Exception {
final HttpResponse createResponse = createObject("");
Expand Down

0 comments on commit a90de16

Please sign in to comment.