Search in sources :

Example 1 with GraphQueryResult

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

Example 2 with GraphQueryResult

use of org.eclipse.rdf4j.query.GraphQueryResult in project rdf4j by eclipse.

the class SPARQLConnection method getStatementGeneral.

private RepositoryResult<Statement> getStatementGeneral(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    GraphQueryResult gRes = null;
    RepositoryResult<Statement> result = null;
    boolean allGood = false;
    try {
        GraphQuery query = prepareGraphQuery(SPARQL, EVERYTHING, "");
        query.setIncludeInferred(includeInferred);
        setBindings(query, subj, pred, obj, contexts);
        gRes = query.evaluate();
        result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(gRes) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (gRes != null) {
                    gRes.close();
                }
            }
        }
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SPARQLGraphQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery) GraphQuery(org.eclipse.rdf4j.query.GraphQuery) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Aggregations

GraphQueryResult (org.eclipse.rdf4j.query.GraphQueryResult)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)1 Statement (org.eclipse.rdf4j.model.Statement)1 GraphQuery (org.eclipse.rdf4j.query.GraphQuery)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)1 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)1 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)1 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)1 SPARQLGraphQuery (org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery)1 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)1 RDFParser (org.eclipse.rdf4j.rio.RDFParser)1