Search in sources :

Example 1 with TupleQueryResultParser

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

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

HttpResponse (org.apache.http.HttpResponse)2 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)2 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)2 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)2 TupleQueryResultParser (org.eclipse.rdf4j.query.resultio.TupleQueryResultParser)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)1