Search in sources :

Example 11 with RepositoryException

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);
    }
}
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 12 with RepositoryException

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());
    }
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) QueryResultFormat(org.eclipse.rdf4j.query.resultio.QueryResultFormat) BooleanQueryResultFormat(org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat) TupleQueryResultFormat(org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) BooleanQueryResultParser(org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)

Example 13 with RepositoryException

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());
        }
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 14 with RepositoryException

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());
        }
    }
}
Also used : QueryResultFormat(org.eclipse.rdf4j.query.resultio.QueryResultFormat) BooleanQueryResultFormat(org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat) TupleQueryResultFormat(org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat) TupleQueryResultParser(org.eclipse.rdf4j.query.resultio.TupleQueryResultParser) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 15 with RepositoryException

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());
        }
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Charset(java.nio.charset.Charset) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Aggregations

RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)67 IOException (java.io.IOException)24 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)15 RDF4JException (org.eclipse.rdf4j.RDF4JException)13 HttpResponse (org.apache.http.HttpResponse)11 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)10 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 HttpGet (org.apache.http.client.methods.HttpGet)7 Resource (org.eclipse.rdf4j.model.Resource)7 Statement (org.eclipse.rdf4j.model.Statement)6 ArrayList (java.util.ArrayList)5 HttpPut (org.apache.http.client.methods.HttpPut)5 RDF4JProtocolSession (org.eclipse.rdf4j.http.client.RDF4JProtocolSession)5 UnauthorizedException (org.eclipse.rdf4j.http.protocol.UnauthorizedException)5 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4