use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLProtocolSession method sendGraphQueryViaHttp.
private HttpResponse sendGraphQueryViaHttp(HttpUriRequest method, boolean requireContext, Set<RDFFormat> rdfFormats) throws RepositoryException, IOException, QueryInterruptedException, MalformedQueryException {
List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, requireContext, getPreferredRDFFormat());
method.addHeader(ACCEPT_PARAM_NAME, String.join(", ", acceptParams));
try {
return executeOK(method);
} catch (RepositoryException | MalformedQueryException | QueryInterruptedException e) {
throw e;
} catch (RDF4JException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.repository.RepositoryException 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.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLConnection method hasStatement.
public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException {
try {
BooleanQuery query = prepareBooleanQuery(SPARQL, SOMETHING, "");
setBindings(query, subj, pred, obj, contexts);
return query.evaluate();
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.repository.RepositoryException 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();
}
}
}
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLConnection method getContextIDs.
public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
TupleQueryResult iter = null;
RepositoryResult<Resource> result = null;
boolean allGood = false;
try {
TupleQuery query = prepareTupleQuery(SPARQL, NAMEDGRAPHS, "");
iter = query.evaluate();
result = new RepositoryResult<Resource>(new ExceptionConvertingIteration<Resource, RepositoryException>(new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(iter) {
@Override
protected Resource convert(BindingSet bindings) throws QueryEvaluationException {
return (Resource) bindings.getValue("_");
}
}) {
@Override
protected RepositoryException convert(Exception e) {
return new RepositoryException(e);
}
});
allGood = true;
return result;
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
} finally {
if (!allGood) {
try {
if (result != null) {
result.close();
}
} finally {
if (iter != null) {
iter.close();
}
}
}
}
}
Aggregations