Adding serialization support or JSON-LD to indivdiual controller. VIVO-255
This commit is contained in:
parent
22d7e04573
commit
ab11fff370
4 changed files with 38 additions and 12 deletions
|
@ -45,6 +45,7 @@ public class VitroHttpServlet extends HttpServlet {
|
||||||
public final static String HTML_MIMETYPE = "text/html";
|
public final static String HTML_MIMETYPE = "text/html";
|
||||||
|
|
||||||
public final static String RDFXML_MIMETYPE = "application/rdf+xml";
|
public final static String RDFXML_MIMETYPE = "application/rdf+xml";
|
||||||
|
public final static String JSON_MIMETYPE = "application/json";
|
||||||
public final static String N3_MIMETYPE = "text/n3"; // unofficial and unregistered
|
public final static String N3_MIMETYPE = "text/n3"; // unofficial and unregistered
|
||||||
public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and unregistered
|
public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and unregistered
|
||||||
|
|
||||||
|
|
|
@ -20,10 +20,14 @@ import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import com.github.jsonldjava.core.JSONLD;
|
||||||
|
import com.github.jsonldjava.core.JSONLDProcessingError;
|
||||||
|
import com.github.jsonldjava.impl.JenaRDFParser;
|
||||||
|
import com.github.jsonldjava.utils.JSONUtils;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
|
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
|
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequiresActions;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
|
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
|
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||||
|
@ -39,6 +43,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Tem
|
||||||
import edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailFactory;
|
import edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailFactory;
|
||||||
import edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailMessage;
|
import edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailMessage;
|
||||||
import edu.cornell.mannlib.vitro.webapp.freemarker.config.FreemarkerConfiguration;
|
import edu.cornell.mannlib.vitro.webapp.freemarker.config.FreemarkerConfiguration;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.Tags;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.Tags;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.User;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.User;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
|
||||||
|
@ -309,6 +314,16 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
||||||
String mediaType = values.getContentType().getMediaType();
|
String mediaType = values.getContentType().getMediaType();
|
||||||
response.setContentType(mediaType);
|
response.setContentType(mediaType);
|
||||||
|
|
||||||
|
if ( JSON_MIMETYPE.equals(mediaType)){
|
||||||
|
//json-ld is not supported by jena v2.6.4
|
||||||
|
try {
|
||||||
|
JenaRDFParser parser = new JenaRDFParser();
|
||||||
|
Object json = JSONLD.fromRDF( values.getModel() , parser);
|
||||||
|
JSONUtils.write(response.getWriter(), json);
|
||||||
|
} catch (JSONLDProcessingError e) {
|
||||||
|
throw new IOException("Could not convert from Jena model to JSON-LD", e);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
String format = "";
|
String format = "";
|
||||||
if ( RDFXML_MIMETYPE.equals(mediaType)) {
|
if ( RDFXML_MIMETYPE.equals(mediaType)) {
|
||||||
format = "RDF/XML";
|
format = "RDF/XML";
|
||||||
|
@ -317,10 +332,12 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
||||||
} else if ( TTL_MIMETYPE.equals(mediaType)) {
|
} else if ( TTL_MIMETYPE.equals(mediaType)) {
|
||||||
format ="TTL";
|
format ="TTL";
|
||||||
}
|
}
|
||||||
|
|
||||||
values.getModel().write( response.getOutputStream(), format );
|
values.getModel().write( response.getOutputStream(), format );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected void doException(VitroRequest vreq, HttpServletResponse response,
|
protected void doException(VitroRequest vreq, HttpServletResponse response,
|
||||||
ResponseValues values) throws TemplateProcessingException {
|
ResponseValues values) throws TemplateProcessingException {
|
||||||
// Log the error, and display an error message on the page.
|
// Log the error, and display an error message on the page.
|
||||||
|
|
|
@ -39,6 +39,8 @@ public class IndividualController extends FreemarkerHttpServlet {
|
||||||
map.put(HTML_MIMETYPE, 0.5f);
|
map.put(HTML_MIMETYPE, 0.5f);
|
||||||
map.put(XHTML_MIMETYPE, 0.5f);
|
map.put(XHTML_MIMETYPE, 0.5f);
|
||||||
map.put("application/xml", 0.5f);
|
map.put("application/xml", 0.5f);
|
||||||
|
map.put(JSON_MIMETYPE, 1.0f);
|
||||||
|
map.put(RDFXML_MIMETYPE, 1.0f);
|
||||||
map.put(RDFXML_MIMETYPE, 1.0f);
|
map.put(RDFXML_MIMETYPE, 1.0f);
|
||||||
map.put(N3_MIMETYPE, 1.0f);
|
map.put(N3_MIMETYPE, 1.0f);
|
||||||
map.put(TTL_MIMETYPE, 1.0f);
|
map.put(TTL_MIMETYPE, 1.0f);
|
||||||
|
|
|
@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.controller.individual;
|
||||||
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.N3_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.RDFXML_MIMETYPE;
|
||||||
import static edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet.TTL_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.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
@ -117,6 +118,8 @@ public class IndividualRequestAnalyzer {
|
||||||
return "/individual/" + m.group(1) + "/" + m.group(1) + ".n3";
|
return "/individual/" + m.group(1) + "/" + m.group(1) + ".n3";
|
||||||
} else if (TTL_MIMETYPE.equals(mediaType)) {
|
} else if (TTL_MIMETYPE.equals(mediaType)) {
|
||||||
return "/individual/" + m.group(1) + "/" + m.group(1) + ".ttl";
|
return "/individual/" + m.group(1) + "/" + m.group(1) + ".ttl";
|
||||||
|
} else if (JSON_MIMETYPE.equals(mediaType)){
|
||||||
|
return "/individual/" + m.group(1) + "/" + m.group(1) + ".jsonld";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// or redirect to the canonical URL for HTML representation.
|
// or redirect to the canonical URL for HTML representation.
|
||||||
|
@ -244,6 +247,9 @@ public class IndividualRequestAnalyzer {
|
||||||
if (formatParam.contains("ttl")) {
|
if (formatParam.contains("ttl")) {
|
||||||
return ContentType.TURTLE;
|
return ContentType.TURTLE;
|
||||||
}
|
}
|
||||||
|
if (formatParam.contains("jsonld") || formatParam.contains("json")){
|
||||||
|
return ContentType.JSON;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check for parts of URL that indicate request for RDF. Examples:
|
* Check for parts of URL that indicate request for RDF. Examples:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue