use of org.eclipse.rdf4j.query.resultio.TupleQueryResultParser in project rdf4j by eclipse.
the class SPARQLProtocolSession method getBackgroundTupleQueryResult.
/*------------------*
* Response parsing *
*------------------*/
/**
* Parse the response in a background thread. HTTP connections are dealt with in the
* {@link BackgroundTupleResult} or (in the error-case) in this method.
*/
protected TupleQueryResult getBackgroundTupleQueryResult(HttpUriRequest method) throws RepositoryException, QueryInterruptedException, MalformedQueryException, IOException {
boolean submitted = false;
// 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");
}
TupleQueryResult tRes = null;
// send the tuple query
HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory());
tRes = background.parse(parser, response.getEntity().getContent());
submitted = true;
return tRes;
} finally {
if (!submitted) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.query.resultio.TupleQueryResultParser 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());
}
}
Aggregations