Search in sources :

Example 1 with QueryResultFormat

use of org.eclipse.rdf4j.query.resultio.QueryResultFormat 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 2 with QueryResultFormat

use of org.eclipse.rdf4j.query.resultio.QueryResultFormat 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 3 with QueryResultFormat

use of org.eclipse.rdf4j.query.resultio.QueryResultFormat 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 4 with QueryResultFormat

use of org.eclipse.rdf4j.query.resultio.QueryResultFormat in project rdf4j by eclipse.

the class SPARQLProtocolSession method sendBooleanQueryViaHttp.

private HttpResponse sendBooleanQueryViaHttp(HttpUriRequest method, Set<QueryResultFormat> booleanFormats) throws IOException, RDF4JException {
    final List<String> acceptValues = new ArrayList<>(booleanFormats.size());
    for (QueryResultFormat format : booleanFormats) {
        // Determine a q-value that reflects the user specified preference
        int qValue = 10;
        if (preferredBQRFormat != null && !preferredBQRFormat.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));
    return executeOK(method);
}
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)

Example 5 with QueryResultFormat

use of org.eclipse.rdf4j.query.resultio.QueryResultFormat in project rdf4j by eclipse.

the class SPARQLProtocolSession method getTupleQueryResult.

/**
 * Parse the response in this thread using the provided {@link TupleQueryResultHandler}. All HTTP
 * connections are closed and released in this method
 */
protected void getTupleQueryResult(HttpUriRequest method, TupleQueryResultHandler handler) throws IOException, TupleQueryResultHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
    // 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");
    }
    // send the tuple query
    HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats);
    try {
        // if we get here, HTTP code is 200
        String mimeType = getResponseMIMEType(response);
        try {
            QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
            TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory());
            parser.setQueryResultHandler(handler);
            parser.parseQueryResult(response.getEntity().getContent());
        } catch (QueryResultParseException e) {
            throw new RepositoryException("Malformed query result from server", e);
        } catch (QueryResultHandlerException e) {
            if (e instanceof TupleQueryResultHandlerException) {
                throw (TupleQueryResultHandlerException) e;
            } else {
                throw new TupleQueryResultHandlerException(e);
            }
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) 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) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Aggregations

BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)5 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)5 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)4 HttpResponse (org.apache.http.HttpResponse)3 ArrayList (java.util.ArrayList)2 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)2 TupleQueryResultParser (org.eclipse.rdf4j.query.resultio.TupleQueryResultParser)2 RDF4JException (org.eclipse.rdf4j.RDF4JException)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 QueryInterruptedException (org.eclipse.rdf4j.query.QueryInterruptedException)1 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 BooleanQueryResultParser (org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser)1 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)1