VIVO-564 reimplemented RDFServiceSparql.sparqlSelectQuery() using direct HttpClient calls instead of using Jena's stuff

This commit is contained in:
brianjlowe 2013-11-21 16:14:26 -05:00
parent 3bca31ea14
commit 11d766ba34

View file

@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sparql;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,7 +15,9 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils; 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;
@ -38,6 +41,7 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraph; import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraph;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeListener; import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeListener;
@ -84,11 +88,11 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
this.readRepository = new HTTPRepository(readEndpointURI); this.readRepository = new HTTPRepository(readEndpointURI);
this.updateRepository = new HTTPRepository(updateEndpointURI); this.updateRepository = new HTTPRepository(updateEndpointURI);
testConnection();
MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
mgr.getParams().setDefaultMaxConnectionsPerHost(10); mgr.getParams().setDefaultMaxConnectionsPerHost(50);
this.httpClient = new HttpClient(mgr); this.httpClient = new HttpClient(mgr);
testConnection();
} }
private void testConnection() { private void testConnection() {
@ -278,13 +282,28 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
*/ */
@Override @Override
public InputStream sparqlSelectQuery(String queryStr, RDFService.ResultFormat resultFormat) throws RDFServiceException { public InputStream sparqlSelectQuery(String queryStr, RDFService.ResultFormat resultFormat) throws RDFServiceException {
Query query = createQuery(queryStr); //QueryEngineHTTP qh = new QueryEngineHTTP(readEndpointURI, queryStr);
QueryExecution qe = QueryExecutionFactory.sparqlService(readEndpointURI, query);
GetMethod meth = new GetMethod(readEndpointURI);
try { try {
ResultSet resultSet = qe.execSelect(); meth.addRequestHeader("Accept", "application/sparql-results+xml");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NameValuePair param = new NameValuePair();
param.setName("query");
param.setValue(queryStr);
NameValuePair[] params = new NameValuePair[1];
params[0] = param;
meth.setQueryString(params);
int response = httpClient.executeMethod(meth);
if (response > 399) {
log.error("response " + response + " to query. \n");
log.debug("update string: \n" + queryStr);
throw new RDFServiceException("Unable to perform SPARQL UPDATE");
}
InputStream in = meth.getResponseBodyAsStream();
ResultSet resultSet = ResultSetFactory.fromXML(in);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
switch (resultFormat) { switch (resultFormat) {
case CSV: case CSV:
@ -301,12 +320,14 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
break; break;
default: default:
throw new RDFServiceException("unrecognized result format"); throw new RDFServiceException("unrecognized result format");
} }
InputStream result = new ByteArrayInputStream(outputStream.toByteArray()); InputStream result = new ByteArrayInputStream(outputStream.toByteArray());
return result; return result;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} finally { } finally {
qe.close(); //qh.close();
meth.releaseConnection();
} }
} }
@ -474,7 +495,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
int response = httpClient.executeMethod(meth); int response = httpClient.executeMethod(meth);
if (response > 399) { if (response > 399) {
log.error("response " + response + " to update. \n"); log.error("response " + response + " to update. \n");
log.debug("update string: \n" + updateString); //log.debug("update string: \n" + updateString);
throw new RDFServiceException("Unable to perform SPARQL UPDATE"); throw new RDFServiceException("Unable to perform SPARQL UPDATE");
} }
} finally { } finally {