From b005179a0be2d0c3f2036c41f76b4f2ba0165b66 Mon Sep 17 00:00:00 2001 From: briancaruso Date: Fri, 21 Sep 2012 18:22:09 +0000 Subject: [PATCH] Adding a RDFService that does not check query syntax --- .../impl/sparql/RDFServiceSparql.java | 4 +- .../impl/sparql/RDFServiceSparqlHttp.java | 167 ++++++++++++++++++ 2 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparqlHttp.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparql.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparql.java index 153fce696..113a3d3b3 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparql.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparql.java @@ -59,8 +59,8 @@ import edu.cornell.mannlib.vitro.webapp.web.URLEncoder; public class RDFServiceSparql extends RDFServiceImpl implements RDFService { private static final Log log = LogFactory.getLog(RDFServiceImpl.class); - private String readEndpointURI; - private String updateEndpointURI; + protected String readEndpointURI; + protected String updateEndpointURI; private HTTPRepository readRepository; private HTTPRepository updateRepository; private HttpClient httpClient; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparqlHttp.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparqlHttp.java new file mode 100644 index 000000000..0752ab0ba --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sparql/RDFServiceSparqlHttp.java @@ -0,0 +1,167 @@ +package edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sparql; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.ResultSetFormatter; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; + +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceImpl; + +/** + * This is a RDFService that passes SPARQL queries to + * the SPARQL endpoint unaltered and without parsing them. + * + * This is useful if the endpoint accepts syntax that does + * not pass the ARQ SPARQL 1.1 parser. The disadvantage + * of this is that it currently returns no useful debugging + * messages when there is a syntax error. + */ +public class RDFServiceSparqlHttp extends RDFServiceSparql { + + public RDFServiceSparqlHttp(String readEndpointURI, String updateEndpointURI) { + super(readEndpointURI, updateEndpointURI); + } + + public RDFServiceSparqlHttp(String endpointURI) { + super(endpointURI); + } + + public RDFServiceSparqlHttp(String readEndpointURI, + String updateEndpointURI, String defaultWriteGraphURI) { + super(readEndpointURI, updateEndpointURI, defaultWriteGraphURI); + } + + + /** + * Performs a SPARQL construct query against the knowledge base. The query may have + * an embedded graph identifier. + * + * @param String query - the SPARQL query to be executed against the RDF store + * @param RDFService.ModelSerializationFormat resultFormat - type of serialization for RDF result of the SPARQL query + * @param OutputStream outputStream - the result of the query + * + */ + @Override + public InputStream sparqlConstructQuery(String queryStr, + RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException { + + Model model = ModelFactory.createDefaultModel(); + //Query query = QueryFactory.create(queryStr); + //QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, queryStr); + + QueryEngineHTTP qeh = new QueryEngineHTTP( readEndpointURI, queryStr); + try { + qeh.execConstruct(model); + } finally { + qeh.close(); + } + + ByteArrayOutputStream serializedModel = new ByteArrayOutputStream(); + model.write(serializedModel,getSerializationFormatString(resultFormat)); + InputStream result = new ByteArrayInputStream(serializedModel.toByteArray()); + return result; + } + + /** + * Performs a SPARQL describe query against the knowledge base. The query may have + * an embedded graph identifier. + * + * @param String query - the SPARQL query to be executed against the RDF store + * @param RDFService.ModelSerializationFormat resultFormat - type of serialization for RDF result of the SPARQL query + * + * @return InputStream - the result of the query + * + */ + @Override + public InputStream sparqlDescribeQuery(String queryStr, + RDFServiceImpl.ModelSerializationFormat resultFormat) throws RDFServiceException { + + Model model = ModelFactory.createDefaultModel(); + QueryEngineHTTP qeh = new QueryEngineHTTP( readEndpointURI, queryStr); + + try { + qeh.execDescribe(model); + } finally { + qeh.close(); + } + + ByteArrayOutputStream serializedModel = new ByteArrayOutputStream(); + model.write(serializedModel,getSerializationFormatString(resultFormat)); + InputStream result = new ByteArrayInputStream(serializedModel.toByteArray()); + return result; + } + + /** + * Performs a SPARQL select query against the knowledge base. The query may have + * an embedded graph identifier. + * + * @param String query - the SPARQL query to be executed against the RDF store + * @param RDFService.ResultFormat resultFormat - format for the result of the Select query + * + * @return InputStream - the result of the query + * + */ + @Override + public InputStream sparqlSelectQuery(String queryStr, RDFService.ResultFormat resultFormat) throws RDFServiceException { + + QueryEngineHTTP qeh = new QueryEngineHTTP( readEndpointURI, queryStr); + + try { + ResultSet resultSet = qeh.execSelect(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + switch (resultFormat) { + case CSV: + ResultSetFormatter.outputAsCSV(outputStream,resultSet); + break; + case TEXT: + ResultSetFormatter.out(outputStream,resultSet); + break; + case JSON: + ResultSetFormatter.outputAsJSON(outputStream, resultSet); + break; + case XML: + ResultSetFormatter.outputAsXML(outputStream, resultSet); + break; + default: + throw new RDFServiceException("unrecognized result format"); + } + + InputStream result = new ByteArrayInputStream(outputStream.toByteArray()); + return result; + + } finally { + qeh.close(); + } + } + + /** + * Performs a SPARQL ASK query against the knowledge base. The query may have + * an embedded graph identifier. + * + * @param String query - the SPARQL query to be executed against the RDF store + * + * @return boolean - the result of the SPARQL query + */ + @Override + public boolean sparqlAskQuery(String queryStr) throws RDFServiceException { + + QueryEngineHTTP qeh = new QueryEngineHTTP( readEndpointURI, queryStr); + + try { + return qeh.execAsk(); + } finally { + qeh.close(); + } + } + +}