Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
409 when setting RDF.type to a string
- change 409 to 400

Resolves: https://jira.duraspace.org/browse/FCREPO-1368
  • Loading branch information
yinlinchen authored and Andrew Woods committed Feb 24, 2015
1 parent dccda20 commit b62061d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
Expand Up @@ -31,7 +31,6 @@
import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_MODIFIED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
Expand Down Expand Up @@ -102,6 +101,9 @@
import nu.validator.htmlparser.sax.HtmlParser;
import nu.validator.saxtree.TreeBuilder;

import org.fcrepo.http.commons.domain.RDFMediaType;
import org.fcrepo.kernel.RdfLexicon;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
Expand All @@ -124,8 +126,6 @@
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.jena.riot.Lang;
import org.fcrepo.http.commons.domain.RDFMediaType;
import org.fcrepo.kernel.RdfLexicon;
import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -1482,7 +1482,7 @@ public void testLinkToNonExistent() throws Exception {
("INSERT { <> <http://some-vocabulary#isMemberOfCollection> " +
"<" + serverAddress + "non-existant> } WHERE {}").getBytes()));
patch.setEntity(e);
assertEquals(CONFLICT.getStatusCode(), getStatus(patch));
assertEquals(BAD_REQUEST.getStatusCode(), getStatus(patch));
}

@Test
Expand Down Expand Up @@ -1570,13 +1570,25 @@ public void testUpdateObjectGraphWithProblems() throws Exception {
patchObjMethod.setEntity(e);
final HttpResponse response = client.execute(patchObjMethod);

if (response.getStatusLine().getStatusCode() != CONFLICT.getStatusCode()
if (response.getStatusLine().getStatusCode() != BAD_REQUEST.getStatusCode()
&& response.getEntity() != null) {
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Got unexpected update response:\n" + content);
}
assertEquals(CONFLICT.getStatusCode(), response.getStatusLine().getStatusCode());
assertEquals(BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode());

}

@Test
public void testPutResourceBadRdf() throws Exception {
final String pid = getRandomUniquePid();
final HttpPut httpPut = new HttpPut(serverAddress + "/test/" + pid);
httpPut.setHeader("Content-Type", "text/turtle");
httpPut.setEntity(new StringEntity("<> a \"still image\"."));

final HttpResponse response = client.execute(httpPut);

assertEquals(BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode());
}

@Test
Expand All @@ -1587,7 +1599,7 @@ public void testRepeatedPut() throws Exception {

final HttpPut secondPut = new HttpPut(serverAddress + pid);
secondPut.setHeader("Content-Type", "text/turtle");
assertEquals(409, getStatus(secondPut));
assertEquals(400, getStatus(secondPut));
}

@Test
Expand Down
Expand Up @@ -15,17 +15,18 @@
*/
package org.fcrepo.http.commons.exceptionhandlers;

import org.apache.commons.codec.binary.Base64;
import org.fcrepo.kernel.exception.MalformedRdfException;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static org.fcrepo.kernel.RdfLexicon.CONSTRAINED_BY;

import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import static javax.ws.rs.core.Response.Status.CONFLICT;
import static javax.ws.rs.core.Response.status;
import static org.fcrepo.kernel.RdfLexicon.CONSTRAINED_BY;
import org.fcrepo.kernel.exception.MalformedRdfException;

import org.apache.commons.codec.binary.Base64;

/**
* @author cabeer
Expand All @@ -37,7 +38,11 @@ public class MalformedRdfExceptionMapper implements ExceptionMapper<MalformedRdf
@Override
public Response toResponse(final MalformedRdfException e) {
final Link link = Link.fromUri(getConstraintUri(e)).rel(CONSTRAINED_BY.getURI()).build();
return status(CONFLICT).entity(e.getMessage()).links(link).build();
final String msg = e.getMessage();
if (msg.matches(".*org.*Exception: .*")) {
return status(BAD_REQUEST).entity(msg.replaceAll("org.*Exception: ", "")).links(link).build();
}
return status(BAD_REQUEST).entity(msg).links(link).build();
}

private static String getConstraintUri(final MalformedRdfException e) {
Expand Down
Expand Up @@ -52,4 +52,22 @@ public void testToResponse() throws IOException {
assertEquals("Constraint data appears malformed", 2, split.length);
assertEquals("xyz", IOUtils.toString(Base64.decodeBase64(split[1].getBytes()), "UTF-8"));
}

@Test
public void testToResponseError() throws IOException {
final String errorPrefix = "org.modeshape.jcr.value.ValueFormatException: ";
final String errorSuffix = "Error converting ...";
final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));

assertEquals(errorSuffix, response.getEntity().toString());
}

@Test
public void testToResponseError2() throws IOException {
final String errorPrefix = "org.modeshape.jcr.value.ValueFormat: ";
final String errorSuffix = "Error converting ...";
final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));

assertEquals(errorPrefix + errorSuffix, response.getEntity().toString());
}
}

0 comments on commit b62061d

Please sign in to comment.