Search in sources :

Example 6 with MalformedQueryException

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());
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) ErrorInfo(org.eclipse.rdf4j.http.protocol.error.ErrorInfo) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) HttpResponse(org.apache.http.HttpResponse) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 7 with MalformedQueryException

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);
    }
}
Also used : QueryResultFormat(org.eclipse.rdf4j.query.resultio.QueryResultFormat) BooleanQueryResultFormat(org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat) TupleQueryResultFormat(org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat) ArrayList(java.util.ArrayList) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RDF4JException(org.eclipse.rdf4j.RDF4JException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException)

Example 8 with MalformedQueryException

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);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) Update(org.eclipse.rdf4j.query.Update)

Example 9 with MalformedQueryException

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);
    }
}
Also used : SPARQLProtocolSession(org.eclipse.rdf4j.http.client.SPARQLProtocolSession) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException)

Example 10 with MalformedQueryException

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);
    }
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SPARQLGraphQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery) GraphQuery(org.eclipse.rdf4j.query.GraphQuery)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)33 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)9 IOException (java.io.IOException)6 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)5 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)5 HttpGet (org.apache.http.client.methods.HttpGet)4 QueryInterruptedException (org.eclipse.rdf4j.query.QueryInterruptedException)4 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)4 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)4 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)3 SPARQLProtocolSession (org.eclipse.rdf4j.http.client.SPARQLProtocolSession)3 Binding (org.eclipse.rdf4j.query.Binding)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)3 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)3 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)3 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)3 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)3 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)3