Search in sources :

Example 1 with BooleanQueryResultParser

use of org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser 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)

Aggregations

HttpResponse (org.apache.http.HttpResponse)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1 BooleanQueryResultParser (org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser)1 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)1 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)1 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)1 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)1 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)1