0

I tested that query:

SELECT ?comment WHERE {<http://pt.dbpedia.org/resource/Portugal> dcterms:subject ?comment}

at http://pt.dbpedia.org/sparql and I get the correct result:

http://pt.dbpedia.org/resource/Categoria:Portugal

But I'm using Jena and when I try to do that query with Jena I get no results. That's the way I'm doing the query with Jena:

private String getComment(String uri) {
          RDFNode node;
          String comment = "";

            final String QUERY = 
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                    "PREFIX dcterms: <http://purl.org/dc/terms/subject>\n" +
                    "SELECT ?comment WHERE {" +
                    "<" + uri + "> dcterms:subject ?comment." +
                    "}";

      final String ENDPOINT = "http://pt.dbpedia.org/sparql";
      final ResultSet rs = QueryExecutionFactory.sparqlService( ENDPOINT, QUERY ).execSelect();

                while( rs.hasNext() ) {
                       QuerySolution querySolution = rs.next();
                       node = querySolution.get("comment");
                       comment = node.toString();
                    }

                return comment;
      }

Is there anything wrong? Thank you!

Luciane
  • 259
  • 4
  • 23

1 Answers1

2

The dcterms: prefix has a typo

The dcterms: prefix is incorrect (it has subject at the end). It should be

http://purl.org/dc/terms/

Use ParameterizedSparqlStrings to avoid injection problems

Also, the way that you're splicing the uri parameter into the query is a bit brittle, and it's subject to injection attacks. E.g., what would happen if uri were the following string?

> <>* <> . <http://example.org/secretData> ?anyProperty ?comment . #

You'd leak information about http://example.org/secretData, since <> <>* <> will always match, and then you'd bind ?comment to all the values of any property of http://example.org/secretData. There's an example of how to do this in this answer to get latitude and longitude of a place dbpedia.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thank you so much! It's such a stupid thing, but you helped me a lot! – Luciane Apr 28 '14 at 18:51
  • Glad to help. You should consider using a [ParameterizedSparqlString](http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/ParameterizedSparqlString.html) instead of string concatenation though, for reasons I'm describing in the answer. – Joshua Taylor Apr 28 '14 at 18:52
  • Thank you! Very helpful example about ParameterizedSparqlStrings !! – Luciane Apr 28 '14 at 18:59