use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLProtocolSession method sendTupleQueryViaHttp.
/**
* Send the tuple query via HTTP and throws an exception in case anything goes wrong, i.e. only for HTTP
* 200 the method returns without exception. If HTTP status code is not equal to 200, the request is
* aborted, however pooled connections are not released.
*
* @param method
* @throws RepositoryException
* @throws HttpException
* @throws IOException
* @throws QueryInterruptedException
* @throws MalformedQueryException
*/
private HttpResponse sendTupleQueryViaHttp(HttpUriRequest method, Set<QueryResultFormat> tqrFormats) throws RepositoryException, IOException, QueryInterruptedException, MalformedQueryException {
final List<String> acceptValues = new ArrayList<String>(tqrFormats.size());
for (QueryResultFormat format : tqrFormats) {
// Determine a q-value that reflects the user specified preference
int qValue = 10;
if (preferredTQRFormat != null && !preferredTQRFormat.equals(format)) {
// Prefer specified format over other formats
qValue -= 2;
}
for (String mimeType : format.getMIMETypes()) {
String acceptParam = mimeType;
if (qValue < 10) {
acceptParam += ";q=0." + qValue;
}
acceptValues.add(acceptParam);
}
}
method.addHeader(ACCEPT_PARAM_NAME, String.join(", ", acceptValues));
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 getBoolean.
/**
* Parse the response in this thread using a suitable {@link BooleanQueryResultParser}. All HTTP
* connections are closed and released in this method
*
* @throws RDF4JException
*/
protected boolean getBoolean(HttpUriRequest method) throws IOException, RDF4JException {
// Specify which formats we support using Accept headers
Set<QueryResultFormat> booleanFormats = BooleanQueryResultParserRegistry.getInstance().getKeys();
if (booleanFormats.isEmpty()) {
throw new RepositoryException("No boolean query result parsers have been registered");
}
// send the tuple query
HttpResponse response = sendBooleanQueryViaHttp(method, booleanFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
try {
QueryResultFormat format = BooleanQueryResultFormat.matchMIMEType(mimeType, booleanFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
BooleanQueryResultParser parser = QueryResultIO.createBooleanParser(format);
QueryResultCollector results = new QueryResultCollector();
parser.setQueryResultHandler(results);
parser.parseQueryResult(response.getEntity().getContent());
return results.getBoolean();
} catch (QueryResultParseException e) {
throw new RepositoryException("Malformed query result from server", e);
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLProtocolSession method executeOK.
/**
* Convenience method to deal with HTTP level errors of tuple, graph and boolean queries in the same way.
* This method aborts the HTTP connection.
*
* @param method
* @throws RDF4JException
*/
protected HttpResponse executeOK(HttpUriRequest method) throws IOException, RDF4JException {
boolean fail = true;
HttpResponse response = execute(method);
try {
int httpCode = response.getStatusLine().getStatusCode();
if (httpCode == HttpURLConnection.HTTP_OK || httpCode == HttpURLConnection.HTTP_NOT_AUTHORITATIVE) {
fail = false;
// everything OK, control flow can continue
return response;
} else {
// trying to contact a non-SPARQL server?
throw new RepositoryException("Failed to get server protocol; no such resource on this server: " + method.getURI().toString());
}
} finally {
if (fail) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.repository.RepositoryException 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.repository.RepositoryException 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());
}
}
}
Aggregations