You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2012/05/03 18:24:34 UTC

svn commit: r1333529 - in /incubator/jena/Jena2/ARQ/trunk/src: main/java/com/hp/hpl/jena/sparql/engine/http/ test/java/com/hp/hpl/jena/sparql/engine/ test/java/com/hp/hpl/jena/sparql/engine/http/

Author: rvesse
Date: Thu May  3 16:24:33 2012
New Revision: 1333529

URL: http://svn.apache.org/viewvc?rev=1333529&view=rev
Log:
Applied Claude Warren's patch for JENA-240 with some minor formatting tidy up, new tests pass as expected

Added:
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/   (with props)
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java
Modified:
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/Service.java
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/TS_Engine.java

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=1333529&r1=1333528&r2=1333529&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 Thu May  3 16:24:33 2012
@@ -19,6 +19,7 @@
 package com.hp.hpl.jena.sparql.engine.http;
 
 import java.io.InputStream ;
+import java.util.Map;
 
 import com.hp.hpl.jena.query.Query ;
 import com.hp.hpl.jena.query.QueryExecException ;
@@ -32,11 +33,59 @@ import com.hp.hpl.jena.sparql.engine.Ren
 import com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorResultSet ;
 import com.hp.hpl.jena.sparql.mgt.Explain ;
 import com.hp.hpl.jena.sparql.util.Context ;
+import com.hp.hpl.jena.sparql.util.Symbol ;
 
 /** Execution of OpService */
 
 public class Service
 {
+    /* define the symbols that Service will use to set the HttpQuery parameters */
+    public static final String base = "http://jena.hpl.hp.com/Service#";
+
+    /**
+     * Use to set the HttpQuery.allowDeflate flag.
+     */
+    public static final Symbol queryDeflate = Symbol.create(base + "queryDeflate");
+
+    /**
+     * Use to set the HttpQuery.allowGZip flag.
+     */
+    public static final Symbol queryGzip = Symbol.create(base + "queryGzip");
+
+    /**
+     * Use to set the user id for basic auth. 
+     */
+    public static final Symbol queryAuthUser = Symbol.create(base + "queryAuthUser");
+
+    /**
+     * Use to set the user password for basic auth.
+     */
+    public static final Symbol queryAuthPwd = Symbol.create(base + "queryAuthPwd");
+
+    /** 
+     * Use this Symbol to allow passing additional service contest variables 
+     * SERVICE <IRI> call.
+     * Parameters need to be grouped by SERVICE <IRI>,  
+     * a Map<String, Context> is assumed.
+     * The key of the first map is the SERVICE IRI, the value is a Context 
+     * who's values will override any defaults in the original context.
+     * 
+     * @see com.hp.hpl.jena.sparql.engine.http.Service
+     */
+    public static final Symbol serviceContext = Symbol.create(base + "serviceContext");
+
+    /**
+     * Set timeout.  The value of this symbol gives the value of the timeout in milliseconds
+     * <ul>
+     * <li>A Number; the long value is used</li>
+     * <li>A string, e.g. "1000", parsed as a number</li>
+     * <li>A string, as two numbers separated by a comma, e.g. "500,10000" parsed as two numbers</li>
+     * </ul>
+     * The first value is passed to HttpQuery.setConnectTimeout() the second, if it exists, is passed
+     * to HttpQuery.setReadTimeout()
+     */
+    public static final Symbol queryTimeout = Symbol.create(base + "queryTimeout");
+
     public static QueryIterator exec(OpService op, Context context)
     {
         if ( ! op.getService().isURI() )
@@ -66,12 +115,97 @@ public class Service
             
         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) ;
+        HttpQuery httpQuery = configureQuery(uri, context, query);
         InputStream in = httpQuery.exec() ;
         ResultSet rs = ResultSetFactory.fromXML(in) ;
         return new QueryIteratorResultSet(rs) ; 
     }
+    
+    /**
+     * Create and configure the HttpQuery object.
+     * 
+     * The parentContext is not modified but is used to create a new context copy.
+     * 
+     * @param uri The uri of the endpoint
+     * @param parentContext The initial context.
+     * @param Query the Query to execute.
+     * @return An HttpQuery configured as per the context.
+     */
+    private static HttpQuery configureQuery(String uri, Context parentContext, Query query) {
+    	HttpQuery httpQuery = new HttpQuery(uri);
+    	Context context = new Context(parentContext);
+
+    	// add the context settings from the service context
+    	@SuppressWarnings("unchecked")
+    	Map<String, Context> serviceContextMap = (Map<String, Context>) context.get(serviceContext);
+    	if (serviceContextMap != null) 
+    	{
+    		Context serviceContext = serviceContextMap.get(uri);
+    		if (serviceContext != null) context.putAll(serviceContext);
+    	}
+
+    	// configure the query object.
+    	httpQuery.merge(QueryEngineHTTP.getServiceParams(uri, context));
+    	httpQuery.addParam(HttpParams.pQuery, query.toString());
+    	httpQuery.setAccept(HttpParams.contentTypeResultsXML);
+    	httpQuery.setAllowGZip(context.isTrue(queryGzip));
+    	httpQuery.setAllowDeflate(context.isTrue(queryDeflate));
+
+    	String user = context.getAsString(queryAuthUser);
+    	String pwd = context.getAsString(queryAuthPwd);
+
+    	if (user != null || pwd != null) {
+    		user = user==null?"":user;
+    		pwd = pwd==null?"":pwd;
+    		httpQuery.setBasicAuthentication(user, pwd.toCharArray());
+    	}
+
+    	setAnyTimeouts(httpQuery, context);
+
+    	return httpQuery;
+    }
+
+    /**
+     * Modified from QueryExecutionBase
+     * 
+     * @see com.hp.hpl.jena.sparql.engine.QueryExecutionBase
+     */
+    private static void setAnyTimeouts(HttpQuery query, Context context) {
+    	if (context.isDefined(queryTimeout)) 
+    	{
+    		Object obj = context.get(queryTimeout);
+    		if (obj instanceof Number) 
+    		{
+    			int x = ((Number) obj).intValue();
+    			query.setConnectTimeout(x);
+    		} 
+    		else if (obj instanceof String)
+    		{
+    			try {
+    				String str = obj.toString();
+    				if (str.contains(",")) {
+    					
+    					String[] a = str.split(",");
+    					int x1 = Integer.parseInt(a[0]);
+    					int x2 = Integer.parseInt(a[1]);
+    					query.setConnectTimeout(x1);
+    					query.setReadTimeout(x2);
+    				} 
+    				else 
+    				{
+    					int x = Integer.parseInt(str);
+    					query.setConnectTimeout(x);
+    				}
+    			} 
+    			catch (NumberFormatException ex) 
+    			{
+    				throw new QueryExecException("Can't interpret string for timeout: " + obj);
+    			}
+    		} 
+    		else
+    		{
+    			throw new QueryExecException("Can't interpret timeout: " + obj);
+    		}
+    	}
+    }
 }

