Search in sources :

Example 1 with QueryResultParseException

use of org.eclipse.rdf4j.query.resultio.QueryResultParseException in project rdf4j by eclipse.

the class BinaryQueryResultParser method processError.

private void processError() throws IOException, QueryResultParseException {
    byte errTypeFlag = in.readByte();
    QueryErrorType errType = null;
    if (errTypeFlag == MALFORMED_QUERY_ERROR) {
        errType = QueryErrorType.MALFORMED_QUERY_ERROR;
    } else if (errTypeFlag == QUERY_EVALUATION_ERROR) {
        errType = QueryErrorType.QUERY_EVALUATION_ERROR;
    } else {
        throw new QueryResultParseException("Unkown error type: " + errTypeFlag);
    }
    String msg = readString();
    // FIXME: is this the right thing to do upon encountering an error?
    throw new QueryResultParseException(errType + ": " + msg);
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException)

Example 2 with QueryResultParseException

use of org.eclipse.rdf4j.query.resultio.QueryResultParseException in project rdf4j by eclipse.

the class AbstractSPARQLXMLParser method parseQueryResultInternal.

protected boolean parseQueryResultInternal(InputStream in, boolean attemptParseBoolean, boolean attemptParseTuple) throws IOException, QueryResultParseException, QueryResultHandlerException {
    if (!attemptParseBoolean && !attemptParseTuple) {
        throw new IllegalArgumentException("Internal error: Did not specify whether to parse as either boolean and/or tuple");
    }
    BufferedInputStream buff = new BufferedInputStream(in);
    // Wrap in a custom InputStream that doesn't allow close to be called by dependencies before we are ready to call it
    UncloseableInputStream uncloseable = new UncloseableInputStream(buff);
    SAXException caughtException = null;
    boolean result = false;
    try {
        if (attemptParseBoolean) {
            buff.mark(Integer.MAX_VALUE);
            try {
                SPARQLBooleanSAXParser valueParser = new SPARQLBooleanSAXParser();
                XMLReader xmlReader;
                if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
                    xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
                } else {
                    xmlReader = XMLReaderFactory.createXMLReader();
                }
                xmlReader.setErrorHandler(this);
                // not explicitly set
                for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) {
                    try {
                        xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // not explicitly set
                for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) {
                    try {
                        xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(valueParser);
                internalSAXParser.parse(uncloseable);
                result = valueParser.getValue();
                try {
                    if (this.handler != null) {
                        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);
                    }
                }
                // result;
                return result;
            } catch (SAXException e) {
                caughtException = e;
            }
            // Reset the buffered input stream and try again looking for tuple
            // results
            buff.reset();
        }
        if (attemptParseTuple) {
            try {
                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                xmlReader.setErrorHandler(this);
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(new SPARQLResultsSAXParser(this.valueFactory, this.handler));
                internalSAXParser.parse(uncloseable);
                // we had success, so remove the exception that we were tracking
                // from
                // the boolean failure
                caughtException = null;
            } catch (SAXException e) {
                caughtException = e;
            }
        }
        if (caughtException != null) {
            Exception wrappedExc = caughtException.getException();
            if (wrappedExc == null) {
                throw new QueryResultParseException(caughtException);
            } else if (wrappedExc instanceof QueryResultParseException) {
                throw (QueryResultParseException) wrappedExc;
            } else if (wrappedExc instanceof QueryResultHandlerException) {
                throw (QueryResultHandlerException) wrappedExc;
            } else {
                throw new QueryResultParseException(wrappedExc);
            }
        }
    } finally {
        // Explicitly call the delegator to the close method to actually close it
        uncloseable.doClose();
    }
    return result;
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) IOException(java.io.IOException) QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) BufferedInputStream(java.io.BufferedInputStream) UncloseableInputStream(org.eclipse.rdf4j.common.io.UncloseableInputStream) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) XMLReader(org.xml.sax.XMLReader)

Example 3 with QueryResultParseException

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

Example 4 with QueryResultParseException

use of org.eclipse.rdf4j.query.resultio.QueryResultParseException in project rdf4j by eclipse.

the class SPARQLXMLParserCustomTest method testEntityExpansionNoSecureProcessing.

/**
 * Test with Secure processing setting off.
 * <p>
 * IMPORTANT: Only turn this on to verify it is still working, as there is
 * no way to safely perform this test.
 * <p>
 * WARNING: This test will cause an OutOfMemoryException when it eventually
 * fails, as it will eventually fail.
 *
 * @throws Exception
 */
@Ignore
@Test(timeout = 10000)
public void testEntityExpansionNoSecureProcessing() throws Exception {
    QueryResultCollector handler = new QueryResultCollector();
    ParseErrorCollector errorCollector = new ParseErrorCollector();
    QueryResultParser aParser = QueryResultIO.createTupleParser(TupleQueryResultFormat.SPARQL).setQueryResultHandler(handler).set(XMLParserSettings.SECURE_PROCESSING, false).setParseErrorListener(errorCollector);
    try {
        // IMPORTANT: This will not use the entity limit
        aParser.parseQueryResult(this.getClass().getResourceAsStream("/sparqlxml/bad-entity-expansion-limit.srx"));
        fail("Parser did not throw an exception");
    } catch (QueryResultParseException e) {
    // assertTrue(e.getMessage().contains(
    // "The parser has encountered more than \"64,000\" entity
    // expansions in this document; this is the limit imposed by the"));
    }
    assertEquals(0, errorCollector.getWarnings().size());
    assertEquals(0, errorCollector.getErrors().size());
    assertEquals(1, errorCollector.getFatalErrors().size());
}
Also used : QueryResultParser(org.eclipse.rdf4j.query.resultio.QueryResultParser) QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) ParseErrorCollector(org.eclipse.rdf4j.rio.helpers.ParseErrorCollector) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with QueryResultParseException

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

QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)12 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)5 QueryResultParser (org.eclipse.rdf4j.query.resultio.QueryResultParser)4 ParseErrorCollector (org.eclipse.rdf4j.rio.helpers.ParseErrorCollector)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)3 ArrayList (java.util.ArrayList)2 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 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HashSet (java.util.HashSet)1 List (java.util.List)1