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 2013/03/29 20:46:57 UTC

svn commit: r1462623 - in /jena/trunk/jena-arq/src: main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java

Author: rvesse
Date: Fri Mar 29 19:46:56 2013
New Revision: 1462623

URL: http://svn.apache.org/r1462623
Log:
Incorporate a modified form of Martynas Jusevicius's patch for JENA-405

QueryEngineHTTP now respects service context if one has been configured for the specified remote service.  Adds additional tests to TestService to check that configuration is respected

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java

Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java?rev=1462623&r1=1462622&r2=1462623&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java Fri Mar 29 19:46:56 2013
@@ -43,7 +43,10 @@ import com.hp.hpl.jena.sparql.resultset.
 import com.hp.hpl.jena.sparql.util.Context ;
 import com.hp.hpl.jena.util.FileManager ;
 
-
+/**
+ * A query execution implementation where queries are executed against a remote service
+ *
+ */
 public class QueryEngineHTTP implements QueryExecution
 {
     private static Logger log = LoggerFactory.getLogger(QueryEngineHTTP.class) ;
@@ -115,6 +118,92 @@ public class QueryEngineHTTP implements 
         this.service = serviceURI ;
         // Copy the global context to freeze it.
         this.context = new Context(ARQ.getContext()) ;
+        
+        // Apply service configuration if relevant
+        QueryEngineHTTP.applyServiceConfig(serviceURI, this);
+    }
+    
+    /**
+     * <p>
+     * Helper method which applies configuration from the Context to the query engine
+     * if a service context exists for the given URI
+     * </p>
+     * <p>
+     * Based off proposed patch for JENA-405 but modified to apply all relevant configuration, this is in part also
+     * based off of the private {@code configureQuery()} method of the {@link Service} class though it omits
+     * parameter merging since that will be done automatically whenever the {@link QueryEngineHTTP} instance
+     * makes a query for remote submission.
+     * </p>
+     * @param serviceURI Service URI
+     */
+    private static void applyServiceConfig(String serviceURI, QueryEngineHTTP engine) {
+        @SuppressWarnings("unchecked")
+        Map<String, Context> serviceContextMap = (Map<String, Context>) engine.context.get(Service.serviceContext);
+        if (serviceContextMap != null && serviceContextMap.containsKey(serviceURI)) {
+            Context serviceContext = serviceContextMap.get(serviceURI);
+            if (log.isDebugEnabled())
+                log.debug("Endpoint URI {} has SERVICE Context: {} ", serviceURI, serviceContext);
+            
+            // Apply behavioral options
+            engine.setAllowGZip(serviceContext.isTrueOrUndef(Service.queryGzip));
+            engine.setAllowDeflate(serviceContext.isTrueOrUndef(Service.queryDeflate));
+            applyServiceTimeouts(engine, serviceContext);
+
+            // Apply authentication settings
+            String user = serviceContext.getAsString(Service.queryAuthUser);
+            String pwd = serviceContext.getAsString(Service.queryAuthPwd);
+
+            if (user != null || pwd != null) {
+                user = user == null ? "" : user;
+                pwd = pwd == null ? "" : pwd;
+                if (log.isDebugEnabled())
+                    log.debug("Setting basic HTTP authentication for endpoint URI {} with username: {} ", serviceURI, user);
+                engine.setBasicAuthentication(user, pwd.toCharArray());
+            }
+        }
+    }
+    
+    /**
+     * Applies context provided timeouts to the given engine
+     * @param engine Engine
+     * @param context Context
+     */
+    private static void applyServiceTimeouts(QueryEngineHTTP engine, Context context) {
+        if (context.isDefined(Service.queryTimeout)) 
+        {
+            Object obj = context.get(Service.queryTimeout);
+            if (obj instanceof Number) 
+            {
+                int x = ((Number) obj).intValue();
+                engine.setTimeout(-1, x);
+            } 
+            else if (obj instanceof String)
+            {
+                try {
+                    String str = obj.toString();
+                    if (str.contains(",")) {
+                        
+                        String[] a = str.split(",");
+                        int connect = Integer.parseInt(a[0]);
+                        int read = Integer.parseInt(a[1]);
+                        engine.setTimeout(read, connect);
+                    } 
+                    else 
+                    {
+                        int x = Integer.parseInt(str);
+                        engine.setTimeout(-1, x);
+                    }
+                } 
+                catch (NumberFormatException ex) 
+                {
+                    throw new QueryExecException("Can't interpret string for timeout: " + obj);
+                }
+            } 
+            else
+            {
+                throw new QueryExecException("Can't interpret timeout: " + obj);
+            }
+        }
     }
     
 //    public void setParams(Params params)
@@ -183,6 +272,14 @@ public class QueryEngineHTTP implements 
         namedGraphURIs.add(name) ;
     }
     
