Search in sources :

Example 1 with QueryResultCollector

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

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONTupleTest method testNonStandardDistinctOrdered.

@Test
public void testNonStandardDistinctOrdered() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/non-standard-distinct-ordered.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 1 variable
    assertEquals(1, handler.getBindingNames().size());
    // first must be called "Concept", etc.,
    assertEquals("Concept", handler.getBindingNames().get(0));
    // -1 results
    assertEquals(100, handler.getBindingSets().size());
}
Also used : SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) InputStream(java.io.InputStream) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Example 3 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONTupleTest method testBindings2.

@Test
public void testBindings2() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings2.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 7 variables
    assertEquals(7, handler.getBindingNames().size());
    // first must be called "x", etc.,
    assertEquals("x", handler.getBindingNames().get(0));
    assertEquals("hpage", handler.getBindingNames().get(1));
    assertEquals("name", handler.getBindingNames().get(2));
    assertEquals("mbox", handler.getBindingNames().get(3));
    assertEquals("age", handler.getBindingNames().get(4));
    assertEquals("blurb", handler.getBindingNames().get(5));
    assertEquals("friend", handler.getBindingNames().get(6));
    // 2 results
    assertEquals(2, handler.getBindingSets().size());
    // Results are ordered, so first should be alice
    assertEquals("http://work.example.org/alice/", handler.getBindingSets().get(0).getValue("hpage").stringValue());
    for (BindingSet b : handler.getBindingSets()) {
        assertNotNull(b.getValue("x"));
        assertNotNull(b.getValue("hpage"));
        assertNotNull(b.getValue("name"));
        assertNotNull(b.getValue("mbox"));
        assertNotNull(b.getValue("friend"));
        assertTrue(b.getValue("x") instanceof BNode);
        assertTrue(b.getValue("hpage") instanceof IRI);
        assertTrue(b.getValue("name") instanceof Literal);
        assertTrue(b.getValue("friend") instanceof BNode);
        BNode value = (BNode) b.getValue("x");
        if (value.getID().equals("r1")) {
            assertNotNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof Literal);
            assertTrue(b.getValue("blurb") instanceof Literal);
            assertEquals("http://work.example.org/alice/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Alice", name.stringValue());
            assertFalse(name.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, name.getDatatype());
            Literal mbox = (Literal) b.getValue("mbox");
            assertEquals("", mbox.stringValue());
            assertFalse(mbox.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, mbox.getDatatype());
            Literal blurb = (Literal) b.getValue("blurb");
            assertEquals("<p xmlns=\"http://www.w3.org/1999/xhtml\">My name is <b>alice</b></p>", blurb.stringValue());
            assertFalse(blurb.getLanguage().isPresent());
            assertEquals(RDF.XMLLITERAL, blurb.getDatatype());
        } else if (value.getID().equals("r2")) {
            assertNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof IRI);
            assertEquals("http://work.example.org/bob/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Bob", name.stringValue());
            assertEquals("en", name.getLanguage().orElse(null));
            assertEquals(RDF.LANGSTRING, name.getDatatype());
            assertEquals("mailto:bob@work.example.org", b.getValue("mbox").stringValue());
        } else {
            fail("Found unexpected binding set in result: " + b.toString());
        }
    }
    assertEquals(1, handler.getLinks().size());
    assertEquals("http://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/#example", handler.getLinks().get(0));
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) BNode(org.eclipse.rdf4j.model.BNode) InputStream(java.io.InputStream) Literal(org.eclipse.rdf4j.model.Literal) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Example 4 with QueryResultCollector

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

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONBooleanTest method testBoolean2.

@Test
public void testBoolean2() throws Exception {
    SPARQLBooleanJSONParser parser = new SPARQLBooleanJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    parser.parseQueryResult(this.getClass().getResourceAsStream("/sparqljson/boolean2.srj"));
    assertTrue(handler.getBoolean());
}
Also used : SPARQLBooleanJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLBooleanJSONParser) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOBooleanTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOBooleanTest) Test(org.junit.Test)

Aggregations

QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)20 Test (org.junit.Test)18 InputStream (java.io.InputStream)10 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)10 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)10 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)5 IRI (org.eclipse.rdf4j.model.IRI)4 Literal (org.eclipse.rdf4j.model.Literal)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 AbstractQueryResultIOBooleanTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOBooleanTest)4 QueryResultParser (org.eclipse.rdf4j.query.resultio.QueryResultParser)4 SPARQLBooleanJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLBooleanJSONParser)4 ParseErrorCollector (org.eclipse.rdf4j.rio.helpers.ParseErrorCollector)4 BNode (org.eclipse.rdf4j.model.BNode)2 HttpResponse (org.apache.http.HttpResponse)1 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1 BooleanQueryResultParser (org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser)1 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)1