use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RDF4JProtocolSession method commitTransaction.
public synchronized void commitTransaction() throws RDF4JException, IOException, UnauthorizedException {
checkRepositoryURL();
if (transactionURL == null) {
throw new IllegalStateException("Transaction URL has not been set");
}
HttpPut method = null;
try {
URIBuilder url = new URIBuilder(transactionURL);
url.addParameter(Protocol.ACTION_PARAM_NAME, Action.COMMIT.toString());
method = new HttpPut(url.build());
final HttpResponse response = execute(method);
try {
int code = response.getStatusLine().getStatusCode();
if (code == HttpURLConnection.HTTP_OK) {
// we're done.
transactionURL = null;
if (ping != null) {
ping.cancel(false);
}
} else {
throw new RepositoryException("unable to commit transaction. HTTP error code " + code);
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
} catch (URISyntaxException e) {
logger.error("could not create URL for transaction commit", e);
throw new RuntimeException(e);
} finally {
if (method != null) {
method.reset();
}
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RDF4JProtocolSession method getStatements.
/*---------------------------*
* Get/add/remove statements *
*---------------------------*/
public void getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws IOException, RDFHandlerException, RepositoryException, UnauthorizedException, QueryInterruptedException {
checkRepositoryURL();
try {
String transactionURL = getTransactionURL();
final boolean useTransaction = transactionURL != null;
String baseLocation = useTransaction ? transactionURL : Protocol.getStatementsLocation(getQueryURL());
URIBuilder url = new URIBuilder(baseLocation);
if (subj != null) {
url.setParameter(Protocol.SUBJECT_PARAM_NAME, Protocol.encodeValue(subj));
}
if (pred != null) {
url.setParameter(Protocol.PREDICATE_PARAM_NAME, Protocol.encodeValue(pred));
}
if (obj != null) {
url.setParameter(Protocol.OBJECT_PARAM_NAME, Protocol.encodeValue(obj));
}
for (String encodedContext : Protocol.encodeContexts(contexts)) {
url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContext);
}
url.setParameter(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred));
if (useTransaction) {
url.setParameter(Protocol.ACTION_PARAM_NAME, Action.GET.toString());
}
HttpRequestBase method = useTransaction ? new HttpPut(url.build()) : new HttpGet(url.build());
try {
getRDF(method, handler, true);
} catch (MalformedQueryException e) {
logger.warn("Server reported unexpected malfored query error", e);
throw new RepositoryException(e.getMessage(), e);
} finally {
method.reset();
}
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
pingTransaction();
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RDF4JProtocolSession method getServerProtocol.
/*------------------*
* Protocol version *
*------------------*/
public String getServerProtocol() throws IOException, RepositoryException, UnauthorizedException {
checkServerURL();
HttpGet method = new HttpGet(Protocol.getProtocolLocation(serverURL));
try {
return EntityUtils.toString(executeOK(method).getEntity());
} catch (RepositoryException e) {
throw e;
} catch (RDF4JException e) {
throw new RepositoryException(e);
} finally {
method.reset();
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDF.
/**
* Parse the response in this thread using the provided {@link RDFHandler}. All HTTP connections are
* closed and released in this method
*/
protected void getRDF(HttpUriRequest method, RDFHandler handler, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
// 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");
}
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
String mimeType = getResponseMIMEType(response);
try {
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());
parser.setRDFHandler(handler);
parser.parse(response.getEntity().getContent(), method.getURI().toASCIIString());
} catch (RDFParseException 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 execute.
protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException {
boolean consume = true;
if (params != null) {
method.setParams(params);
}
HttpResponse response = httpClient.execute(method, httpContext);
try {
int httpCode = response.getStatusLine().getStatusCode();
if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) {
consume = false;
// everything OK, control flow can continue
return response;
} else {
switch(httpCode) {
case // 401
HttpURLConnection.HTTP_UNAUTHORIZED:
throw new UnauthorizedException();
case // 503
HttpURLConnection.HTTP_UNAVAILABLE:
throw new QueryInterruptedException();
default:
ErrorInfo errInfo = getErrorInfo(response);
// Throw appropriate exception
if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) {
throw new RDFParseException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) {
throw new UnsupportedRDFormatException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) {
throw new MalformedQueryException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) {
throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage());
} else if (errInfo.toString().length() > 0) {
throw new RepositoryException(errInfo.toString());
} else {
throw new RepositoryException(response.getStatusLine().getReasonPhrase());
}
}
}
} finally {
if (consume) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
Aggregations