use of org.eclipse.rdf4j.query.MalformedQueryException 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());
}
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException 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.query.MalformedQueryException in project rdf4j by eclipse.
the class SPARQLUpdateOperation method execute.
public void execute(RepositoryConnection con) throws RepositoryException {
try {
Update preparedUpdate = con.prepareUpdate(QueryLanguage.SPARQL, getUpdateString(), getBaseURI());
preparedUpdate.setIncludeInferred(isIncludeInferred());
preparedUpdate.setDataset(getDataset());
if (getBindings() != null) {
for (Binding binding : getBindings()) {
preparedUpdate.setBinding(binding.getName(), binding.getValue());
}
}
preparedUpdate.execute();
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (UpdateExecutionException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class HTTPUpdate method execute.
@Override
public void execute() throws UpdateExecutionException {
try {
if (httpCon.getRepository().useCompatibleMode()) {
if (httpCon.isAutoCommit()) {
// execute update immediately
SPARQLProtocolSession client = getHttpClient();
try {
client.sendUpdate(getQueryLanguage(), getQueryString(), getBaseURI(), dataset, includeInferred, getMaxExecutionTime(), getBindingsArray());
} catch (UnauthorizedException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (QueryInterruptedException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (MalformedQueryException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (IOException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
}
} else {
// defer execution as part of transaction.
httpCon.scheduleUpdate(this);
}
return;
}
SPARQLProtocolSession client = getHttpClient();
try {
httpCon.flushTransactionState(Action.UPDATE);
client.sendUpdate(getQueryLanguage(), getQueryString(), getBaseURI(), dataset, includeInferred, getMaxExecutionTime(), getBindingsArray());
} catch (UnauthorizedException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (QueryInterruptedException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (MalformedQueryException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
} catch (IOException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
}
} catch (RepositoryException e) {
throw new HTTPUpdateExecutionException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class SPARQLConnection method exportStatements.
public void exportStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws RepositoryException, RDFHandlerException {
try {
GraphQuery query = prepareGraphQuery(SPARQL, EVERYTHING, "");
setBindings(query, subj, pred, obj, contexts);
query.evaluate(handler);
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
}
}
Aggregations