NIHVIVO-3877 and NIHVIVO-3872 using RDFService for aspects of import and export
This commit is contained in:
parent
0f9d1a79d1
commit
eace91759a
5 changed files with 129 additions and 54 deletions
|
@ -3,7 +3,10 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.controller.jena;
|
package edu.cornell.mannlib.vitro.webapp.controller.jena;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.io.PipedInputStream;
|
||||||
|
import java.io.PipedOutputStream;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -11,6 +14,7 @@ import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
@ -28,6 +32,11 @@ import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceModelMaker;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaOutputUtils;
|
import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaOutputUtils;
|
||||||
|
|
||||||
public class JenaExportController extends BaseEditController {
|
public class JenaExportController extends BaseEditController {
|
||||||
|
@ -116,12 +125,10 @@ public class JenaExportController extends BaseEditController {
|
||||||
getServletContext()).getABoxModel();
|
getServletContext()).getABoxModel();
|
||||||
}
|
}
|
||||||
else if("full".equals(assertedOrInferredParam)){
|
else if("full".equals(assertedOrInferredParam)){
|
||||||
model = ModelContext.getUnionOntModelSelector(
|
outputSparqlConstruct(ABOX_FULL_CONSTRUCT, formatParam, response);
|
||||||
getServletContext()).getABoxModel();
|
|
||||||
}
|
}
|
||||||
else if("asserted".equals(assertedOrInferredParam)){
|
else if("asserted".equals(assertedOrInferredParam)){
|
||||||
model = ModelContext.getBaseOntModelSelector(
|
outputSparqlConstruct(ABOX_ASSERTED_CONSTRUCT, formatParam, response);
|
||||||
getServletContext()).getABoxModel();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if("tbox".equals(subgraphParam)){
|
else if("tbox".equals(subgraphParam)){
|
||||||
|
@ -161,46 +168,16 @@ public class JenaExportController extends BaseEditController {
|
||||||
getServletContext()).getTBoxModel());
|
getServletContext()).getTBoxModel());
|
||||||
}
|
}
|
||||||
else if("full".equals(assertedOrInferredParam)){
|
else if("full".equals(assertedOrInferredParam)){
|
||||||
ontModel = xutil.extractTBox(dataset, ontologyURI, FULL_GRAPH);
|
outputSparqlConstruct(FULL_FULL_CONSTRUCT, formatParam, response);
|
||||||
ontModel.addSubModel(ModelContext.getUnionOntModelSelector(
|
|
||||||
getServletContext()).getABoxModel());
|
|
||||||
ontModel.addSubModel(ModelContext.getUnionOntModelSelector(
|
|
||||||
getServletContext()).getTBoxModel());
|
|
||||||
ontModel.addSubModel(ModelContext.getUnionOntModelSelector(
|
|
||||||
getServletContext()).getApplicationMetadataModel());
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
ontModel = xutil.extractTBox(
|
outputSparqlConstruct(FULL_ASSERTED_CONSTRUCT, formatParam, response);
|
||||||
dataset, ontologyURI, ASSERTIONS_GRAPH);
|
|
||||||
ontModel.addSubModel(ModelContext.getBaseOntModelSelector(
|
|
||||||
getServletContext()).getABoxModel());
|
|
||||||
ontModel.addSubModel(ModelContext.getBaseOntModelSelector(
|
|
||||||
getServletContext()).getTBoxModel());
|
|
||||||
ontModel.addSubModel(ModelContext.getBaseOntModelSelector(
|
|
||||||
getServletContext()).getApplicationMetadataModel());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JenaOutputUtils.setNameSpacePrefixes(model,vreq.getWebappDaoFactory());
|
JenaOutputUtils.setNameSpacePrefixes(model,vreq.getWebappDaoFactory());
|
||||||
|
setHeaders(response, formatParam);
|
||||||
if ( formatParam == null ) {
|
|
||||||
formatParam = "RDF/XML-ABBREV"; // default
|
|
||||||
}
|
|
||||||
String mime = formatToMimetype.get( formatParam );
|
|
||||||
if ( mime == null ) {
|
|
||||||
throw new RuntimeException( "Unsupported RDF format " + formatParam);
|
|
||||||
}
|
|
||||||
|
|
||||||
response.setContentType( mime );
|
|
||||||
if(mime.equals("application/rdf+xml"))
|
|
||||||
response.setHeader("content-disposition", "attachment; filename=" + "export.rdf");
|
|
||||||
else if(mime.equals("text/n3"))
|
|
||||||
response.setHeader("content-disposition", "attachment; filename=" + "export.n3");
|
|
||||||
else if(mime.equals("text/plain"))
|
|
||||||
response.setHeader("content-disposition", "attachment; filename=" + "export.txt");
|
|
||||||
else if(mime.equals("application/x-turtle"))
|
|
||||||
response.setHeader("content-disposition", "attachment; filename=" + "export.ttl");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
OutputStream outStream = response.getOutputStream();
|
OutputStream outStream = response.getOutputStream();
|
||||||
|
@ -237,6 +214,50 @@ public class JenaExportController extends BaseEditController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setHeaders(HttpServletResponse response, String formatParam) {
|
||||||
|
if ( formatParam == null ) {
|
||||||
|
formatParam = "RDF/XML-ABBREV"; // default
|
||||||
|
}
|
||||||
|
String mime = formatToMimetype.get( formatParam );
|
||||||
|
if ( mime == null ) {
|
||||||
|
throw new RuntimeException( "Unsupported RDF format " + formatParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setContentType( mime );
|
||||||
|
if(mime.equals("application/rdf+xml"))
|
||||||
|
response.setHeader("content-disposition", "attachment; filename=" + "export.rdf");
|
||||||
|
else if(mime.equals("text/n3"))
|
||||||
|
response.setHeader("content-disposition", "attachment; filename=" + "export.n3");
|
||||||
|
else if(mime.equals("text/plain"))
|
||||||
|
response.setHeader("content-disposition", "attachment; filename=" + "export.txt");
|
||||||
|
else if(mime.equals("application/x-turtle"))
|
||||||
|
response.setHeader("content-disposition", "attachment; filename=" + "export.ttl");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void outputSparqlConstruct(String queryStr, String formatParam,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
RDFService rdfService = RDFServiceUtils.getRDFServiceFactory(getServletContext()).getRDFService();
|
||||||
|
try {
|
||||||
|
setHeaders(response, formatParam);
|
||||||
|
OutputStream out = response.getOutputStream();
|
||||||
|
if ( formatParam.startsWith("RDF/XML") ) {
|
||||||
|
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".getBytes());
|
||||||
|
}
|
||||||
|
InputStream in = rdfService.sparqlConstructQuery(
|
||||||
|
queryStr, RDFServiceUtils.getSerializationFormatFromJenaString(
|
||||||
|
formatParam));
|
||||||
|
IOUtils.copy(in, out);
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
} catch (RDFServiceException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
throw new RuntimeException(ioe);
|
||||||
|
} finally {
|
||||||
|
rdfService.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void prepareExportSelectionPage( VitroRequest vreq, HttpServletResponse response ) {
|
private void prepareExportSelectionPage( VitroRequest vreq, HttpServletResponse response ) {
|
||||||
vreq.setAttribute( "bodyJsp", Controllers.EXPORT_SELECTION_JSP );
|
vreq.setAttribute( "bodyJsp", Controllers.EXPORT_SELECTION_JSP );
|
||||||
RequestDispatcher dispatcher = vreq.getRequestDispatcher( Controllers.BASIC_JSP );
|
RequestDispatcher dispatcher = vreq.getRequestDispatcher( Controllers.BASIC_JSP );
|
||||||
|
@ -278,16 +299,35 @@ public class JenaExportController extends BaseEditController {
|
||||||
formatToExtension.put("RDF/XML",".rdf");
|
formatToExtension.put("RDF/XML",".rdf");
|
||||||
formatToExtension.put("RDF/XML-ABBREV",".rdf");
|
formatToExtension.put("RDF/XML-ABBREV",".rdf");
|
||||||
formatToExtension.put("N3",".n3");
|
formatToExtension.put("N3",".n3");
|
||||||
formatToExtension.put("N-TRIPLES",".nt");
|
formatToExtension.put("N-TRIPLE",".nt");
|
||||||
formatToExtension.put("TURTLE",".ttl");
|
formatToExtension.put("TURTLE",".ttl");
|
||||||
|
|
||||||
formatToMimetype = new HashMap<String,String>();
|
formatToMimetype = new HashMap<String,String>();
|
||||||
formatToMimetype.put("RDF/XML","application/rdf+xml");
|
formatToMimetype.put("RDF/XML","application/rdf+xml");
|
||||||
formatToMimetype.put("RDF/XML-ABBREV","application/rdf+xml");
|
formatToMimetype.put("RDF/XML-ABBREV","application/rdf+xml");
|
||||||
formatToMimetype.put("N3","text/n3");
|
formatToMimetype.put("N3","text/n3");
|
||||||
formatToMimetype.put("N-TRIPLES", "text/plain");
|
formatToMimetype.put("N-TRIPLE", "text/plain");
|
||||||
formatToMimetype.put("TURTLE", "application/x-turtle");
|
formatToMimetype.put("TURTLE", "application/x-turtle");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String ABOX_FULL_CONSTRUCT = "CONSTRUCT { ?s ?p ?o } " +
|
||||||
|
"WHERE { GRAPH ?g { ?s ?p ?o } FILTER (!regex(str(?g), \"tbox\")) " +
|
||||||
|
"FILTER (?g != <" + JenaDataSourceSetupBase.JENA_APPLICATION_METADATA_MODEL + ">) " +
|
||||||
|
"FILTER (?g != <" + RDFServiceModelMaker.METADATA_MODEL_URI + ">) }";
|
||||||
|
|
||||||
|
private static final String ABOX_ASSERTED_CONSTRUCT = "CONSTRUCT { ?s ?p ?o } " +
|
||||||
|
"WHERE { GRAPH ?g { ?s ?p ?o } FILTER (!regex(str(?g), \"tbox\")) " +
|
||||||
|
"FILTER (?g != <" + JenaDataSourceSetupBase.JENA_INF_MODEL + ">) " +
|
||||||
|
"FILTER (?g != <" + JenaDataSourceSetupBase.JENA_APPLICATION_METADATA_MODEL + ">) " +
|
||||||
|
"FILTER (?g != <" + RDFServiceModelMaker.METADATA_MODEL_URI + ">) }";
|
||||||
|
|
||||||
|
private static final String FULL_FULL_CONSTRUCT = "CONSTRUCT { ?s ?p ?o } " +
|
||||||
|
"WHERE { ?s ?p ?o }";
|
||||||
|
|
||||||
|
private static final String FULL_ASSERTED_CONSTRUCT = "CONSTRUCT { ?s ?p ?o } " +
|
||||||
|
"WHERE { GRAPH ?g { ?s ?p ?o } " +
|
||||||
|
"FILTER (?g != <" + JenaDataSourceSetupBase.JENA_INF_MODEL + ">) " +
|
||||||
|
"FILTER (?g != <" + JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL + ">) }";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.controller.jena;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -41,6 +42,7 @@ import edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServ
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeSet;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeSet;
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
||||||
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
||||||
|
|
||||||
public class RDFUploadController extends JenaIngestController {
|
public class RDFUploadController extends JenaIngestController {
|
||||||
|
@ -239,7 +241,13 @@ public class RDFUploadController extends JenaIngestController {
|
||||||
ModelMaker maker = getVitroJenaModelMaker(request);
|
ModelMaker maker = getVitroJenaModelMaker(request);
|
||||||
|
|
||||||
if (docLoc!=null && modelName != null) {
|
if (docLoc!=null && modelName != null) {
|
||||||
doLoadRDFData(modelName, docLoc, filePath, languageStr, maker);
|
RDFService rdfService = RDFServiceUtils.getRDFServiceFactory(
|
||||||
|
getServletContext()).getRDFService();
|
||||||
|
try {
|
||||||
|
doLoadRDFData(modelName, docLoc, filePath, languageStr, rdfService);
|
||||||
|
} finally {
|
||||||
|
rdfService.close();
|
||||||
|
}
|
||||||
String modelType = getModelType(request, maker);
|
String modelType = getModelType(request, maker);
|
||||||
showModelList(request, maker, modelType);
|
showModelList(request, maker, modelType);
|
||||||
} else {
|
} else {
|
||||||
|
@ -319,12 +327,12 @@ public class RDFUploadController extends JenaIngestController {
|
||||||
String docLoc,
|
String docLoc,
|
||||||
String filePath,
|
String filePath,
|
||||||
String language,
|
String language,
|
||||||
ModelMaker modelMaker) {
|
RDFService rdfService) {
|
||||||
Model m = modelMaker.getModel(modelName);
|
|
||||||
m.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
try {
|
||||||
if ( (docLoc != null) && (docLoc.length()>0) ) {
|
if ( (docLoc != null) && (docLoc.length()>0) ) {
|
||||||
m.read(docLoc, language);
|
URL docLocURL = new URL(docLoc);
|
||||||
|
InputStream in = docLocURL.openStream();
|
||||||
|
readIntoModel(in, language, rdfService, modelName);
|
||||||
} else if ( (filePath != null) && (filePath.length()>0) ) {
|
} else if ( (filePath != null) && (filePath.length()>0) ) {
|
||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
File[] files;
|
File[] files;
|
||||||
|
@ -338,7 +346,8 @@ public class RDFUploadController extends JenaIngestController {
|
||||||
File currentFile = files[i];
|
File currentFile = files[i];
|
||||||
log.debug("Reading file " + currentFile.getName());
|
log.debug("Reading file " + currentFile.getName());
|
||||||
try {
|
try {
|
||||||
m.read(fileStream.getInputStream(), null, language);
|
readIntoModel(fileStream.getInputStream(), language,
|
||||||
|
rdfService, modelName);
|
||||||
fileStream.delete();
|
fileStream.delete();
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
String errMsg = "Error loading RDF from " +
|
String errMsg = "Error loading RDF from " +
|
||||||
|
@ -348,8 +357,20 @@ public class RDFUploadController extends JenaIngestController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} catch (IOException e) {
|
||||||
m.leaveCriticalSection();
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readIntoModel(InputStream in, String language,
|
||||||
|
RDFService rdfService, String modelName) {
|
||||||
|
ChangeSet cs = rdfService.manufactureChangeSet();
|
||||||
|
cs.addAddition(in, RDFServiceUtils.getSerializationFormatFromJenaString(
|
||||||
|
language), modelName);
|
||||||
|
try {
|
||||||
|
rdfService.changeSetUpdate(cs);
|
||||||
|
} catch (RDFServiceException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,8 +30,6 @@ public class RDFServiceUtils {
|
||||||
|
|
||||||
private static final String RDFSERVICEFACTORY_ATTR =
|
private static final String RDFSERVICEFACTORY_ATTR =
|
||||||
RDFServiceUtils.class.getName() + ".RDFServiceFactory";
|
RDFServiceUtils.class.getName() + ".RDFServiceFactory";
|
||||||
private static final String RDFSERVICEFACTORY_FILTERING_ATTR =
|
|
||||||
RDFServiceUtils.class.getName() + ".RDFServiceFactory.Filtering";
|
|
||||||
|
|
||||||
|
|
||||||
public static RDFServiceFactory getRDFServiceFactory(ServletContext context) {
|
public static RDFServiceFactory getRDFServiceFactory(ServletContext context) {
|
||||||
|
@ -80,11 +78,27 @@ public class RDFServiceUtils {
|
||||||
return "RDF/XML";
|
return "RDF/XML";
|
||||||
case N3:
|
case N3:
|
||||||
return "N3";
|
return "N3";
|
||||||
|
case NTRIPLE:
|
||||||
|
return "N-TRIPLE";
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("unexpected format in getSerializationFormatString");
|
throw new RuntimeException("unexpected format in getSerializationFormatString");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ModelSerializationFormat getSerializationFormatFromJenaString(String jenaString) {
|
||||||
|
if ("N3".equals(jenaString) || "TTL".equals(jenaString)
|
||||||
|
|| "TURTLE".equals(jenaString)) {
|
||||||
|
return ModelSerializationFormat.N3;
|
||||||
|
} else if ("N-TRIPLE".equals(jenaString)) {
|
||||||
|
return ModelSerializationFormat.NTRIPLE;
|
||||||
|
} else if ("RDF/XML".equals(jenaString)
|
||||||
|
|| "RDF/XML-ABBREV".equals(jenaString)) {
|
||||||
|
return ModelSerializationFormat.RDFXML;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("unrecognized format " + jenaString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static RDFService getRDFService(VitroRequest vreq) {
|
public static RDFService getRDFService(VitroRequest vreq) {
|
||||||
return getRDFServiceFactory(
|
return getRDFServiceFactory(
|
||||||
vreq.getSession().getServletContext()).getRDFService();
|
vreq.getSession().getServletContext()).getRDFService();
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
<option value="RDF/XML">RDF/XML</option>
|
<option value="RDF/XML">RDF/XML</option>
|
||||||
<option value="RDF/XML-ABBREV">RDF/XML abbrev.</option>
|
<option value="RDF/XML-ABBREV">RDF/XML abbrev.</option>
|
||||||
<option value="N3">N3</option>
|
<option value="N3">N3</option>
|
||||||
<option value="N-TRIPLES">N-Triples</option>
|
<option value="N-TRIPLE">N-Triples</option>
|
||||||
<option value="TURTLE">Turtle</option>
|
<option value="TURTLE">Turtle</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@
|
||||||
<input type="radio" name="format" value="RDF/XML-ABBREV" checked="checked" selected="selected"/> RDF/XML abbreviated
|
<input type="radio" name="format" value="RDF/XML-ABBREV" checked="checked" selected="selected"/> RDF/XML abbreviated
|
||||||
<input type="radio" name="format" value="RDF/XML"/> RDF/XML
|
<input type="radio" name="format" value="RDF/XML"/> RDF/XML
|
||||||
<input type="radio" name="format" value="N3"/> N3
|
<input type="radio" name="format" value="N3"/> N3
|
||||||
<input type="radio" name="format" value="N-TRIPLES"/> N-Triples
|
<input type="radio" name="format" value="N-TRIPLE"/> N-Triples
|
||||||
<input type="radio" name="format" value="TURTLE"/> Turtle
|
<input type="radio" name="format" value="TURTLE"/> Turtle
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue