use of org.eclipse.rdf4j.query.TupleQueryResultHandlerException in project rdf4j by eclipse.
the class SPARQLProtocolSession method getTupleQueryResult.
/**
* Parse the response in this thread using the provided {@link TupleQueryResultHandler}. All HTTP
* connections are closed and released in this method
*/
protected void getTupleQueryResult(HttpUriRequest method, TupleQueryResultHandler handler) throws IOException, TupleQueryResultHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
// Specify which formats we support
Set<QueryResultFormat> tqrFormats = TupleQueryResultParserRegistry.getInstance().getKeys();
if (tqrFormats.isEmpty()) {
throw new RepositoryException("No tuple query result parsers have been registered");
}
// send the tuple query
HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
try {
QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory());
parser.setQueryResultHandler(handler);
parser.parseQueryResult(response.getEntity().getContent());
} catch (QueryResultParseException e) {
throw new RepositoryException("Malformed query result from server", e);
} catch (QueryResultHandlerException e) {
if (e instanceof TupleQueryResultHandlerException) {
throw (TupleQueryResultHandlerException) e;
} else {
throw new TupleQueryResultHandlerException(e);
}
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
}
use of org.eclipse.rdf4j.query.TupleQueryResultHandlerException in project rdf4j by eclipse.
the class SPARQLResultsCSVWriter method handleSolution.
@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
if (bindingNames == null) {
throw new IllegalStateException("Must call startQueryResult before handleSolution");
}
try {
for (int i = 0; i < bindingNames.size(); i++) {
String name = bindingNames.get(i);
Value value = bindingSet.getValue(name);
if (value != null) {
writeValue(value);
}
if (i < bindingNames.size() - 1) {
writer.write(",");
}
}
writer.write("\r\n");
} catch (IOException e) {
throw new TupleQueryResultHandlerException(e);
}
}
use of org.eclipse.rdf4j.query.TupleQueryResultHandlerException in project rdf4j by eclipse.
the class SPARQLResultsTSVWriter method startQueryResult.
@Override
public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
tupleVariablesFound = true;
this.bindingNames = bindingNames;
try {
for (int i = 0; i < bindingNames.size(); i++) {
// mandatory prefix in TSV
writer.write("?");
writer.write(bindingNames.get(i));
if (i < bindingNames.size() - 1) {
writer.write("\t");
}
}
writer.write("\n");
} catch (IOException e) {
throw new TupleQueryResultHandlerException(e);
}
}
use of org.eclipse.rdf4j.query.TupleQueryResultHandlerException in project rdf4j by eclipse.
the class AbstractSPARQLXMLWriter method endQueryResult.
@Override
public void endQueryResult() throws TupleQueryResultHandlerException {
try {
if (!documentOpen) {
startDocument();
}
if (!headerOpen) {
startHeader();
}
if (!headerComplete) {
endHeader();
}
if (!tupleVariablesFound) {
throw new IllegalStateException("Could not end query result as startQueryResult was not called first.");
}
xmlWriter.endTag(RESULT_SET_TAG);
endDocument();
} catch (IOException e) {
throw new TupleQueryResultHandlerException(e);
} catch (TupleQueryResultHandlerException e) {
throw e;
} catch (QueryResultHandlerException e) {
throw new TupleQueryResultHandlerException(e);
}
}
use of org.eclipse.rdf4j.query.TupleQueryResultHandlerException in project rdf4j by eclipse.
the class DAWGTestResultSetParser method endRDF.
@Override
public void endRDF() throws RDFHandlerException {
try {
Resource resultSetNode = GraphUtil.getUniqueSubject(graph, RDF.TYPE, RESULTSET);
List<String> bindingNames = getBindingNames(resultSetNode);
tqrHandler.startQueryResult(bindingNames);
Iterator<Value> solIter = GraphUtil.getObjectIterator(graph, resultSetNode, SOLUTION);
while (solIter.hasNext()) {
Value solutionNode = solIter.next();
if (solutionNode instanceof Resource) {
reportSolution((Resource) solutionNode, bindingNames);
} else {
throw new RDFHandlerException("Value for " + SOLUTION + " is not a resource: " + solutionNode);
}
}
tqrHandler.endQueryResult();
} catch (GraphUtilException e) {
throw new RDFHandlerException(e.getMessage(), e);
} catch (TupleQueryResultHandlerException e) {
throw new RDFHandlerException(e.getMessage(), e);
}
}
Aggregations