VIVO-1004 Treat an accept header of application/ld+json as equivalent to application/json

This is a minimal fix. It doesn't take related issues into account. It also doesn't attempt to
clean up the logic in IndividualController, or to deal with the fact that much of this logic is
repeated around the application.
This commit is contained in:
Jim Blake 2015-03-20 17:21:08 -04:00
parent a909cda3ee
commit cf5a56d8e8
8 changed files with 19 additions and 7 deletions

View file

@ -51,6 +51,7 @@ public class VitroHttpServlet extends HttpServlet implements MultipartRequestWra
public final static String RDFXML_MIMETYPE = "application/rdf+xml";
public final static String JSON_MIMETYPE = "application/json";
public final static String JSON_LD_MIMETYPE = "application/ld+json";
public final static String N3_MIMETYPE = "text/n3"; // unofficial and unregistered
public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and unregistered

View file

@ -80,7 +80,7 @@ public class SparqlQueryApiController extends VitroApiServlet {
} catch (NotAcceptableException | AcceptHeaderParsingException e) {
sendShortResponse(SC_NOT_ACCEPTABLE,
"The accept header does not include any "
+ "available content type.", e, resp);
+ "available content type: " + e.getMessage(), resp);
} catch (RDFServiceException e) {
sendShortResponse(SC_INTERNAL_SERVER_ERROR,
"Problem executing the query.", e, resp);

View file

@ -20,7 +20,9 @@ public enum RdfResultMediaType {
TTL("text/turtle", false, "N3", "TTL"),
JSON("application/json", false, "N3", "JSON");
JSON("application/json", false, "N3", "JSON"),
JSON_LD("application/ld+json", false, "N3", "JSON");
// ----------------------------------------------------------------------
// Keep a map of content types, for easy conversion back and forth

View file

@ -55,7 +55,7 @@ abstract class SparqlQueryApiRdfProducer extends SparqlQueryApiExecutor {
if (mediaType.isNativeFormat()) {
IOUtils.copy(rawResult, out);
} else if (mediaType == RdfResultMediaType.JSON) {
} else if (mediaType.getJenaResponseFormat().equals("JSON")) {
// JSON-LD is a special case, since jena 2.6.4 doesn't support it.
try {
JenaRDFParser parser = new JenaRDFParser();

View file

@ -325,7 +325,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
String mediaType = values.getContentType().getMediaType();
response.setContentType(mediaType);
if ( JSON_MIMETYPE.equals(mediaType)){
if ( JSON_MIMETYPE.equals(mediaType) || JSON_LD_MIMETYPE.equals(mediaType)){
//json-ld is not supported by jena v2.6.4
try {
JenaRDFParser parser = new JenaRDFParser();

View file

@ -45,6 +45,7 @@ public class IndividualController extends FreemarkerHttpServlet {
map.put(XHTML_MIMETYPE, 0.5f);
map.put("application/xml", 0.5f);
map.put(JSON_MIMETYPE, 1.0f);
map.put(JSON_LD_MIMETYPE, 1.0f);
map.put(RDFXML_MIMETYPE, 1.0f);
map.put(RDFXML_MIMETYPE, 1.0f);
map.put(N3_MIMETYPE, 1.0f);

View file

@ -2,10 +2,11 @@
package edu.cornell.mannlib.vitro.webapp.controller.individual;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.JSON_LD_MIMETYPE;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.JSON_MIMETYPE;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.N3_MIMETYPE;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.RDFXML_MIMETYPE;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.TTL_MIMETYPE;
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.JSON_MIMETYPE;
import java.util.Map;
import java.util.regex.Matcher;
@ -118,7 +119,7 @@ public class IndividualRequestAnalyzer {
return "/individual/" + m.group(1) + "/" + m.group(1) + ".n3";
} else if (TTL_MIMETYPE.equals(mediaType)) {
return "/individual/" + m.group(1) + "/" + m.group(1) + ".ttl";
} else if (JSON_MIMETYPE.equals(mediaType)){
} else if (JSON_MIMETYPE.equals(mediaType) || JSON_LD_MIMETYPE.equals(mediaType)){
return "/individual/" + m.group(1) + "/" + m.group(1) + ".jsonld";
}
}
@ -145,7 +146,8 @@ public class IndividualRequestAnalyzer {
IndividualController.ACCEPTED_CONTENT_TYPES);
if (RDFXML_MIMETYPE.equals(ctStr) || N3_MIMETYPE.equals(ctStr)
|| TTL_MIMETYPE.equals(ctStr) || JSON_MIMETYPE.equals(ctStr)) {
|| TTL_MIMETYPE.equals(ctStr) || JSON_MIMETYPE.equals(ctStr)
|| JSON_LD_MIMETYPE.equals(ctStr)) {
return new ContentType(ctStr);
}
} catch (Throwable th) {

View file

@ -151,6 +151,12 @@ public class ContentType implements Serializable {
public static final ContentType JSON =
new ContentType("application/json;" + DEFAULT_CHARSET).lock();
/**
* A ContentType constant that describes the JSON-LD content type.
*/
public static final ContentType JSON_LD =
new ContentType("application/ld+json;" + DEFAULT_CHARSET).lock();
/**
* A ContentType constant that describes the Javascript content type.
*/