Skip to content
Permalink
Browse files

Updating logic to successfully detect empty request body

  • Loading branch information...
escowles committed Oct 30, 2014
1 parent 3432611 commit c335ba20d91b00e1fd153756906dfb4e7a616d06
@@ -262,20 +262,26 @@ public Response createOrReplaceObjectRdf(
resourceTriples = getResourceTriples(prefer);
}

if (requestBodyStream == null && !resource.isNew()) {
throw new ClientErrorException("No RDF provided and the resource already exists!", CONFLICT);
} else if (requestBodyStream != null) {
if ((resource instanceof FedoraObject || resource instanceof Datastream)
&& isRdfContentType(contentType.toString())) {
try {
replaceResourceWithStream(resource, requestBodyStream, contentType, resourceTriples);
} catch (final RiotException e) {
throw new BadRequestException("RDF was not parsable", e);
}
} else if (resource instanceof FedoraBinary) {
replaceResourceBinaryWithStream((FedoraBinary) resource,
requestBodyStream, contentDisposition, contentType.toString(), checksum);
} else if (!resource.isNew()) {
if (resource instanceof FedoraBinary) {
replaceResourceBinaryWithStream((FedoraBinary) resource,
requestBodyStream, contentDisposition, contentType.toString(), checksum);
} else if (isRdfContentType(contentType.toString())) {
try {
replaceResourceWithStream(resource, requestBodyStream, contentType, resourceTriples);
} catch (final RiotException e) {
throw new BadRequestException("RDF was not parsable", e);
}
} else if (!resource.isNew()) {
boolean emptyRequest = true;
try {
emptyRequest = requestBodyStream.read() == -1;
} catch (IOException ex) {
LOGGER.debug("Error checking for request body content", ex);
}

if (requestContentType == null && emptyRequest) {
throw new ClientErrorException("Resource Already Exists", CONFLICT);
} else {
throw new ClientErrorException("Invalid Content Type " + requestContentType, UNSUPPORTED_MEDIA_TYPE);
}
}
@@ -114,6 +114,7 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
@@ -834,6 +835,18 @@ public void testPutDatastreamContentOnObject() throws Exception {
415, response.getStatusLine().getStatusCode());
}

@Test
public void testEmptyPutToExistingObject() throws Exception {
final String content = "foo";
final String pid = getRandomUniquePid();
createObject(pid);

final HttpPut put = new HttpPut(serverAddress + pid);
final HttpResponse response = execute(put);
assertEquals("Expected 409 response code when doing empty PUT on an existing object.",
409, response.getStatusLine().getStatusCode());
}

@Test
public void testPutMalformedRDFOnObject() throws Exception {
final String content = "foo";
@@ -1538,6 +1551,7 @@ public void testUpdateObjectWithoutContentType() throws Exception {
createObject(pid);

final HttpPut httpPut = new HttpPut(serverAddress + pid);
httpPut.setEntity(new ByteArrayEntity("bogus content".getBytes()));
assertEquals(415, getStatus(httpPut));
}

@@ -2209,4 +2223,4 @@ public String apply(final Header h) {
}


}
}

0 comments on commit c335ba2

Please sign in to comment.
You can’t perform that action at this time.