Search in sources :

Example 11 with QueryResultHandlerException

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

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

the class BooleanTextParser method parse.

@Override
public synchronized boolean parse(InputStream in) throws IOException, QueryResultParseException {
    Reader reader = new InputStreamReader(in, Charset.forName("US-ASCII"));
    String value = IOUtil.readString(reader, 16);
    value = value.trim();
    boolean result = false;
    if (value.equalsIgnoreCase("true")) {
        result = true;
    } else if (value.equalsIgnoreCase("false")) {
        result = false;
    } else {
        throw new QueryResultParseException("Invalid value: " + value);
    }
    if (this.handler != null) {
        try {
            this.handler.handleBoolean(result);
        } catch (QueryResultHandlerException e) {
            if (e.getCause() != null && e.getCause() instanceof IOException) {
                throw (IOException) e.getCause();
            } else {
                throw new QueryResultParseException("Found an issue with the query result handler", e);
            }
        }
    }
    return result;
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 13 with QueryResultHandlerException

use of org.eclipse.rdf4j.query.QueryResultHandlerException 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 14 with QueryResultHandlerException

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

the class AbstractSPARQLXMLWriter method startDocument.

@Override
public void startDocument() throws QueryResultHandlerException {
    if (!documentOpen) {
        documentOpen = true;
        headerOpen = false;
        headerComplete = false;
        tupleVariablesFound = false;
        try {
            xmlWriter.setPrettyPrint(getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT));
            if (getWriterConfig().get(XMLWriterSettings.INCLUDE_XML_PI)) {
                xmlWriter.startDocument();
            }
            xmlWriter.setAttribute("xmlns", NAMESPACE);
            if (getWriterConfig().get(BasicQueryWriterSettings.ADD_SESAME_QNAME)) {
                xmlWriter.setAttribute("xmlns:q", SESAMEQNAME.NAMESPACE);
            }
            for (String nextPrefix : namespaceTable.keySet()) {
                this.log.debug("Adding custom prefix for <{}> to map to <{}>", nextPrefix, namespaceTable.get(nextPrefix));
                xmlWriter.setAttribute("xmlns:" + namespaceTable.get(nextPrefix), nextPrefix);
            }
        } catch (IOException e) {
            throw new QueryResultHandlerException(e);
        }
    }
}
Also used : IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 15 with QueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method startQueryResult.

@Override
public void startQueryResult(List<String> columnHeaders) throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        tupleVariablesFound = true;
        jg.writeArrayFieldStart("vars");
        for (String nextColumn : columnHeaders) {
            jg.writeString(nextColumn);
        }
        jg.writeEndArray();
    } 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)

Aggregations

QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)20 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)18 IOException (java.io.IOException)16 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)3 Binding (org.eclipse.rdf4j.query.Binding)2 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)1 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 Indenter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter.Indenter)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 HttpResponse (org.apache.http.HttpResponse)1 UncloseableInputStream (org.eclipse.rdf4j.common.io.UncloseableInputStream)1 SimpleSAXParser (org.eclipse.rdf4j.common.xml.SimpleSAXParser)1 TupleQueryResultBuilder (org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)1 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)1 TupleQueryResultParser (org.eclipse.rdf4j.query.resultio.TupleQueryResultParser)1