Repository: jena Updated Branches: refs/heads/master e4b4f80f6 -> cc038809f
Query string in URLs; uniform HttpClient/HttpContext usage. JENA-1330: pass HttpClient/HttpContext in query and update calls. JENA-1331: Support additional query string in URLs for services. Add operations for HttpClient/HttpContext to QueryExecutionFactory and UpdateExecutionFactory. Use HttpContext in HtpQuery, then HttpClientContext.adapt as necessary. Tests for URL generation. Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/5c5518a7 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/5c5518a7 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/5c5518a7 Branch: refs/heads/master Commit: 5c5518a765f5c22749be3a2b9cac6ab5431f4627 Parents: e4b4f80 Author: Andy Seaborne <[email protected]> Authored: Thu Apr 27 14:18:26 2017 +0100 Committer: Andy Seaborne <[email protected]> Committed: Thu Apr 27 14:18:26 2017 +0100 ---------------------------------------------------------------------- .../jena/query/QueryExecutionFactory.java | 96 ++++++++++++-- .../jena/sparql/engine/http/HttpQuery.java | 33 ++--- .../sparql/engine/http/QueryEngineHTTP.java | 35 +++-- .../jena/sparql/modify/UpdateProcessRemote.java | 5 +- .../sparql/modify/UpdateProcessRemoteForm.java | 7 +- .../jena/update/UpdateExecutionFactory.java | 114 +++++++++++++++-- .../rdfconnection/TestRDFConnectionRemote.java | 20 ++- .../org/apache/jena/rdfconnection/RDFConn.java | 41 +++++- .../rdfconnection/RDFConnectionFactory.java | 3 +- .../jena/rdfconnection/RDFConnectionRemote.java | 25 ++-- .../jena/rdfconnection/TS_RDFConnection.java | 3 +- .../apache/jena/rdfconnection/TestRDFConn.java | 127 +++++++++++++++++++ 12 files changed, 426 insertions(+), 83 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/query/QueryExecutionFactory.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/query/QueryExecutionFactory.java b/jena-arq/src/main/java/org/apache/jena/query/QueryExecutionFactory.java index 3738099..7e2adf1 100644 --- a/jena-arq/src/main/java/org/apache/jena/query/QueryExecutionFactory.java +++ b/jena-arq/src/main/java/org/apache/jena/query/QueryExecutionFactory.java @@ -20,6 +20,7 @@ package org.apache.jena.query; import java.util.List ; import org.apache.http.client.HttpClient; +import org.apache.http.protocol.HttpContext; import org.apache.jena.atlas.logging.Log ; import org.apache.jena.rdf.model.Model ; import org.apache.jena.sparql.core.DatasetGraph ; @@ -281,13 +282,17 @@ public class QueryExecutionFactory return sparqlService(service, query, (HttpClient)null) ; } - /** Create a QueryExecution that will access a SPARQL service over HTTP + static public QueryExecution sparqlService(String service, String query, HttpClient client) { + return sparqlService(service, query, client, null); + } + /** Create a QueryExecution that will access a SPARQL service over HTTP * @param service URL of the remote service * @param query Query string to execute * @param client HTTP client + * @param httpContext HTTP Context * @return QueryExecution */ - static public QueryExecution sparqlService(String service, String query, HttpClient client) { + static public QueryExecution sparqlService(String service, String query, HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; checkArg(query) ; return sparqlService(service, QueryFactory.create(query), client) ; @@ -311,10 +316,22 @@ public class QueryExecutionFactory * @return QueryExecution */ static public QueryExecution sparqlService(String service, String query, String defaultGraph, HttpClient client) { + return sparqlService(service, query, defaultGraph, client, null) ; + } + + /** Create a QueryExecution that will access a SPARQL service over HTTP + * @param service URL of the remote service + * @param query Query string to execute + * @param defaultGraph URI of the default graph + * @param client HTTP client + * @param httpContext HTTP Context + * @return QueryExecution + */ + static public QueryExecution sparqlService(String service, String query, String defaultGraph, HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; // checkNotNull(defaultGraph, "IRI for default graph is null") ; checkArg(query) ; - return sparqlService(service, QueryFactory.create(query), defaultGraph, client) ; + return sparqlService(service, QueryFactory.create(query), defaultGraph, client, httpContext) ; } /** Create a QueryExecution that will access a SPARQL service over HTTP @@ -338,11 +355,25 @@ public class QueryExecutionFactory */ static public QueryExecution sparqlService(String service, String query, List<String> defaultGraphURIs, List<String> namedGraphURIs, HttpClient client) { + return sparqlService(service, query, defaultGraphURIs, namedGraphURIs, client, null) ; + } + + /** Create a QueryExecution that will access a SPARQL service over HTTP + * @param service URL of the remote service + * @param query Query string to execute + * @param defaultGraphURIs List of URIs to make up the default graph + * @param namedGraphURIs List of URIs to make up the named graphs + * @param client HTTP client + * @param httpContext HTTP Context + * @return QueryExecution + */ + static public QueryExecution sparqlService(String service, String query, List<String> defaultGraphURIs, List<String> namedGraphURIs, + HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; // checkNotNull(defaultGraphURIs, "List of default graph URIs is null") ; // checkNotNull(namedGraphURIs, "List of named graph URIs is null") ; checkArg(query) ; - return sparqlService(service, QueryFactory.create(query), defaultGraphURIs, namedGraphURIs, client) ; + return sparqlService(service, QueryFactory.create(query), defaultGraphURIs, namedGraphURIs, client, httpContext) ; } /** Create a QueryExecution that will access a SPARQL service over HTTP @@ -367,6 +398,18 @@ public class QueryExecutionFactory } /** Create a QueryExecution that will access a SPARQL service over HTTP + * @param service URL of the remote service + * @param query Query to execute + * @param client HTTP client + * @return QueryExecution + */ + static public QueryExecution sparqlService(String service, Query query, HttpClient client, HttpContext httpContext) { + checkNotNull(service, "URL for service is null") ; + checkArg(query) ; + return createServiceRequest(service, query, client, httpContext) ; + } + + /** Create a QueryExecution that will access a SPARQL service over HTTP * @param service URL of the remote service * @param query Query to execute * @param defaultGraphURIs List of URIs to make up the default graph @@ -374,7 +417,7 @@ public class QueryExecutionFactory * @return QueryExecution */ static public QueryExecution sparqlService(String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs) { - return sparqlService(service, query, defaultGraphURIs, namedGraphURIs, null) ; + return sparqlService(service, query, defaultGraphURIs, namedGraphURIs, null, null) ; } /** Create a QueryExecution that will access a SPARQL service over HTTP @@ -385,8 +428,21 @@ public class QueryExecutionFactory * @param client HTTP client * @return QueryExecution */ + static public QueryExecution sparqlService(String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs, HttpClient client) { + return sparqlService(service, query, defaultGraphURIs, namedGraphURIs, client, null); + } + + /** Create a QueryExecution that will access a SPARQL service over HTTP + * @param service URL of the remote service + * @param query Query to execute + * @param defaultGraphURIs List of URIs to make up the default graph + * @param namedGraphURIs List of URIs to make up the named graphs + * @param client HTTP client + * @param httpContext HTTP Context + * @return QueryExecution + */ static public QueryExecution sparqlService(String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs, - HttpClient client) { + HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; // checkNotNull(defaultGraphURIs, "List of default graph URIs is null") ; // checkNotNull(namedGraphURIs, "List of named graph URIs is null") ; @@ -417,14 +473,24 @@ public class QueryExecutionFactory * @return QueryExecution */ static public QueryExecution sparqlService(String service, Query query, String defaultGraph, HttpClient client) { + return sparqlService(service, query, client, null); + } + + /** Create a QueryExecution that will access a SPARQL service over HTTP + * @param service URL of the remote service + * @param query Query to execute + * @param defaultGraph URI of the default graph + * @param client HTTP client + * @return QueryExecution + */ + static public QueryExecution sparqlService(String service, Query query, String defaultGraph, HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; // checkNotNull(defaultGraph, "IRI for default graph is null") ; checkArg(query) ; - QueryEngineHTTP qe = createServiceRequest(service, query, client) ; + QueryEngineHTTP qe = createServiceRequest(service, query, client, httpContext) ; qe.addDefaultGraph(defaultGraph) ; return qe ; } - /** Create a service request for remote execution over HTTP. The returned class, * {@link QueryEngineHTTP}, * allows various HTTP specific parameters to be set. @@ -449,6 +515,20 @@ public class QueryExecutionFactory return qe ; } + /** Create a service request for remote execution over HTTP. The returned class, + * {@link QueryEngineHTTP}, + * allows various HTTP specific parameters to be set. + * @param service Endpoint URL + * @param query Query + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Query Engine + */ + static public QueryEngineHTTP createServiceRequest(String service, Query query, HttpClient client, HttpContext httpContext) { + QueryEngineHTTP qe = new QueryEngineHTTP(service, query, client, httpContext) ; + return qe ; + } + // ----------------- static public Plan createPlan(Query query, DatasetGraph dataset, Binding input, Context context) { http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java index 9f5b45f..7d7ccd8 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java @@ -27,6 +27,8 @@ import java.util.regex.Pattern ; import org.apache.http.client.HttpClient ; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; import org.apache.jena.atlas.web.HttpException ; import org.apache.jena.atlas.web.TypedInputStream ; import org.apache.jena.query.ARQ ; @@ -69,7 +71,7 @@ public class HttpQuery extends Params { private boolean allowCompression = false; private HttpClient client; - private HttpClientContext context; + private HttpContext context; /** * Create a execution object for a whole model GET @@ -177,7 +179,7 @@ public class HttpQuery extends Params { * Sets the context to use * @param context HTTP context */ - public void setContext(HttpClientContext context) { + public void setContext(HttpContext context) { this.context = context; } @@ -200,10 +202,11 @@ public class HttpQuery extends Params { /** * Gets the HTTP context that is being used, or sets and returns a default - * @return the {@code HttpClientContext} in scope + * @return the {@code HttpContext} in scope */ - public HttpClientContext getContext() { - if (context == null) context = new HttpClientContext(); + public HttpContext getContext() { + if (context == null) + context = new BasicHttpContext(); return context; } @@ -274,8 +277,11 @@ public class HttpQuery extends Params { */ public InputStream exec() throws QueryExceptionHTTP { // Select the appropriate HttpClient to use - contextualizeCompressionSettings(); - contextualizeTimeoutSettings(); + HttpClientContext hcc = HttpClientContext.adapt(getContext()); + RequestConfig.Builder builder = RequestConfig.copy(hcc.getRequestConfig()); + contextualizeCompressionSettings(builder); + contextualizeTimeoutSettings(builder); + hcc.setRequestConfig(builder.build()); try { if (usesPOST()) return execPost(); @@ -289,17 +295,14 @@ public class HttpQuery extends Params { } } - private void contextualizeCompressionSettings() { - final RequestConfig.Builder builder = RequestConfig.copy(getContext().getRequestConfig()); + private void contextualizeCompressionSettings(RequestConfig.Builder builder) { builder.setContentCompressionEnabled(allowCompression); - context.setRequestConfig(builder.build()); } - private void contextualizeTimeoutSettings() { - final RequestConfig.Builder builder = RequestConfig.copy(context.getRequestConfig()); - if (connectTimeout > 0) builder.setConnectTimeout(connectTimeout); - - context.setRequestConfig(builder.build()); + private void contextualizeTimeoutSettings(RequestConfig.Builder builder) { + if (connectTimeout <= 0) + return; + builder.setConnectTimeout(connectTimeout); } private InputStream execGet() throws QueryExceptionHTTP { http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java index 417e327..713cbc0 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java @@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit ; import org.apache.http.client.HttpClient ; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.protocol.HttpContext ; import org.apache.jena.atlas.RuntimeIOException; import org.apache.jena.atlas.io.IO ; import org.apache.jena.atlas.lib.Pair ; @@ -71,7 +72,7 @@ public class QueryEngineHTTP implements QueryExecution { private List<String> defaultGraphURIs = new ArrayList<>(); private List<String> namedGraphURIs = new ArrayList<>(); private HttpClient client; - private HttpClientContext httpContext; + private HttpContext httpContext; private boolean closed = false; @@ -110,26 +111,30 @@ public class QueryEngineHTTP implements QueryExecution { private InputStream retainedConnection = null; public QueryEngineHTTP(String serviceURI, Query query) { - this(serviceURI, query, query.toString()); + this(serviceURI, query, null, null); } public QueryEngineHTTP(String serviceURI, Query query, HttpClient client) { - this(serviceURI, query, query.toString(), client); + this(serviceURI, query, client, null); + } + + public QueryEngineHTTP(String serviceURI, Query query, HttpClient client, HttpContext httpContext) { + this(serviceURI, query, query.toString(), client, httpContext); } public QueryEngineHTTP(String serviceURI, String queryString) { - this(serviceURI, null, queryString); + this(serviceURI, queryString, null, null); } public QueryEngineHTTP(String serviceURI, String queryString, HttpClient client) { - this(serviceURI, null, queryString, client); - } - - private QueryEngineHTTP(String serviceURI, Query query, String queryString) { - this(serviceURI, query, queryString, null); + this(serviceURI, queryString, client, null); } - private QueryEngineHTTP(String serviceURI, Query query, String queryString, HttpClient client) { + public QueryEngineHTTP(String serviceURI, String queryString, HttpClient client, HttpContext httpContext) { + this(serviceURI, null, queryString, client, httpContext); + } + + private QueryEngineHTTP(String serviceURI, Query query, String queryString, HttpClient client, HttpContext httpContext) { this.query = query; this.queryString = queryString; this.service = serviceURI; @@ -142,6 +147,7 @@ public class QueryEngineHTTP implements QueryExecution { // service context in the parent constructor if the specified // client is null if (client != null) setClient(client); + if (httpContext != null) setHttpContext(httpContext); } /** @@ -310,16 +316,16 @@ public class QueryEngineHTTP implements QueryExecution { * * @param context HTTP context */ - public void setHttpContext(HttpClientContext context) { + public void setHttpContext(HttpContext context) { this.httpContext = context; } /** * Get the HTTP context in use, if none is set then null. * - * @return the {@code HttpClientContext} in scope + * @return the {@code HttpContext} in scope */ - public HttpClientContext getHttpContext() { + public HttpContext getHttpContext() { return httpContext; } @@ -619,7 +625,8 @@ public class QueryEngineHTTP implements QueryExecution { } } httpQuery.setClient(client); - httpQuery.setContext(getHttpContext()); + HttpClientContext hcc = ( httpContext == null ) ? null : HttpClientContext.adapt(httpContext); + httpQuery.setContext(hcc); // Apply timeouts if (connectTimeout > 0) httpQuery.setConnectTimeout((int) connectTimeoutUnit.toMillis(connectTimeout)); http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemote.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemote.java b/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemote.java index 506de60..1d8ef65 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemote.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemote.java @@ -19,6 +19,7 @@ package org.apache.jena.sparql.modify; import org.apache.http.client.HttpClient; +import org.apache.http.protocol.HttpContext; import org.apache.jena.riot.WebContent ; import org.apache.jena.riot.web.HttpOp ; import org.apache.jena.sparql.ARQException ; @@ -47,14 +48,16 @@ public class UpdateProcessRemote extends UpdateProcessRemoteBase * @param endpoint Update endpoint * @param context Context * @param client HTTP client + * @param httpContext HTTP Context */ - public UpdateProcessRemote(UpdateRequest request, String endpoint, Context context, HttpClient client) + public UpdateProcessRemote(UpdateRequest request, String endpoint, Context context, HttpClient client, HttpContext httpContext) { this(request, endpoint, context); // Don't want to overwrite config we may have picked up from // service context in the parent constructor if the specified // client is null if (client != null) this.setClient(client); + if (httpContext != null) this.setHttpContext(httpContext); } @Override http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemoteForm.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemoteForm.java b/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemoteForm.java index de3a67d..fc9b840 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemoteForm.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/modify/UpdateProcessRemoteForm.java @@ -21,6 +21,7 @@ package org.apache.jena.sparql.modify; import static org.apache.jena.riot.web.HttpOp.execHttpPostForm; import org.apache.http.client.HttpClient; +import org.apache.http.protocol.HttpContext; import org.apache.jena.riot.web.HttpResponseLib; import org.apache.jena.sparql.ARQException ; import org.apache.jena.sparql.engine.http.HttpParams ; @@ -62,13 +63,17 @@ public class UpdateProcessRemoteForm extends UpdateProcessRemoteBase { * Context * @param client * HTTP Client + * @param httpContext + * HTTP Context + */ - public UpdateProcessRemoteForm(UpdateRequest request, String endpoint, Context context, HttpClient client) { + public UpdateProcessRemoteForm(UpdateRequest request, String endpoint, Context context, HttpClient client, HttpContext httpContext) { this(request, endpoint, context); // Don't want to overwrite config we may have picked up from // service context in the parent constructor if the specified // client is null if (client != null) this.setClient(client); + if (httpContext != null) this.setHttpContext(httpContext); } @Override http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-arq/src/main/java/org/apache/jena/update/UpdateExecutionFactory.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/update/UpdateExecutionFactory.java b/jena-arq/src/main/java/org/apache/jena/update/UpdateExecutionFactory.java index a30ac4b..1569562 100644 --- a/jena-arq/src/main/java/org/apache/jena/update/UpdateExecutionFactory.java +++ b/jena-arq/src/main/java/org/apache/jena/update/UpdateExecutionFactory.java @@ -19,6 +19,7 @@ package org.apache.jena.update; import org.apache.http.client.HttpClient; +import org.apache.http.protocol.HttpContext; import org.apache.jena.query.ARQ ; import org.apache.jena.query.Dataset ; import org.apache.jena.query.QuerySolution ; @@ -275,8 +276,6 @@ public class UpdateExecutionFactory return uProc; } - - /** Create an UpdateProcessor that sends the update to a remote SPARQL Update service. * @param update Updates * @param remoteEndpoint Endpoint URL @@ -284,7 +283,7 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemote(Update update, String remoteEndpoint) { - return createRemote(new UpdateRequest(update), remoteEndpoint, null, null) ; + return createRemote(new UpdateRequest(update), remoteEndpoint, null, null, null) ; } /** Create an UpdateProcessor that sends the update to a remote SPARQL Update service. @@ -295,7 +294,18 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemote(Update update, String remoteEndpoint, HttpClient client) { - return createRemote(new UpdateRequest(update), remoteEndpoint, null, client) ; + return createRemote(update, remoteEndpoint, client, null); + } + + /** Create an UpdateProcessor that sends the update to a remote SPARQL Update service. + * @param update Updates + * @param remoteEndpoint Endpoint URL + * @param client HTTP client + * @return Remote Update processor + */ + public static UpdateProcessor createRemote(Update update, String remoteEndpoint, HttpClient client, HttpContext httpContext) + { + return createRemote(new UpdateRequest(update), remoteEndpoint, null, client, httpContext) ; } /** Create an UpdateProcessor that sends the update to a remote SPARQL Update service. @@ -318,6 +328,19 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemote(Update update, String remoteEndpoint, Context context, HttpClient client) { + return createRemote(update, remoteEndpoint, context, client, null); + } + + /** Create an UpdateProcessor that sends the update to a remote SPARQL Update service. + * @param update Updates + * @param remoteEndpoint Endpoint URL + * @param context Context + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemote(Update update, String remoteEndpoint, Context context, HttpClient client, HttpContext httpContext) + { return createRemote(new UpdateRequest(update), remoteEndpoint, context, client) ; } @@ -328,7 +351,7 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemote(UpdateRequest updateRequest, String remoteEndpoint) { - return createRemote(updateRequest, remoteEndpoint, null, null) ; + return createRemote(updateRequest, remoteEndpoint, null, null, null) ; } /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service. @@ -345,6 +368,18 @@ public class UpdateExecutionFactory /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service. * @param updateRequest Updates * @param remoteEndpoint Endpoint URL + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemote(UpdateRequest updateRequest, String remoteEndpoint, HttpClient client, HttpContext httpContext) + { + return createRemote(updateRequest, remoteEndpoint, null, client, httpContext) ; + } + + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service. + * @param updateRequest Updates + * @param remoteEndpoint Endpoint URL * @param context Context * @return Remote Update processor */ @@ -362,7 +397,20 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemote(UpdateRequest updateRequest, String remoteEndpoint, Context context, HttpClient client) { - return new UpdateProcessRemote(updateRequest, remoteEndpoint, context, client) ; + return new UpdateProcessRemote(updateRequest, remoteEndpoint, context, client, null) ; + } + + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service. + * @param updateRequest Updates + * @param remoteEndpoint Endpoint URL + * @param context Context + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemote(UpdateRequest updateRequest, String remoteEndpoint, Context context, HttpClient client, HttpContext httpContext) + { + return new UpdateProcessRemote(updateRequest, remoteEndpoint, context, client, httpContext) ; } /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form @@ -372,7 +420,7 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemoteForm(Update update, String remoteEndpoint) { - return createRemoteForm(update, remoteEndpoint, null, null) ; + return createRemoteForm(update, remoteEndpoint, null, null, null) ; } /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form @@ -389,6 +437,18 @@ public class UpdateExecutionFactory /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form * @param update Updates * @param remoteEndpoint Endpoint URL + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemoteForm(Update update, String remoteEndpoint, HttpClient client, HttpContext httpContext) + { + return createRemoteForm(update, remoteEndpoint, null, client, httpContext) ; + } + + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form + * @param update Updates + * @param remoteEndpoint Endpoint URL * @param context Context * @return Remote Update processor */ @@ -410,13 +470,24 @@ public class UpdateExecutionFactory } /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form + * @param update Updates + * @param remoteEndpoint Endpoint URL + * @param context Context + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemoteForm(Update update, String remoteEndpoint, Context context, HttpClient client, HttpContext httpContext) { + return createRemoteForm(new UpdateRequest(update), remoteEndpoint, null, client, httpContext) ; + } + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form * @param updateRequest Updates * @param remoteEndpoint Endpoint URL * @return Remote Update processor */ public static UpdateProcessor createRemoteForm(UpdateRequest updateRequest, String remoteEndpoint) { - return createRemoteForm(updateRequest, remoteEndpoint, null, null) ; + return createRemoteForm(updateRequest, remoteEndpoint, null, null, null) ; } /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form @@ -433,6 +504,18 @@ public class UpdateExecutionFactory /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form * @param updateRequest Updates * @param remoteEndpoint Endpoint URL + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemoteForm(UpdateRequest updateRequest, String remoteEndpoint, HttpClient client, HttpContext httpContext) + { + return createRemoteForm(updateRequest, remoteEndpoint, null, client, httpContext) ; + } + + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form + * @param updateRequest Updates + * @param remoteEndpoint Endpoint URL * @param context Context * @return Remote Update processor */ @@ -450,6 +533,19 @@ public class UpdateExecutionFactory */ public static UpdateProcessor createRemoteForm(UpdateRequest updateRequest, String remoteEndpoint, Context context, HttpClient client) { - return new UpdateProcessRemoteForm(updateRequest, remoteEndpoint, context, client) ; + return createRemoteForm(updateRequest, remoteEndpoint, context, client, null) ; + } + + /** Create an UpdateProcessor that sends the update request to a remote SPARQL Update service using an HTML form + * @param updateRequest Updates + * @param remoteEndpoint Endpoint URL + * @param context Context + * @param client HTTP client + * @param httpContext HTTP Context + * @return Remote Update processor + */ + public static UpdateProcessor createRemoteForm(UpdateRequest updateRequest, String remoteEndpoint, Context context, HttpClient client, HttpContext httpContext) + { + return new UpdateProcessRemoteForm(updateRequest, remoteEndpoint, context, client, httpContext) ; } } http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-integration-tests/src/test/java/org/apache/jena/test/rdfconnection/TestRDFConnectionRemote.java ---------------------------------------------------------------------- diff --git a/jena-integration-tests/src/test/java/org/apache/jena/test/rdfconnection/TestRDFConnectionRemote.java b/jena-integration-tests/src/test/java/org/apache/jena/test/rdfconnection/TestRDFConnectionRemote.java index 5abc77e..5f111a5 100644 --- a/jena-integration-tests/src/test/java/org/apache/jena/test/rdfconnection/TestRDFConnectionRemote.java +++ b/jena-integration-tests/src/test/java/org/apache/jena/test/rdfconnection/TestRDFConnectionRemote.java @@ -51,28 +51,26 @@ public class TestRDFConnectionRemote extends AbstractTestRDFConnection { server.start() ; } - @AfterClass - public static void afterClass() { - server.stop(); - } - @Before public void beforeTest() { // Clear server Txn.executeWrite(serverdsg, ()->serverdsg.clear()) ; } - -// @After -// public void afterTest() {} -// } +// @After +// public void afterTest() {} + + @AfterClass + public static void afterClass() { + server.stop(); + } + @Override protected boolean supportsAbort() { return false ; } @Override protected RDFConnection connection() { - return RDFConnectionFactory.connect("http://localhost:2244/ds") ; + return RDFConnectionFactory.connect("http://localhost:2244/ds"); } - } http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConn.java ---------------------------------------------------------------------- diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConn.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConn.java index 27d8f96..63edd75 100644 --- a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConn.java +++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConn.java @@ -18,6 +18,8 @@ package org.apache.jena.rdfconnection; +import java.util.Objects; + /** package-wide utilities etc */ /*package*/ class RDFConn { private static String dftName = "default" ; @@ -26,15 +28,44 @@ package org.apache.jena.rdfconnection; return name == null || name.equals(dftName) ; } - /*package*/ static String queryStringForGraph(String graphName) { + private static String queryStringForGraph(String ch, String graphName) { return - (RDFConn.isDefault(graphName) ) - ? "?default" - : "?graph="+graphName ; + ch + + (RDFConn.isDefault(graphName) + ? "default" + : "graph="+graphName) ; } /*package*/ static String urlForGraph(String graphStoreProtocolService, String graphName) { - return graphStoreProtocolService + queryStringForGraph(graphName) ; + // If query string + String ch = "?"; + if ( graphStoreProtocolService.contains("?") ) + // Already has a query string, append with "&" + ch = "&"; + return graphStoreProtocolService + queryStringForGraph(ch, graphName) ; + } + + /*package*/ static String formServiceURL(String destination, String srvEndpoint) { + Objects.requireNonNull(srvEndpoint, "Service Endpoint"); + if ( destination == null ) + return srvEndpoint; + // If the srvEndpoint looks like an absolute URL, use as given. + if ( srvEndpoint.startsWith("http:/") || srvEndpoint.startsWith("https:/") ) + return srvEndpoint; + String queryString = null; + String dest = destination; + if ( destination.contains("?") ) { + // query string : remove and append later. + int i = destination.indexOf('?'); + queryString = destination.substring(i); + dest = destination.substring(0, i); + } + if ( dest.endsWith("/") ) + dest = dest.substring(0, dest.length()-1); + dest = dest+"/"+srvEndpoint; + if ( queryString != null ) + dest = dest+queryString; + return dest; } } http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java ---------------------------------------------------------------------- diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java index a8c0e45..1e69b19 100644 --- a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java +++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java @@ -55,8 +55,7 @@ public class RDFConnectionFactory { String updateServiceEndpoint, String graphStoreProtocolEndpoint) { return new RDFConnectionRemote(queryServiceEndpoint, updateServiceEndpoint, graphStoreProtocolEndpoint); - } - + } /** Create a connection to a remote location by URL. * This is the URL for the dataset. http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java ---------------------------------------------------------------------- diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java index 183cff1..5467000 100644 --- a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java +++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java @@ -50,7 +50,7 @@ import org.apache.jena.update.UpdateRequest; import org.apache.jena.web.HttpSC; /** - * Implemntation of the {@link RDFConnection} interface using remote SPARQL operations. + * Implementation of the {@link RDFConnection} interface using remote SPARQL operations. */ public class RDFConnectionRemote implements RDFConnection { private static final String fusekiDftSrvQuery = "sparql"; @@ -74,7 +74,6 @@ public class RDFConnectionRemote implements RDFConnection { fusekiDftSrvGSP); } - /** Create connection, using URL of the dataset and default service names */ public RDFConnectionRemote(String destination) { this(requireNonNull(destination), @@ -83,7 +82,6 @@ public class RDFConnectionRemote implements RDFConnection { fusekiDftSrvGSP); } - // ?? /** Create connection, using full URLs for services. Pass a null for "no service endpoint". */ public RDFConnectionRemote(String sQuery, String sUpdate, String sGSP) { this(null, sQuery, sUpdate, sGSP); @@ -97,9 +95,9 @@ public class RDFConnectionRemote implements RDFConnection { /** Create connection, using URL of the dataset and short names for the services */ public RDFConnectionRemote(HttpClient httpClient, String destination, String sQuery, String sUpdate, String sGSP) { this.destination = destination; - this.svcQuery = formServiceURL(destination,sQuery); - this.svcUpdate = formServiceURL(destination,sUpdate); - this.svcGraphStore = formServiceURL(destination,sGSP); + this.svcQuery = RDFConn.formServiceURL(destination, sQuery); + this.svcUpdate = RDFConn.formServiceURL(destination, sUpdate); + this.svcGraphStore = RDFConn.formServiceURL(destination, sGSP); this.httpClient = httpClient; } @@ -119,25 +117,20 @@ public class RDFConnectionRemote implements RDFConnection { this.httpContext = httpContext; } - private static String formServiceURL(String destination, String srvEndpoint) { - if ( destination == null ) - return srvEndpoint; - String dest = destination; - if ( dest.endsWith("/") ) - dest = dest.substring(0, dest.length()-1); - return dest+"/"+srvEndpoint; - } + + // Needs HttpContext + @Override public QueryExecution query(Query query) { checkQuery(); - return exec(()->QueryExecutionFactory.createServiceRequest(svcQuery, query)); + return exec(()->QueryExecutionFactory.sparqlService(svcQuery, query, this.httpClient, this.httpContext)); } @Override public void update(UpdateRequest update) { checkUpdate(); - UpdateProcessor proc = UpdateExecutionFactory.createRemote(update, svcUpdate); + UpdateProcessor proc = UpdateExecutionFactory.createRemote(update, svcUpdate, this.httpClient, this.httpContext); exec(()->proc.execute()); } http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TS_RDFConnection.java ---------------------------------------------------------------------- diff --git a/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TS_RDFConnection.java b/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TS_RDFConnection.java index 373bd52..82af6a9 100644 --- a/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TS_RDFConnection.java +++ b/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TS_RDFConnection.java @@ -23,10 +23,11 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses( { - // Other tests are in jena-integration-tests + // Other tests, especifically for RDFCommectionRemote are in jena-integration-tests TestRDFConnectionLocalTxnMem.class , TestRDFConnectionLocalMRSW.class , TestLocalIsolation.class + , TestRDFConn.class }) public class TS_RDFConnection {} http://git-wip-us.apache.org/repos/asf/jena/blob/5c5518a7/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TestRDFConn.java ---------------------------------------------------------------------- diff --git a/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TestRDFConn.java b/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TestRDFConn.java new file mode 100644 index 0000000..0f6bfa4 --- /dev/null +++ b/jena-rdfconnection/src/test/java/org/apache/jena/rdfconnection/TestRDFConn.java @@ -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 org.apache.jena.rdfconnection; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestRDFConn { + + @Test public void service_url_01() { + testServiceName(null, "XYZ", "XYZ"); + } + + @Test public void service_url_02() { + testServiceName("http://example/", "XYZ", "http://example/XYZ"); + } + + @Test public void service_url_03() { + testServiceName("http://example/abc", "XYZ", "http://example/abc/XYZ"); + } + + @Test public void service_url_04() { + testServiceName("http://example/abc/", "XYZ", "http://example/abc/XYZ"); + } + + @Test public void service_url_05() { + testServiceName("http://example/abc?param=value", "XYZ", "http://example/abc/XYZ?param=value"); + } + + @Test public void service_url_06() { + testServiceName("http://example/dataset", "http://other/abc/", "http://other/abc/"); + } + + @Test public void service_url_07() { + testServiceName("http://example/dataset", "http://example/abc/XYZ?param=value", "http://example/abc/XYZ?param=value"); + } + + private static void testServiceName(String destination, String service, String expected) { + String x = RDFConn.formServiceURL(destination, service); + assertEquals(expected, x); + } + + // Assumes service name constructed correctly (see above). + + @Test public void gsp_url_01() { + testGSP("http://example/", null, "http://example/?default"); + } + + @Test public void gsp_url_02() { + testGSP("http://example/", "default", "http://example/?default"); + } + + @Test public void gsp_url_03() { + testGSP("http://example/dataset", null, "http://example/dataset?default"); + } + + @Test public void gsp_url_04() { + testGSP("http://example/dataset", "default", "http://example/dataset?default"); + } + + @Test public void gsp_url_05() { + testGSP("http://example/dataset?param=value", "default", "http://example/dataset?param=value&default"); + } + + @Test public void gsp_url_06() { + testGSP("http://example/?param=value", "default", "http://example/?param=value&default"); + } + + @Test public void gsp_url_07() { + testGSP("http://example/dataset?param=value", "default", "http://example/dataset?param=value&default"); + } + + @Test public void gsp_url_08() { + testGSP("http://example/dataset/?param=value", "default", "http://example/dataset/?param=value&default"); + } + + @Test public void gsp_url_11() { + testGSP("http://example/dataset", "name", "http://example/dataset?graph=name"); + } + + @Test public void gsp_url_12() { + testGSP("http://example/", "name", "http://example/?graph=name"); + } + + @Test public void gsp_url_13() { + testGSP("http://example/dataset/", "name", "http://example/dataset/?graph=name"); + } + + @Test public void gsp_url_20() { + testGSP("http://example/dataset?param=value", null, "http://example/dataset?param=value&default"); + } + + @Test public void gsp_url_21() { + testGSP("http://example/?param=value", null, "http://example/?param=value&default"); + } + + @Test public void gsp_url_16() { + testGSP("http://example/dataset?param=value", "name", "http://example/dataset?param=value&graph=name"); + } + + @Test public void gsp_url_17() { + testGSP("http://example/?param=value", "name", "http://example/?param=value&graph=name"); + } + + private void testGSP(String gsp, String graphName, String expected) { + String x = RDFConn.urlForGraph(gsp, graphName); + assertEquals(expected, x); + } + +}
