use of org.eclipse.rdf4j.query.GraphQueryResult in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDFBackground.
/**
* Parse the response in a background thread. HTTP connections are dealt with in the
* {@link BackgroundGraphResult} or (in the error-case) in this method.
*/
protected GraphQueryResult getRDFBackground(HttpUriRequest method, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
boolean submitted = false;
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
GraphQueryResult gRes = null;
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setParserConfig(getParserConfig());
parser.setParseErrorListener(new ParseErrorLogger());
Charset charset = null;
// SES-1793 : Do not attempt to check for a charset if the format is
// defined not to have a charset
// This prevents errors caused by people erroneously attaching a
// charset to a binary formatted document
HttpEntity entity = response.getEntity();
if (format.hasCharset() && entity != null && entity.getContentType() != null) {
// required?
try {
charset = ContentType.parse(entity.getContentType().getValue()).getCharset();
} catch (IllegalCharsetNameException e) {
// work around for Joseki-3.2
// Content-Type: application/rdf+xml;
// charset=application/rdf+xml
}
if (charset == null) {
charset = UTF8;
}
}
if (entity == null) {
throw new RepositoryException("Server response was empty.");
}
String baseURI = method.getURI().toASCIIString();
gRes = background.parse(parser, entity.getContent(), charset, baseURI);
submitted = true;
return gRes;
} finally {
if (!submitted) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.query.GraphQueryResult in project rdf4j by eclipse.
the class SPARQLConnection method getStatementGeneral.
private RepositoryResult<Statement> getStatementGeneral(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
GraphQueryResult gRes = null;
RepositoryResult<Statement> result = null;
boolean allGood = false;
try {
GraphQuery query = prepareGraphQuery(SPARQL, EVERYTHING, "");
query.setIncludeInferred(includeInferred);
setBindings(query, subj, pred, obj, contexts);
gRes = query.evaluate();
result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(gRes) {
@Override
protected RepositoryException convert(Exception e) {
return new RepositoryException(e);
}
});
allGood = true;
return result;
} finally {
if (!allGood) {
try {
if (result != null) {
result.close();
}
} finally {
if (gRes != null) {
gRes.close();
}
}
}
}
}
Aggregations