Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Content types with parameters should be stripped out before translati…
…ng them into RDF formats

fixes https://www.pivotaltracker.com/story/show/70599014"
  • Loading branch information
cbeer committed May 2, 2014
1 parent 21d0b88 commit 624b9b0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
Expand Up @@ -25,8 +25,8 @@
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status;
import static org.apache.jena.riot.RDFLanguages.contentTypeToLang;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.apache.jena.riot.WebContent.contentTypeToLang;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand Down Expand Up @@ -170,7 +170,7 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,

final String partName = contentDisposition.getParameters().get("name");

final String contentTypeString = part.getMediaType().toString();
final String contentTypeString = getSimpleContentType(part.getMediaType()).toString();

LOGGER.trace("Processing {} part {} with media type {}",
contentDispositionType, partName, contentTypeString);
Expand All @@ -184,7 +184,7 @@ public Response batchModify(@PathParam("path") final List<PathSegment> pathList,
if (contentDisposition.getFileName() != null) {
realContentDisposition = ATTACHMENT;
} else if (contentTypeString.equals(contentTypeSPARQLUpdate)
|| contentTypeToLang(contentTypeString) != null) {
|| isRdfContentType(contentTypeString)) {
realContentDisposition = INLINE;
} else if (partName.equals(FORM_DATA_DELETE_PART_NAME)) {
realContentDisposition = DELETE;
Expand Down
Expand Up @@ -49,7 +49,6 @@
import java.text.ParseException;
import java.util.List;

import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.status;
Expand Down Expand Up @@ -87,9 +86,7 @@ public Response create(@PathParam("path")
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream, @Context final HttpServletResponse servletResponse)
throws InvalidChecksumException, RepositoryException, URISyntaxException, ParseException {
final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;
final MediaType contentType = getSimpleContentType(requestContentType);


final String newDatastreamPath;
Expand Down Expand Up @@ -188,9 +185,7 @@ public Response modifyContent(@PathParam("path") final List<PathSegment> pathLis

try {
final String path = toPath(pathList);
final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;
final MediaType contentType = getSimpleContentType(requestContentType);

if (nodeService.exists(session, path)) {

Expand Down
40 changes: 20 additions & 20 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -24,7 +24,6 @@
import static com.sun.jersey.api.Responses.conflict;
import static com.sun.jersey.api.Responses.notAcceptable;
import static com.sun.jersey.api.Responses.notFound;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
Expand All @@ -39,8 +38,8 @@
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
import static org.apache.http.HttpStatus.SC_CONFLICT;
import static org.apache.http.HttpStatus.SC_PRECONDITION_FAILED;
import static org.apache.jena.riot.RDFLanguages.contentTypeToLang;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.apache.jena.riot.WebContent.contentTypeToLang;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
Expand Down Expand Up @@ -403,11 +402,16 @@ public Response createOrReplaceObjectRdf(
final FedoraResource resource;
final Response.ResponseBuilder response;


final MediaType contentType = getSimpleContentType(requestContentType);

if (nodeService.exists(session, path)) {
resource = nodeService.getObject(session, path);
response = noContent();
} else {
resource = createFedoraResource(null, requestContentType, path);
final MediaType effectiveContentType
= requestBodyStream == null || requestContentType == null ? null : contentType;
resource = createFedoraResource(null, effectiveContentType, path);
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

Expand All @@ -422,8 +426,7 @@ public Response createOrReplaceObjectRdf(
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

if (requestContentType != null && requestBodyStream != null) {
final String contentType = requestContentType.toString();
final String format = contentTypeToLang(contentType).getName().toUpperCase();
final String format = contentTypeToLang(contentType.toString()).getName().toUpperCase();

final Model inputModel = createDefaultModel()
.read(requestBodyStream,
Expand Down Expand Up @@ -477,9 +480,7 @@ public Response createObject(@PathParam("path")
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;
final MediaType contentType = getSimpleContentType(requestContentType);

final String contentTypeString = contentType.toString();

Expand Down Expand Up @@ -512,16 +513,20 @@ public Response createObject(@PathParam("path")

try {

final FedoraResource result = createFedoraResource(mixin, requestContentType, newObjectPath);
final MediaType effectiveContentType
= requestBodyStream == null || requestContentType == null ? null : contentType;
final FedoraResource result = createFedoraResource(mixin,
effectiveContentType,
newObjectPath);

final Response.ResponseBuilder response;
final URI location = new URI(idTranslator.getSubject(result.getNode().getPath()).getURI());

if (requestBodyStream == null) {
if (requestBodyStream == null || requestContentType == null) {
LOGGER.trace("No request body detected");
response = created(location).entity(location.toString());
} else {
LOGGER.trace("Received createObject with a request body");
LOGGER.trace("Received createObject with a request body and content type \"{}\"", contentTypeString);

if (contentTypeString.equals(contentTypeSPARQLUpdate)) {
LOGGER.trace("Found SPARQL-Update content, applying..");
Expand All @@ -531,16 +536,11 @@ public Response createObject(@PathParam("path")
} else {
response = noContent();
}
} else if (contentTypeToLang(contentTypeString) != null) {
} else if (isRdfContentType(contentTypeString)) {
LOGGER.trace("Found a RDF syntax, attempting to replace triples");

final Lang lang = contentTypeToLang(contentTypeString);

if (lang == null) {
throw new WebApplicationException(notAcceptable().entity(
"Invalid Content type " + contentType).build());
}

final String format = lang.getName().toUpperCase();

final Model inputModel =
Expand Down Expand Up @@ -642,7 +642,7 @@ private String getRequestedObjectType(final String mixin, final MediaType reques
} else {
if (requestContentType != null) {
final String s = requestContentType.toString();
if (!s.equals(contentTypeSPARQLUpdate) && contentTypeToLang(s) == null) {
if (!s.equals(contentTypeSPARQLUpdate) && !isRdfContentType(s)) {
objectType = FEDORA_DATASTREAM;
}
}
Expand Down Expand Up @@ -672,7 +672,8 @@ public Response createObjectFromFormPost(
@FormDataParam("file") final InputStream file
) throws RepositoryException, URISyntaxException, InvalidChecksumException, ParseException, IOException {

return createObject(pathList, mixin, null, null, null, slug, servletResponse, uriInfo, file);
final MediaType effectiveContentType = file == null ? null : MediaType.APPLICATION_OCTET_STREAM_TYPE;
return createObject(pathList, mixin, null, null, effectiveContentType, slug, servletResponse, uriInfo, file);

}

Expand All @@ -695,7 +696,6 @@ public Response deleteObject(@PathParam("path")

final FedoraResource resource =
nodeService.getObject(session, path);

evaluateRequestPreconditions(request, resource);

nodeService.deleteObject(session, path);
Expand Down
Expand Up @@ -121,7 +121,6 @@ public class FedoraNodesIT extends AbstractResourceIT {

@Test
public void testIngest() throws Exception {

final String pid = getRandomUniquePid();

final HttpResponse response = createObject(pid);
Expand Down
Expand Up @@ -16,6 +16,8 @@

package org.fcrepo.http.commons;

import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static org.apache.jena.riot.RDFLanguages.contentTypeToLang;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Date;
Expand All @@ -27,6 +29,7 @@
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -211,6 +214,15 @@ protected static void evaluateRequestPreconditions(final Request request,
evaluateRequestPreconditions(request, resource, false);
}

protected static MediaType getSimpleContentType(MediaType requestContentType) {
return requestContentType != null ? new MediaType(requestContentType.getType(), requestContentType.getSubtype())
: APPLICATION_OCTET_STREAM_TYPE;
}

protected static boolean isRdfContentType(String contentTypeString) {
return !contentTypeString.equals("text/plain") && contentTypeToLang(contentTypeString) != null;
}

protected void addResponseInformationToStream(
final FedoraResource resource, final RdfStream dataset,
final UriInfo uriInfo, final IdentifierTranslator subjects)
Expand Down
Expand Up @@ -16,16 +16,19 @@

package org.fcrepo.http.commons;

import static org.fcrepo.http.commons.AbstractResource.getSimpleContentType;
import static org.fcrepo.http.commons.AbstractResource.toPath;
import static org.fcrepo.http.commons.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.http.commons.test.util.TestHelpers.setField;
import static org.junit.Assert.assertEquals;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriInfo;

import com.google.common.collect.ImmutableMap;
import org.fcrepo.kernel.identifiers.PidMinter;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.utils.NamespaceTools;
Expand Down Expand Up @@ -167,4 +170,12 @@ public void testToPathEmpty() {
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testGetSimpleContentType() {
final MediaType mediaType = new MediaType("text", "plain", ImmutableMap.of("charset", "UTF-8"));
final MediaType sanitizedMediaType = getSimpleContentType(mediaType);

assertEquals("text/plain", sanitizedMediaType.toString());
}
}

0 comments on commit 624b9b0

Please sign in to comment.