Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/ServiceAuthenticator.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/ServiceAuthenticator.java?rev=1497583&r1=1497582&r2=1497583&view=diff ============================================================================== --- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/ServiceAuthenticator.java (original) +++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/ServiceAuthenticator.java Thu Jun 27 22:14:09 2013 @@ -93,7 +93,7 @@ public class ServiceAuthenticator extend @SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) this.context.get(Service.serviceContext); - if (serviceContextMap != null && serviceContextMap.containsKey(Service.serviceContext)) { + if (serviceContextMap != null && serviceContextMap.containsKey(target.toString())) { // Try to obtain Context for target URI Context serviceContext = serviceContextMap.get(target.toString()); if (serviceContext != null) { @@ -112,7 +112,7 @@ public class ServiceAuthenticator extend @SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) this.context.get(Service.serviceContext); - if (serviceContextMap != null && serviceContextMap.containsKey(Service.serviceContext)) { + if (serviceContextMap != null && serviceContextMap.containsKey(target.toString())) { // Try to obtain Context for target URI Context serviceContext = serviceContextMap.get(target.toString()); if (serviceContext != null) {
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java?rev=1497583&r1=1497582&r2=1497583&view=diff ============================================================================== --- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java (original) +++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java Thu Jun 27 22:14:09 2013 @@ -32,6 +32,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicLong; import org.apache.http.*; +import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -40,6 +41,7 @@ import org.apache.http.client.methods.Ht import org.apache.http.entity.EntityTemplate; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.SystemDefaultHttpClient; import org.apache.http.message.BasicNameValuePair; @@ -111,8 +113,6 @@ public class HttpOp { public static void setDefaultAuthenticator(HttpAuthenticator authenticator) { defaultAuthenticator = authenticator; } - - // TODO Currently most of the code wraps IO Exceptions as AtlasException, may be better to wrap as HTTP Exception /** * Executes a HTTP Get request handling the response with one of the given @@ -142,7 +142,7 @@ public class HttpOp { */ public static void execHttpGet(String url, String acceptHeader, Map<String, HttpResponseHandler> handlers, HttpContext httpContext) { - execHttpGet(url, acceptHeader, handlers, httpContext, defaultAuthenticator); + execHttpGet(url, acceptHeader, handlers, null, httpContext, defaultAuthenticator); } /** @@ -164,13 +164,15 @@ public class HttpOp { * Accept Header * @param handlers * Response Handlers + * @param httpClient + * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator */ public static void execHttpGet(String url, String acceptHeader, Map<String, HttpResponseHandler> handlers, - HttpContext httpContext, HttpAuthenticator authenticator) { + HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) { try { long id = counter.incrementAndGet(); String requestURI = determineRequestURI(url); @@ -184,16 +186,16 @@ public class HttpOp { httpget.addHeader(HttpNames.hAccept, acceptHeader); // Prepare and execute - DefaultHttpClient httpclient = new SystemDefaultHttpClient(); + httpClient = ensureClient(httpClient); httpContext = ensureContext(httpContext); - applyAuthentication(httpclient, url, httpContext, authenticator); - HttpResponse response = httpclient.execute(httpget, httpContext); + applyAuthentication(asAbstractClient(httpClient), url, httpContext, authenticator); + HttpResponse response = httpClient.execute(httpget, httpContext); // Handle response httpResponse(id, response, baseIRI, handlers); - httpclient.getConnectionManager().shutdown(); + httpClient.getConnectionManager().shutdown(); } catch (IOException ex) { - IO.exception(ex); + throw new HttpException(ex); } } @@ -212,7 +214,7 @@ public class HttpOp { * @return Typed Input Stream */ public static TypedInputStreamHttp execHttpGet(String url, String acceptHeader, HttpContext httpContext) { - return execHttpGet(url, acceptHeader, httpContext, defaultAuthenticator); + return execHttpGet(url, acceptHeader, null, httpContext, defaultAuthenticator); } /** @@ -229,14 +231,16 @@ public class HttpOp { * URL * @param acceptHeader * Accept Header + * @param httpClient + * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator * @return Typed Input Stream, null if the URL returns 404 */ - public static TypedInputStreamHttp execHttpGet(String url, String acceptHeader, HttpContext httpContext, - HttpAuthenticator authenticator) { + public static TypedInputStreamHttp execHttpGet(String url, String acceptHeader, HttpClient httpClient, + HttpContext httpContext, HttpAuthenticator authenticator) { try { long id = counter.incrementAndGet(); String requestURI = determineRequestURI(url); @@ -251,10 +255,10 @@ public class HttpOp { httpget.addHeader(HttpNames.hAccept, acceptHeader); // Prepare and execute - DefaultHttpClient httpclient = new SystemDefaultHttpClient(); + httpClient = ensureClient(httpClient); httpContext = ensureContext(httpContext); - applyAuthentication(httpclient, url, httpContext, authenticator); - HttpResponse response = httpclient.execute(httpget, httpContext); + applyAuthentication(asAbstractClient(httpClient), url, httpContext, authenticator); + HttpResponse response = httpClient.execute(httpget, httpContext); // Response StatusLine statusLine = response.getStatusLine(); @@ -280,10 +284,9 @@ public class HttpOp { if (log.isDebugEnabled()) log.debug(format("[%d] %d %s :: %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase(), mt)); - return new TypedInputStreamHttp(entity.getContent(), mt, httpclient.getConnectionManager()); + return new TypedInputStreamHttp(entity.getContent(), mt, httpClient.getConnectionManager()); } catch (IOException ex) { - IO.exception(ex); - return null; + throw new HttpException(ex); } } @@ -332,8 +335,7 @@ public class HttpOp { instream.close(); return string; } catch (IOException ex) { - IO.exception(ex); - return null; + throw new HttpException(ex); } } @@ -663,7 +665,7 @@ public class HttpOp { httpclient.getConnectionManager().shutdown(); } catch (IOException ex) { - IO.exception(ex); + throw new HttpException(ex); } finally { closeEntity(provider); } @@ -679,13 +681,14 @@ public class HttpOp { * URL * @param acceptHeader * Accept Header - * @param params Parameters to POST + * @param params + * Parameters to POST * @param httpContext * HTTP Context * @return Typed Input Stream */ public static TypedInputStreamHttp execHttpPostForm(String url, String acceptHeader, Params params, HttpContext httpContext) { - return execHttpPostForm(url, acceptHeader, params, httpContext, defaultAuthenticator); + return execHttpPostForm(url, acceptHeader, params, null, httpContext, defaultAuthenticator); } /** @@ -704,14 +707,16 @@ public class HttpOp { * Accept Header * @param params * Parameters to POST + * @param httpClient + * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator * @return Typed Input Stream, null if the URL returns 404 */ - public static TypedInputStreamHttp execHttpPostForm(String url, String acceptHeader, Params params, HttpContext httpContext, - HttpAuthenticator authenticator) { + public static TypedInputStreamHttp execHttpPostForm(String url, String acceptHeader, Params params, HttpClient httpClient, + HttpContext httpContext, HttpAuthenticator authenticator) { try { long id = counter.incrementAndGet(); String requestURI = determineRequestURI(url); @@ -727,10 +732,10 @@ public class HttpOp { httppost.setEntity(convertFormParams(params)); // Prepare and execute - DefaultHttpClient httpclient = new SystemDefaultHttpClient(); + httpClient = ensureClient(httpClient); httpContext = ensureContext(httpContext); - applyAuthentication(httpclient, url, httpContext, authenticator); - HttpResponse response = httpclient.execute(httppost, httpContext); + applyAuthentication(asAbstractClient(httpClient), url, httpContext, authenticator); + HttpResponse response = httpClient.execute(httppost, httpContext); // Response StatusLine statusLine = response.getStatusLine(); @@ -756,10 +761,9 @@ public class HttpOp { if (log.isDebugEnabled()) log.debug(format("[%d] %d %s :: %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase(), mt)); - return new TypedInputStreamHttp(entity.getContent(), mt, httpclient.getConnectionManager()); + return new TypedInputStreamHttp(entity.getContent(), mt, httpClient.getConnectionManager()); } catch (IOException ex) { - IO.exception(ex); - return null; + throw new HttpException(ex); } } @@ -845,7 +849,7 @@ public class HttpOp { httpResponse(id, response, baseIRI, handlers); httpclient.getConnectionManager().shutdown(); } catch (IOException ex) { - IO.exception(ex); + throw new HttpException(ex); } } @@ -985,8 +989,26 @@ public class HttpOp { httpResponse(id, response, baseIRI, null); httpclient.getConnectionManager().shutdown(); } catch (IOException ex) { - IO.exception(ex); + throw new HttpException(ex); + } + } + + /** + * Ensures that a HTTP Client is non-null + * + * @param client + * HTTP Client + * @return HTTP Client + */ + private static HttpClient ensureClient(HttpClient client) { + return client != null ? client : new SystemDefaultHttpClient(); + } + + private static AbstractHttpClient asAbstractClient(HttpClient client) { + if (AbstractHttpClient.class.isAssignableFrom(client.getClass())) { + return (AbstractHttpClient) client; } + return null; } /** @@ -1001,7 +1023,12 @@ public class HttpOp { } /** - * Applies authentication to the client as appropriate + * Applies authentication to the given client as appropriate + * <p> + * If a null authenticator is provided this method tries to use the + * registered default authenticator which may be set via the + * {@link HttpOp#setDefaultAuthenticator(HttpAuthenticator)} method. + * </p> * * @param client * HTTP Client @@ -1012,8 +1039,12 @@ public class HttpOp { * @param authenticator * HTTP Authenticator */ - private static void applyAuthentication(DefaultHttpClient client, String target, HttpContext context, + public static void applyAuthentication(AbstractHttpClient client, String target, HttpContext context, HttpAuthenticator authenticator) { + // Cannot apply to null client + if (client == null) + return; + // Fallback to default authenticator if null authenticator provided if (authenticator == null) authenticator = defaultAuthenticator; Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java?rev=1497583&r1=1497582&r2=1497583&view=diff ============================================================================== --- jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java (original) +++ jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java Thu Jun 27 22:14:09 2013 @@ -52,6 +52,8 @@ import com.hp.hpl.jena.rdf.model.ModelFa import com.hp.hpl.jena.shared.JenaException ; import com.hp.hpl.jena.sparql.graph.GraphFactory ; import com.hp.hpl.jena.sparql.graph.UnmodifiableGraph ; + +// TODO Support use of a HttpAuthenticator public class DatasetGraphAccessorHTTP implements DatasetGraphAccessor { 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=1497583&r1=1497582&r2=1497583&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 Thu Jun 27 22:14:09 2013 @@ -18,12 +18,14 @@ package com.hp.hpl.jena.sparql.engine.http; +import java.net.SocketException; import java.util.HashMap ; import java.util.Map ; import org.junit.AfterClass ; import org.junit.Assert ; import org.junit.BeforeClass ; +import org.junit.Ignore; import org.junit.Test ; import com.hp.hpl.jena.graph.Node ; @@ -49,7 +51,8 @@ public class TestService { @BeforeClass public static void recordContextState() { value = ARQ.getContext().get(Service.serviceContext) ; } @AfterClass public static void restoreContextState() { ARQ.getContext().set(Service.serviceContext, value) ; } - + + @Test public void testNumericTimeout() { BasicPattern basicPattern = new BasicPattern(); @@ -66,10 +69,10 @@ public class TestService { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { - if (expected.getCause() instanceof java.net.SocketTimeoutException) { + if (expected.getCause() instanceof SocketException) { // expected } else { - Assert.fail(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass() + Assert.fail(String.format("Expected SocketException (or subclass thereof), instead got: %s %s", expected.getCause().getClass() .getName(), expected.getCause().getMessage())); } } @@ -92,10 +95,10 @@ public class TestService { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { - if (expected.getCause() instanceof java.net.SocketTimeoutException) { + if (expected.getCause() instanceof java.net.SocketException) { // expected } else { - Assert.fail(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass() + Assert.fail(String.format("Expected SocketException (or subclass thereof), instead got: %s %s", expected.getCause().getClass() .getName(), expected.getCause().getMessage())); } } @@ -117,10 +120,10 @@ public class TestService { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { - if (expected.getCause() instanceof java.net.SocketTimeoutException) { + if (expected.getCause() instanceof java.net.SocketException) { // expected } else { - Assert.fail(String.format("Expected SocketTimeoutException, instead got: %s %s", expected.getCause().getClass() + Assert.fail(String.format("Expected SocketException (or subclass thereof), instead got: %s %s", expected.getCause().getClass() .getName(), expected.getCause().getMessage())); } }