+    /**
+     * Gets whether basic authentication credentials have been provided
+     * @return True if basic authentication credentials have been provided
+     */
+    public boolean isUsingBasicAuthentication() {
+        return this.user != null || this.password != null;
+    }
+    
     /** Set user and password for basic authentication.
      *  After the request is made (one of the exec calls), the application
      *  can overwrite the password array to remove details of the secret.
@@ -386,7 +483,19 @@ public class QueryEngineHTTP implements 
     public long getTimeout1() { return asMillis(readTimeout, readTimeoutUnit) ; } 
     
     @Override
-    public long getTimeout2() { return asMillis(connectTimeout, connectTimeoutUnit) ; } 
+    public long getTimeout2() { return asMillis(connectTimeout, connectTimeoutUnit) ; }
+    
+    /**
+     * Gets whether HTTP requests will indicate to the remote server that GZip encoding of responses is accepted
+     * @return True if GZip encoding will be accepted
+     */
+    public boolean getAllowGZip() { return allowGZip; }
+    
+    /**
+     * Gets whether HTTP requests will indicate to the remote server that Deflate encoding of responses is accepted
+     * @return True if Deflate encoding will be accepted
+     */
+    public boolean getAllowDeflate() { return allowDeflate; }
 
     private static long asMillis(long duration, TimeUnit timeUnit)
     {

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java?rev=1462623&r1=1462622&r2=1462623&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/engine/http/TestService.java Fri Mar 29 19:46:56 2013
@@ -18,111 +18,251 @@
 
 package com.hp.hpl.jena.sparql.engine.http;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 import com.hp.hpl.jena.graph.Node;
-import com.hp.hpl.jena.graph.NodeFactory ;
+import com.hp.hpl.jena.graph.NodeFactory;
 import com.hp.hpl.jena.graph.Triple;
 import com.hp.hpl.jena.query.ARQ;
+import com.hp.hpl.jena.query.Query;
+import com.hp.hpl.jena.query.QueryExecutionFactory;
+import com.hp.hpl.jena.query.QueryFactory;
 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 {
-    
+    private static final String SERVICE = "http://example.com:40000";
 
-	@Test
-	public void testNumericTimeout()
-	{
-		BasicPattern basicPattern = new BasicPattern();
-		basicPattern.add( Triple.ANY );
-		Node serviceNode = NodeFactory.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( String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass().getName(), expected.getCause().getMessage()) );
-			}
-		}
-
-	}
-
-	@Test
-	public void testStringTimeout()
-	{
-		BasicPattern basicPattern = new BasicPattern();
-		basicPattern.add( Triple.ANY );
-		Node serviceNode = NodeFactory.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( String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass().getName(), expected.getCause().getMessage()) );
-			}
-		}	
-	}
-
-	@Test
-	public void testStringTimeout2()
-	{
-		BasicPattern basicPattern = new BasicPattern();
-		basicPattern.add( Triple.ANY );
-		Node serviceNode = NodeFactory.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( String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass().getName(), expected.getCause().getMessage()) );
-			}
-		}	
-	}
+    @Test
+    public void testNumericTimeout() {
+        BasicPattern basicPattern = new BasicPattern();
+        basicPattern.add(Triple.ANY);
+        Node serviceNode = NodeFactory.createURI(SERVICE);
+        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(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass()
+                        .getName(), expected.getCause().getMessage()));
+            }
+        }
+
+    }
+
+    @Test
+    public void testStringTimeout() {
+        BasicPattern basicPattern = new BasicPattern();
+        basicPattern.add(Triple.ANY);
+        Node serviceNode = NodeFactory.createURI(SERVICE);
+        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(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass()
+                        .getName(), expected.getCause().getMessage()));
+            }
+        }
+    }
+
+    @Test
+    public void testStringTimeout2() {
+        BasicPattern basicPattern = new BasicPattern();
+        basicPattern.add(Triple.ANY);
+        Node serviceNode = NodeFactory.createURI(SERVICE);
+        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(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass()
+                        .getName(), expected.getCause().getMessage()));
+            }
+        }
+    }
+
+    @Test
+    public void service_context_application_01() {
+        // This test requires no service context to be set
+        @SuppressWarnings("unchecked")
+        Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        if (serviceContextMap != null) {
+            serviceContextMap.remove(SERVICE);
+        }
+
+        Query q = QueryFactory.create("ASK { }");
+        QueryEngineHTTP engine = (QueryEngineHTTP) QueryExecutionFactory.createServiceRequest(SERVICE, q);
+        Assert.assertNotNull(engine);
+
+        // Check that no settings were changed
+        Assert.assertEquals(-1, engine.getTimeout1());
+        Assert.assertEquals(-1, engine.getTimeout2());
+        Assert.assertTrue(engine.getAllowGZip());
+        Assert.assertTrue(engine.getAllowDeflate());
+        Assert.assertFalse(engine.isUsingBasicAuthentication());
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void service_context_application_02() {
+        // This test requires us to set some authentication credentials for the
+        // service
+        Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        if (serviceContextMap == null) {
+            ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
+            serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        }
+        if (serviceContextMap.get(SERVICE) == null) {
+            serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
+        }
+        Context serviceContext = (Context) serviceContextMap.get(SERVICE);
+        try {
+            serviceContext.put(Service.queryAuthUser, "user");
+            serviceContext.put(Service.queryAuthPwd, "password");
+
+            Query q = QueryFactory.create("ASK { }");
+            QueryEngineHTTP engine = (QueryEngineHTTP) QueryExecutionFactory.createServiceRequest(SERVICE, q);
+            Assert.assertNotNull(engine);
+
+            // Check that no settings were changed
+            Assert.assertEquals(-1, engine.getTimeout1());
+            Assert.assertEquals(-1, engine.getTimeout2());
+            Assert.assertTrue(engine.getAllowGZip());
+            Assert.assertTrue(engine.getAllowDeflate());
+            Assert.assertTrue(engine.isUsingBasicAuthentication());
+
+        } finally {
+            serviceContext.remove(Service.queryAuthUser);
+            serviceContext.remove(Service.queryAuthPwd);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void service_context_application_03() {
+        // This test requires us to set some timeouts for the service
+        Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        if (serviceContextMap == null) {
+            ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
+            serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        }
+        if (serviceContextMap.get(SERVICE) == null) {
+            serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
+        }
+        Context serviceContext = (Context) serviceContextMap.get(SERVICE);
+        try {
+            serviceContext.put(Service.queryTimeout, "10");
+
+            Query q = QueryFactory.create("ASK { }");
+            QueryEngineHTTP engine = (QueryEngineHTTP) QueryExecutionFactory.createServiceRequest(SERVICE, q);
+            Assert.assertNotNull(engine);
+
+            // Check that no settings were changed
+            Assert.assertEquals(-1, engine.getTimeout1());
+            Assert.assertEquals(10, engine.getTimeout2());
+            Assert.assertTrue(engine.getAllowGZip());
+            Assert.assertTrue(engine.getAllowDeflate());
+            Assert.assertFalse(engine.isUsingBasicAuthentication());
+        } finally {
+            serviceContext.remove(Service.queryTimeout);
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void service_context_application_04() {
+        // This test requires us to set some timeouts for the service
+        Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        if (serviceContextMap == null) {
+            ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
+            serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        }
+        if (serviceContextMap.get(SERVICE) == null) {
+            serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
+        }
+        Context serviceContext = (Context) serviceContextMap.get(SERVICE);
+        try {
+            serviceContext.put(Service.queryTimeout, "10,20");
+
+            Query q = QueryFactory.create("ASK { }");
+            QueryEngineHTTP engine = (QueryEngineHTTP) QueryExecutionFactory.createServiceRequest(SERVICE, q);
+            Assert.assertNotNull(engine);
+
+            // Check that no settings were changed
+            Assert.assertEquals(20, engine.getTimeout1());
+            Assert.assertEquals(10, engine.getTimeout2());
+            Assert.assertTrue(engine.getAllowGZip());
+            Assert.assertTrue(engine.getAllowDeflate());
+            Assert.assertFalse(engine.isUsingBasicAuthentication());
+        } finally {
+            serviceContext.remove(Service.queryTimeout);
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void service_context_application_05() {
+        // This test requires us to set that GZip and Deflate are permitted
+        Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        if (serviceContextMap == null) {
+            ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
+            serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
+        }
+        if (serviceContextMap.get(SERVICE) == null) {
+            serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
+        }
+        Context serviceContext = (Context) serviceContextMap.get(SERVICE);
+        try {
+            serviceContext.put(Service.queryGzip, false);
+            serviceContext.put(Service.queryDeflate, false);
+
+            Query q = QueryFactory.create("ASK { }");
+            QueryEngineHTTP engine = (QueryEngineHTTP) QueryExecutionFactory.createServiceRequest(SERVICE, q);
+            Assert.assertNotNull(engine);
+
+            // Check that no settings were changed
+            Assert.assertEquals(-1, engine.getTimeout1());
+            Assert.assertEquals(-1, engine.getTimeout2());
+            Assert.assertFalse(engine.getAllowGZip());
+            Assert.assertFalse(engine.getAllowDeflate());
+            Assert.assertFalse(engine.isUsingBasicAuthentication());
+        } finally {
+            serviceContext.remove(Service.queryGzip);
+            serviceContext.remove(Service.queryDeflate);
+        }
+    }
 }
-