4242import org .eclipse .rdf4j .model .impl .DynamicModelFactory ;
4343import org .eclipse .rdf4j .model .impl .SimpleValueFactory ;
4444import org .eclipse .rdf4j .model .util .Literals ;
45+ import org .eclipse .rdf4j .model .vocabulary .RDF4J ;
46+ import org .eclipse .rdf4j .model .vocabulary .SESAME ;
4547import org .eclipse .rdf4j .query .BindingSet ;
4648import org .eclipse .rdf4j .query .BooleanQuery ;
4749import org .eclipse .rdf4j .query .GraphQuery ;
@@ -313,12 +315,13 @@ public long size(Resource... contexts) throws RepositoryException {
313315 String sizeAsTupleQuery (Resource ... contexts ) {
314316 String query = COUNT_EVERYTHING ;
315317 if (contexts != null && isQuadMode ()) {
316- if (contexts .length == 1 && contexts [0 ].isIRI ()) {
317- query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + ((IRI ) contexts [0 ]).stringValue ()
318+ if (contexts .length == 1 && isExposableGraphIri (contexts [0 ])) { // in case the context is null we want the
319+ // default graph.
320+ query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + contexts [0 ].stringValue ()
318321 + "> { ?s ?p ?o}}" ;
319322 } else if (contexts .length > 0 ) {
320323 String graphs = Arrays .stream (contexts )
321- .filter (Resource :: isIRI )
324+ .filter (SPARQLConnection :: isExposableGraphIri )
322325 .map (Resource ::stringValue )
323326 .map (s -> "FROM <" + s + ">" )
324327 .collect (Collectors .joining (" " ));
@@ -328,6 +331,17 @@ String sizeAsTupleQuery(Resource... contexts) {
328331 return query ;
329332 }
330333
334+ /**
335+ * For the sparql protocol a context must be an IRI However we can't send out the RDF4j intenral default graph IRIs
336+ *
337+ * @param resource to test if it can be the IRI for a named graph
338+ * @return true if it the input can be a foreign named graph.
339+ */
340+ private static boolean isExposableGraphIri (Resource resource ) {
341+ // We use the instanceof test to avoid any issue with a null pointer.
342+ return resource instanceof IRI && RDF4J .NIL != resource && SESAME .NIL != resource ;
343+ }
344+
331345 @ Override
332346 public RepositoryResult <Statement > getStatements (Resource subj , IRI pred , Value obj , boolean includeInferred ,
333347 Resource ... contexts ) throws RepositoryException {
0 commit comments