Search in sources :

Example 11 with TupleQueryResultHandlerException

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

Example 12 with TupleQueryResultHandlerException

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

the class SPARQLResultsCSVWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    if (bindingNames == null) {
        throw new IllegalStateException("Must call startQueryResult before handleSolution");
    }
    try {
        for (int i = 0; i < bindingNames.size(); i++) {
            String name = bindingNames.get(i);
            Value value = bindingSet.getValue(name);
            if (value != null) {
                writeValue(value);
            }
            if (i < bindingNames.size() - 1) {
                writer.write(",");
            }
        }
        writer.write("\r\n");
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Example 13 with TupleQueryResultHandlerException

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

the class SPARQLResultsTSVWriter method startQueryResult.

@Override
public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    tupleVariablesFound = true;
    this.bindingNames = bindingNames;
    try {
        for (int i = 0; i < bindingNames.size(); i++) {
            // mandatory prefix in TSV
            writer.write("?");
            writer.write(bindingNames.get(i));
            if (i < bindingNames.size() - 1) {
                writer.write("\t");
            }
        }
        writer.write("\n");
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException)

Example 14 with TupleQueryResultHandlerException

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

the class AbstractSPARQLXMLWriter method endQueryResult.

@Override
public void endQueryResult() throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        if (!headerComplete) {
            endHeader();
        }
        if (!tupleVariablesFound) {
            throw new IllegalStateException("Could not end query result as startQueryResult was not called first.");
        }
        xmlWriter.endTag(RESULT_SET_TAG);
        endDocument();
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    } catch (TupleQueryResultHandlerException e) {
        throw e;
    } catch (QueryResultHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 15 with TupleQueryResultHandlerException

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

the class DAWGTestResultSetParser method endRDF.

@Override
public void endRDF() throws RDFHandlerException {
    try {
        Resource resultSetNode = GraphUtil.getUniqueSubject(graph, RDF.TYPE, RESULTSET);
        List<String> bindingNames = getBindingNames(resultSetNode);
        tqrHandler.startQueryResult(bindingNames);
        Iterator<Value> solIter = GraphUtil.getObjectIterator(graph, resultSetNode, SOLUTION);
        while (solIter.hasNext()) {
            Value solutionNode = solIter.next();
            if (solutionNode instanceof Resource) {
                reportSolution((Resource) solutionNode, bindingNames);
            } else {
                throw new RDFHandlerException("Value for " + SOLUTION + " is not a resource: " + solutionNode);
            }
        }
        tqrHandler.endQueryResult();
    } catch (GraphUtilException e) {
        throw new RDFHandlerException(e.getMessage(), e);
    } catch (TupleQueryResultHandlerException e) {
        throw new RDFHandlerException(e.getMessage(), e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) GraphUtilException(org.eclipse.rdf4j.model.util.GraphUtilException)

Aggregations

TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)24 IOException (java.io.IOException)12 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)9 Value (org.eclipse.rdf4j.model.Value)6 Binding (org.eclipse.rdf4j.query.Binding)4 TupleQueryResultBuilder (org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder)4 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)4 BNode (org.eclipse.rdf4j.model.BNode)2 Literal (org.eclipse.rdf4j.model.Literal)2 Resource (org.eclipse.rdf4j.model.Resource)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 HttpResponse (org.apache.http.HttpResponse)1 Graph (org.eclipse.rdf4j.model.Graph)1 IRI (org.eclipse.rdf4j.model.IRI)1 GraphImpl (org.eclipse.rdf4j.model.impl.GraphImpl)1 GraphUtilException (org.eclipse.rdf4j.model.util.GraphUtilException)1 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)1 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)1 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1