Modified: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/TS_Engine.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/TS_Engine.java?rev=1333529&r1=1333528&r2=1333529&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/TS_Engine.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/TS_Engine.java Thu May  3 16:24:33 2012
@@ -22,12 +22,14 @@ import org.junit.runner.RunWith ;
 import org.junit.runners.Suite ;
 
 import com.hp.hpl.jena.sparql.engine.binding.TestBindingStreams ;
+import com.hp.hpl.jena.sparql.engine.http.TestService;
 import com.hp.hpl.jena.sparql.engine.iterator.TestQueryIterSort ;
 
 @RunWith(Suite.class)
 @Suite.SuiteClasses( {
         TestBindingStreams.class
       , TestQueryIterSort.class
+      , TestService.class
 })
 
 public class TS_Engine {}

Propchange: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/
------------------------------------------------------------------------------
    bugtraq:number = true

Added: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java?rev=1333529&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java Thu May  3 16:24:33 2012
@@ -0,0 +1,127 @@
+/**
+ * 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 com.hp.hpl.jena.sparql.engine.http;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.query.ARQ;
+import com.hp.hpl.jena.sparql.algebra.op.OpBGP;
+import com.hp.hpl.jena.sparql.algebra.op.OpService;
+import com.hp.hpl.jena.sparql.core.BasicPattern;
+import com.hp.hpl.jena.sparql.util.Context;
+
+
+public class TestService {
+    
+
+	@Test
+	public void testNumericTimeout()
+	{
+		BasicPattern basicPattern = new BasicPattern();
+		basicPattern.add( Triple.ANY );
+		Node serviceNode = Node.createURI("http://example.com:40000");
+		OpService opService = new OpService( serviceNode, new OpBGP( basicPattern ), false);
+
+		Context context = new Context();
+		ARQ.setNormalMode(context);
+
+		context.set(Service.queryTimeout, 10 );	
+
+		try {
+			Service.exec(opService, context);
+			Assert.fail( "Expected QueryExceptionHTTP");
+		}
+		catch (QueryExceptionHTTP expected)
+		{
+			if (expected.getCause() instanceof java.net.SocketTimeoutException)
+			{
+				// expected
+			}
+			else
+			{
+				Assert.fail( "Expected SocketTimeoutException" );
+			}
+		}
+
+	}
+
+	@Test
+	public void testStringTimeout()
+	{
+		BasicPattern basicPattern = new BasicPattern();
+		basicPattern.add( Triple.ANY );
+		Node serviceNode = Node.createURI("http://example.com:40000");
+		OpService opService = new OpService( serviceNode, new OpBGP( basicPattern ), false);
+
+		Context context = new Context();
+		ARQ.setNormalMode(context);
+
+		context.set(Service.queryTimeout, "10" );	
+
+		try {
+			Service.exec(opService, context);
+			Assert.fail( "Expected QueryExceptionHTTP");
+		}
+		catch (QueryExceptionHTTP expected)
+		{
+			if (expected.getCause() instanceof java.net.SocketTimeoutException)
+			{
+				// expected
+			}
+			else
+			{
+				Assert.fail( "Expected SocketTimeoutException" );
+			}
+		}	
+	}
+
+	@Test
+	public void testStringTimeout2()
+	{
+		BasicPattern basicPattern = new BasicPattern();
+		basicPattern.add( Triple.ANY );
+		Node serviceNode = Node.createURI("http://example.com:40000");
+		OpService opService = new OpService( serviceNode, new OpBGP( basicPattern ), false);
+
+		Context context = new Context();
+		ARQ.setNormalMode(context);
+
+		context.set(Service.queryTimeout, "10,10000" );	
+
+		try {
+			Service.exec(opService, context);
+			Assert.fail( "Expected QueryExceptionHTTP");
+		}
+		catch (QueryExceptionHTTP expected)
+		{
+			if (expected.getCause() instanceof java.net.SocketTimeoutException)
+			{
+				// expected
+			}
+			else
+			{
+				Assert.fail( "Expected SocketTimeoutException" );
+			}
+		}	
+	}
+}
+