You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by ca...@apache.org on 2012/01/30 17:14:01 UTC
svn commit: r1237753 - in /incubator/jena/Jena2/ARQ/trunk: ./
src-examples/arq/examples/ src/main/java/com/hp/hpl/jena/query/
src/main/java/com/hp/hpl/jena/sparql/engine/http/
Author: castagna
Date: Mon Jan 30 16:14:00 2012
New Revision: 1237753
URL: http://svn.apache.org/viewvc?rev=1237753&view=rev
Log:
JENA-195
HttpQuery and QueryEngineHTTP consistently throw a QueryExecException exception if a 'query' parameter is specified with SERVICE.
Added a couple of examples (i.e. ExampleDBPedia2|3) to show the two ways to specify a query parameter with SERVICE.
I hope everything is fine now. If not, let me know and I'll fix it directly in trunk.
Added:
incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java (with props)
incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java (with props)
Modified:
incubator/jena/Jena2/ARQ/trunk/ChangeLog.txt
incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/query/ARQ.java
incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java
Modified: incubator/jena/Jena2/ARQ/trunk/ChangeLog.txt
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/ChangeLog.txt?rev=1237753&r1=1237752&r2=1237753&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/ChangeLog.txt (original)
+++ incubator/jena/Jena2/ARQ/trunk/ChangeLog.txt Mon Jan 30 16:14:00 2012
@@ -5,6 +5,7 @@ ChangeLog for ARQ
+ SPARQL 1.1: Complete scope tracking of variables (JENA-142)
+ Faster writing of XML and JSON results formats (JENA-178)
++ Support for SERVICE with additional parameters (JENA-195)
==== ARQ 2.8.9 / 2.9.0
Added: incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java?rev=1237753&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java Mon Jan 30 16:14:00 2012
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package arq.examples;
+
+import com.hp.hpl.jena.query.Query ;
+import com.hp.hpl.jena.query.QueryExecution ;
+import com.hp.hpl.jena.query.QueryExecutionFactory ;
+import com.hp.hpl.jena.query.QueryFactory ;
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFormatter ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+
+public class ExampleDBpedia2
+{
+ static public void main(String... argv) {
+ String queryString =
+ "SELECT * WHERE { " +
+ " SERVICE <http://dbpedia-live.openlinksw.com/sparql?timeout=2000> { " +
+ " SELECT DISTINCT ?company where {?company a <http://dbpedia.org/ontology/Company>} LIMIT 20" +
+ " }" +
+ "}" ;
+ Query query = QueryFactory.create(queryString) ;
+ QueryExecution qexec = QueryExecutionFactory.create(query, ModelFactory.createDefaultModel()) ;
+ try {
+ ResultSet rs = qexec.execSelect() ;
+ ResultSetFormatter.out(System.out, rs, query) ;
+ } finally {
+ qexec.close() ;
+ }
+ }
+
+}
Propchange: incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia2.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java?rev=1237753&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java Mon Jan 30 16:14:00 2012
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package arq.examples;
+
+import java.util.ArrayList ;
+import java.util.HashMap ;
+import java.util.List ;
+import java.util.Map ;
+
+import com.hp.hpl.jena.query.ARQ ;
+import com.hp.hpl.jena.query.Query ;
+import com.hp.hpl.jena.query.QueryExecution ;
+import com.hp.hpl.jena.query.QueryExecutionFactory ;
+import com.hp.hpl.jena.query.QueryFactory ;
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFormatter ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+
+public class ExampleDBpedia3
+{
+ static public void main(String... argv) {
+ String serviceURI = "http://dbpedia-live.openlinksw.com/sparql" ;
+ String queryString =
+ "SELECT * WHERE { " +
+ " SERVICE <" + serviceURI + "> { " +
+ " SELECT DISTINCT ?company where {?company a <http://dbpedia.org/ontology/Company>} LIMIT 20" +
+ " }" +
+ "}" ;
+
+ Query query = QueryFactory.create(queryString) ;
+ QueryExecution qexec = QueryExecutionFactory.create(query, ModelFactory.createDefaultModel()) ;
+
+ Map<String, Map<String,List<String>>> serviceParams = new HashMap<String, Map<String,List<String>>>() ;
+ Map<String,List<String>> params = new HashMap<String,List<String>>() ;
+ List<String> values = new ArrayList<String>() ;
+ values.add("2000") ;
+ params.put("timeout", values) ;
+ serviceParams.put(serviceURI, params) ;
+ qexec.getContext().set(ARQ.serviceParams, serviceParams) ;
+
+ try {
+ ResultSet rs = qexec.execSelect() ;
+ ResultSetFormatter.out(System.out, rs, query) ;
+ } finally {
+ qexec.close() ;
+ }
+ }
+
+}
Propchange: incubator/jena/Jena2/ARQ/trunk/src-examples/arq/examples/ExampleDBpedia3.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/query/ARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/query/ARQ.java?rev=1237753&r1=1237752&r2=1237753&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/query/ARQ.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/query/ARQ.java Mon Jan 30 16:14:00 2012
@@ -211,6 +211,17 @@ public class ARQ
/** Symbol to name the Xerces-J regular expression engine */
public static final Symbol xercesRegex = ARQConstants.allocSymbol("xercesRegex") ;
+ /**
+ * Use this Symbol to allow passing additional query parameters to a
+ * SERVICE <IRI> call.
+ * Parameters need to be grouped by SERVICE <IRI>,
+ * a Map<String, Map<String,List<String>>> is assumed.
+ * The key of the first map is the SERVICE IRI, the value is a Map
+ * which maps the name of a query string parameters to its values.
+ *
+ * @see Service
+ */
+ public static final Symbol serviceParams = ARQConstants.allocSymbol("serviceParams") ;
/**
* A Long value that specifies the number of bindings (or triples for CONSTRUCT queries) to be stored in memory by sort
Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java?rev=1237753&r1=1237752&r2=1237753&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java Mon Jan 30 16:14:00 2012
@@ -29,12 +29,14 @@ import java.net.URL ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Map ;
+import java.util.regex.Pattern ;
import org.apache.commons.codec.binary.Base64 ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.hp.hpl.jena.query.ARQ ;
+import com.hp.hpl.jena.query.QueryExecException ;
import com.hp.hpl.jena.shared.JenaException ;
import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
import com.hp.hpl.jena.sparql.util.Convert ;
@@ -68,6 +70,8 @@ public class HttpQuery extends Params
String responseMessage = null ;
boolean forcePOST = false ;
String queryString = null ;
+ boolean serviceParams = false ;
+ private final Pattern queryParamPattern = Pattern.compile(".+[&|\\?]query=.*") ;
//static final String ENC_UTF8 = "UTF-8" ;
@@ -97,8 +101,11 @@ public class HttpQuery extends Params
log.trace("URL: "+serviceURL) ;
if ( serviceURL.indexOf('?') >= 0 )
- throw new QueryExceptionHTTP(-1, "URL already has a query string ("+serviceURL+")") ;
-
+ serviceParams = true ;
+
+ if ( queryParamPattern.matcher(serviceURL).matches() )
+ throw new QueryExecException("SERVICE URL overrides the 'query' SPARQL protocol parameter") ;
+
this.serviceURL = serviceURL ;
}
@@ -178,7 +185,7 @@ public class HttpQuery extends Params
if ( count() == 0 )
target = new URL(serviceURL) ;
else
- target = new URL(serviceURL+"?"+qs) ;
+ target = new URL(serviceURL+(serviceParams ? "&" : "?")+qs) ;
}
catch (MalformedURLException malEx)
{ throw new QueryExceptionHTTP(0, "Malformed URL: "+malEx) ; }
Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java?rev=1237753&r1=1237752&r2=1237753&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java Mon Jan 30 16:14:00 2012
@@ -23,6 +23,7 @@ import java.io.InputStream ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.List ;
+import java.util.Map ;
import java.util.concurrent.TimeUnit ;
import org.openjena.atlas.io.IO ;
@@ -34,6 +35,7 @@ import org.slf4j.LoggerFactory ;
import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.query.Query ;
+import com.hp.hpl.jena.query.QueryExecException ;
import com.hp.hpl.jena.query.QueryExecution ;
import com.hp.hpl.jena.query.QuerySolution ;
import com.hp.hpl.jena.query.ResultSet ;
@@ -251,6 +253,7 @@ public class QueryEngineHTTP implements
throw new ARQException("HTTP execution already closed") ;
HttpQuery httpQuery = new HttpQuery(service) ;
+ httpQuery.merge(getServiceParams(service, context)) ;
httpQuery.addParam(HttpParams.pQuery, queryString );
for ( Iterator<String> iter = defaultGraphURIs.iterator() ; iter.hasNext() ; )
@@ -271,6 +274,31 @@ public class QueryEngineHTTP implements
return httpQuery ;
}
+ // This is to allow setting additional/optional query parameters on a per SERVICE level, see: JENA-195
+ protected static Params getServiceParams(String serviceURI, Context context) throws QueryExecException
+ {
+ Params params = new Params();
+ @SuppressWarnings("unchecked")
+ Map<String, Map<String,List<String>>> serviceParams = (Map<String, Map<String,List<String>>>)context.get(ARQ.serviceParams) ;
+ if ( serviceParams != null )
+ {
+ Map<String,List<String>> paramsMap = serviceParams.get(serviceURI) ;
+ if ( paramsMap != null )
+ {
+ for (String param : paramsMap.keySet())
+ {
+ if ( HttpParams.pQuery.equals(param) )
+ throw new QueryExecException("ARQ serviceParams overrides the 'query' SPARQL protocol parameter") ;
+
+ List<String> values = paramsMap.get(param) ;
+ for (String value : values)
+ params.addParam(param, value) ;
+ }
+ }
+ }
+ return params;
+ }
+
public void cancel() { finished = true ; }
@Override
Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java?rev=1237753&r1=1237752&r2=1237753&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java Mon Jan 30 16:14:00 2012
@@ -64,9 +64,11 @@ public class Service
// else
query = OpAsQuery.asQuery(opRemote) ;
- Explain.explain("HTTP", query, context) ;
- HttpQuery httpQuery = new HttpQuery(op.getService().getURI()) ;
- httpQuery.addParam(HttpParams.pQuery, query.toString() );
+ Explain.explain("HTTP", query, context) ;
+ String uri = op.getService().getURI() ;
+ HttpQuery httpQuery = new HttpQuery(uri) ;
+ httpQuery.merge( QueryEngineHTTP.getServiceParams(uri, context) ) ;
+ httpQuery.addParam(HttpParams.pQuery, query.toString() ) ;
httpQuery.setAccept(HttpParams.contentTypeResultsXML) ;
InputStream in = httpQuery.exec() ;
ResultSet rs = ResultSetFactory.fromXML(in) ;
Re: svn commit: r1237753 - in /incubator/jena/Jena2/ARQ/trunk: ./
src-examples/arq/examples/ src/main/java/com/hp/hpl/jena/query/ src/main/java/com/hp/hpl/jena/sparql/engine/http/
Posted by Paolo Castagna <ca...@googlemail.com>.
castagna@apache.org wrote:
> HttpQuery and QueryEngineHTTP consistently throw a QueryExecException exception if a 'query' parameter is specified with SERVICE.
All query parameters are ok, but a parameter named 'query'.
Paolo