diff --git a/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java b/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java index 58cd087d5f7..5303c7a49d5 100644 --- a/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java +++ b/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java @@ -695,19 +695,49 @@ public static CompletableFuture> executeJDKAsync(HttpClient return httpClient.sendAsync(httpRequest, BodyHandlers.ofInputStream()); } + /** + * Push data. POST, PUT, PATCH request with no response body data. + * @deprecated Use {@link #httpPushData(HttpClient, HttpMethod, String, Consumer, BodyPublisher)} + */ + @Deprecated(forRemoval = true) + public static void httpPushData(HttpClient httpClient, @SuppressWarnings("removal") Push style, String url, Consumer modifier, BodyPublisher body) { + HttpResponse response = httpPushWithResponse(httpClient, style.method(), url, modifier, body); + handleResponseNoBody(response); + } + /** Push data. POST, PUT, PATCH request with no response body data. */ - public static void httpPushData(HttpClient httpClient, Push style, String url, Consumer modifier, BodyPublisher body) { - HttpResponse response = httpPushWithResponse(httpClient, style, url, modifier, body); + public static void httpPushData(HttpClient httpClient, HttpMethod method, String url, Consumer modifier, BodyPublisher body) { + HttpResponse response = httpPushWithResponse(httpClient, method, url, modifier, body); handleResponseNoBody(response); } + /** + * Push data. POST, PUT, PATCH request with no response body data. + * @deprecated Use {@link #httpPushWithResponse(HttpClient, HttpMethod, String, Consumer, BodyPublisher)} + */ + @Deprecated(forRemoval = true) + public static HttpResponse httpPushWithResponse(HttpClient httpClient, @SuppressWarnings("removal") Push style, String url, + Consumer modifier, BodyPublisher body) { + return httpPushWithResponse(httpClient, style.method(), url, modifier, body); + + } // Worker - public static HttpResponse httpPushWithResponse(HttpClient httpClient, Push style, String url, + public static HttpResponse httpPushWithResponse(HttpClient httpClient, HttpMethod method, String url, Consumer modifier, BodyPublisher body) { + switch(method) { + case POST,PUT, PATCH -> {} + default -> { throw HttpException.error("Method not suitable for push operation: "+method); } + } + return httpPushWithResponse(httpClient, method.method(), url, modifier, body); + } + + + private static HttpResponse httpPushWithResponse(HttpClient httpClient, String method, String url, + Consumer modifier, BodyPublisher body) { URI uri = toRequestURI(url); HttpRequest.Builder builder = requestBuilderFor(url); builder.uri(uri); - builder.method(style.method(), body); + builder.method(method, body); if ( modifier != null ) modifier.accept(builder); HttpResponse response = execute(httpClient, builder.build()); @@ -817,7 +847,7 @@ public static String responseHeader(HttpResponse response, String headerName) */ public static boolean isFuseki(String datasetURL) { HttpRequest.Builder builder = - HttpRequest.newBuilder().uri(toRequestURI(datasetURL)).method(HttpNames.METHOD_HEAD, BodyPublishers.noBody()); + HttpRequest.newBuilder().uri(toRequestURI(datasetURL)).method(HttpMethod.METHOD_HEAD, BodyPublishers.noBody()); HttpRequest request = builder.build(); HttpClient httpClient = HttpEnv.getHttpClient(datasetURL); HttpResponse response = execute(httpClient, request); diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpMethod.java b/jena-arq/src/main/java/org/apache/jena/http/HttpMethod.java similarity index 63% rename from jena-arq/src/main/java/org/apache/jena/riot/web/HttpMethod.java rename to jena-arq/src/main/java/org/apache/jena/http/HttpMethod.java index 47b02f196fc..256c106bc7b 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpMethod.java +++ b/jena-arq/src/main/java/org/apache/jena/http/HttpMethod.java @@ -19,25 +19,34 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.apache.jena.riot.web; +package org.apache.jena.http; import static org.apache.jena.atlas.lib.Lib.uppercase; public enum HttpMethod { - // METHOD_ only for transition/rename. - METHOD_DELETE("DELETE"), - METHOD_HEAD("HEAD"), - METHOD_GET("GET"), - METHOD_QUERY("QUERY"), - METHOD_OPTIONS("OPTIONS"), - METHOD_PATCH("PATCH"), - METHOD_POST("POST"), - METHOD_PUT("PUT"), - METHOD_TRACE("TRACE") + DELETE("DELETE"), + HEAD("HEAD"), + GET("GET"), + QUERY("QUERY"), + OPTIONS("OPTIONS"), + PATCH("PATCH"), + POST("POST"), + PUT("PUT"), + TRACE("TRACE") ; private final String method; + public static final String METHOD_DELETE = "DELETE"; + public static final String METHOD_HEAD = "HEAD"; + public static final String METHOD_GET = "GET"; + public static final String METHOD_QUERY = "QUERY" ; + public static final String METHOD_OPTIONS = "OPTIONS"; + public static final String METHOD_PATCH = "PATCH" ; + public static final String METHOD_POST = "POST"; + public static final String METHOD_PUT = "PUT"; + public static final String METHOD_TRACE = "TRACE"; + private HttpMethod(String name) { this.method = name; } diff --git a/jena-arq/src/main/java/org/apache/jena/http/HttpOp.java b/jena-arq/src/main/java/org/apache/jena/http/HttpOp.java index d2ad2e22160..d948621715d 100644 --- a/jena-arq/src/main/java/org/apache/jena/http/HttpOp.java +++ b/jena-arq/src/main/java/org/apache/jena/http/HttpOp.java @@ -22,11 +22,7 @@ package org.apache.jena.http; import static org.apache.jena.http.HttpLib.*; -import static org.apache.jena.http.Push.PATCH; -import static org.apache.jena.http.Push.POST; -import static org.apache.jena.http.Push.PUT; -import static org.apache.jena.riot.web.HttpNames.METHOD_HEAD; -import static org.apache.jena.riot.web.HttpNames.METHOD_OPTIONS; +import static org.apache.jena.http.HttpMethod.*; import java.io.InputStream; import java.net.Authenticator; @@ -270,6 +266,7 @@ private static void execHttpPost(HttpClient httpClient, String url, String conte } // ---- POST stream response. + // The URL may have a query string. /** POST - the application MUST close the InputStream.*/ public static TypedInputStream httpPostStream(String url) { @@ -291,7 +288,7 @@ public static TypedInputStream httpPostStream(HttpClient httpClient, String url, return execPostStream(httpClient, url, acceptHeader); } - /** POST(URL) -> InputStream+Content-Type. The application MUST close the InputStream. */ + /** POST(URL), no body -> InputStream+Content-Type. The application MUST close the InputStream. */ private static TypedInputStream execPostStream(HttpClient httpClient, String url, String acceptHeader) { return execPostStream(httpClient, url, null, null, acceptHeader); } @@ -323,15 +320,19 @@ public static TypedInputStream httpPostStream(HttpClient httpClient, String url, return execPostStream(httpClient, url, contentType, bodyContent, acceptHeader); } - /** POST(URL), with a body -> InputStream+Content-Type. The application MUST close the InputStream. */ private static TypedInputStream execPostStream(HttpClient httpClient, String url, String contentType, BodyPublisher bodyPublisher, String acceptHeader) { + return execStream(httpClient, url, HttpMethod.POST, contentType, bodyPublisher, acceptHeader); + } + + /** QUERY/POST/PATCH(URL), with a body -> InputStream+Content-Type. The application MUST close the InputStream. */ + private static TypedInputStream execStream(HttpClient httpClient, String url, HttpMethod method, String contentType, BodyPublisher bodyPublisher, String acceptHeader) { acceptHeader = HttpLib.dft(acceptHeader, "*/"); if ( bodyPublisher == null ) bodyPublisher = BodyPublishers.noBody(); HttpRequest.Builder builder = HttpLib.requestBuilderFor(url).uri(toRequestURI(url)); HttpLib.contentTypeHeader(builder, contentType); HttpLib.acceptHeader(builder, acceptHeader); - HttpRequest request = builder.POST(bodyPublisher).build(); + HttpRequest request = builder.method(method.method(), bodyPublisher).build(); HttpResponse response = HttpLib.execute(httpClient, request); return HttpLib.handleResponseTypedInputStream(response); } @@ -354,6 +355,26 @@ public static void httpPut(HttpClient httpClient, String url, String contentType execPushData(httpClient, PUT, url, contentType, body); } + // ---- QUERY + + /** QUERY + * + */ + /** Perform an HTTP QUERY to a URL. No body. The application MUST close the InputStream. */ + public static TypedInputStream httpQueryStream(String url, String acceptHeader) { + return httpQueryStream(HttpEnv.getHttpClient(url), url, null, null, acceptHeader); + } + + /** QUERY (HTTP method), with a HTTP body - the application MUST close the InputStream.*/ + public static TypedInputStream httpQueryStream(String url, String contentType, BodyPublisher bodyPublisher, String acceptHeader) { + return httpQueryStream(HttpEnv.getHttpClient(url), url, contentType, bodyPublisher, acceptHeader); + } + + /** QUERY (HTTP method), with a HTTP body - the application MUST close the InputStream.*/ + public static TypedInputStream httpQueryStream(HttpClient httpClient, String url, String contentType, BodyPublisher bodyPublisher, String acceptHeader) { + return execStream(httpClient, url, HttpMethod.QUERY, contentType, bodyPublisher, acceptHeader); + } + // ---- PATCH /** PATCH @@ -370,12 +391,12 @@ public static void httpPatch(String url, String contentType, BodyPublisher body) * @see BodyPublishers#ofString */ public static void httpPatch(HttpClient httpClient, String url, String contentType, BodyPublisher body) { - execPushData(httpClient, PATCH, url, contentType, body); + execPushData(httpClient, PATCH, url, contentType, body); } /** Push data. POST, PUT, PATCH request with no response body data. */ - private static void execPushData(HttpClient httpClient, Push style, String url, String contentType, BodyPublisher body) { - HttpLib.httpPushData(httpClient, style, url, setContentTypeHeader(contentType), body); + private static void execPushData(HttpClient httpClient, HttpMethod method, String url, String contentType, BodyPublisher body) { + HttpLib.httpPushData(httpClient, method, url, setContentTypeHeader(contentType), body); } // ---- DELETE @@ -408,7 +429,7 @@ public static String httpOptions(String url) { public static String httpOptions(HttpClient httpClient, String url) { // Need to access the response headers HttpRequest.Builder builder = - HttpLib.requestBuilderFor(url).uri(toRequestURI(url)).method(METHOD_OPTIONS, BodyPublishers.noBody()); + HttpLib.requestBuilderFor(url).uri(toRequestURI(url)).method(OPTIONS.method(), BodyPublishers.noBody()); HttpRequest request = builder.build(); HttpResponse response = execute(httpClient, request); String allowValue = HttpLib.responseHeader(response, HttpNames.hAllow); @@ -452,7 +473,7 @@ public static String httpHead(String url, String acceptHeader) { */ public static String httpHead(HttpClient httpClient, String url, String acceptHeader) { HttpRequest.Builder builder = - HttpLib.requestBuilderFor(url).uri(toRequestURI(url)).method(METHOD_HEAD, BodyPublishers.noBody()); + HttpLib.requestBuilderFor(url).uri(toRequestURI(url)).method(HEAD.method(), BodyPublishers.noBody()); HttpLib.acceptHeader(builder, acceptHeader); HttpRequest request = builder.build(); HttpResponse response = execute(httpClient, request); diff --git a/jena-arq/src/main/java/org/apache/jena/http/HttpRDF.java b/jena-arq/src/main/java/org/apache/jena/http/HttpRDF.java index f7d5cbbaef2..36a3ee8fdb8 100644 --- a/jena-arq/src/main/java/org/apache/jena/http/HttpRDF.java +++ b/jena-arq/src/main/java/org/apache/jena/http/HttpRDF.java @@ -180,7 +180,7 @@ public static void httpPostGraph(HttpClient httpClient, String url, Graph graph, public static void httpPostGraph(HttpClient httpClient, String url, Graph graph, RDFFormat format, Map httpHeaders) { BodyPublisher bodyPublisher = graphToHttpBody(graph, format); - pushBody(httpClient, url, Push.POST, bodyPublisher, format, httpHeaders); + pushBody(httpClient, url, HttpMethod.POST, bodyPublisher, format, httpHeaders); } /** Post a graph and expect an RDF graph back as the result. */ @@ -191,7 +191,7 @@ public static Graph httpPostGraphRtn(String url, Graph graph) { /** Post a graph and expect an RDF graph back as the result. */ public static Graph httpPostGraphRtn(HttpClient httpClient, String url, Graph graph, RDFFormat format, Map httpHeaders) { BodyPublisher bodyPublisher = graphToHttpBody(graph, HttpEnv.defaultTriplesFormat); - HttpResponse httpResponse = pushWithResponse(httpClient, url, Push.POST, bodyPublisher, format, httpHeaders); + HttpResponse httpResponse = pushWithResponse(httpClient, url, HttpMethod.POST, bodyPublisher, format, httpHeaders); Graph graphResponse = GraphFactory.createDefaultGraph(); StreamRDF dest = StreamRDFLib.graph(graphResponse); httpResponseToStreamRDF(url, httpResponse, dest); @@ -205,7 +205,7 @@ public static void httpPostDataset(HttpClient httpClient, String url, DatasetGra public static void httpPostDataset(HttpClient httpClient, String url, DatasetGraph dataset, RDFFormat format, Map httpHeaders) { BodyPublisher bodyPublisher = datasetToHttpBody(dataset, format); - pushBody(httpClient, url, Push.POST, bodyPublisher, format, httpHeaders); + pushBody(httpClient, url, HttpMethod.POST, bodyPublisher, format, httpHeaders); } public static void httpPutGraph(String url, Graph graph) { @@ -219,7 +219,7 @@ public static void httpPutGraph(HttpClient httpClient, String url, Graph graph, public static void httpPutGraph(HttpClient httpClient, String url, Graph graph, RDFFormat format, Map httpHeaders) { BodyPublisher bodyPublisher = graphToHttpBody(graph, format); - pushBody(httpClient, url, Push.PUT, bodyPublisher, format, httpHeaders); + pushBody(httpClient, url, HttpMethod.PUT, bodyPublisher, format, httpHeaders); } public static void httpPutDataset(HttpClient httpClient, String url, DatasetGraph dataset, RDFFormat format) { @@ -229,28 +229,28 @@ public static void httpPutDataset(HttpClient httpClient, String url, DatasetGrap public static void httpPutDataset(HttpClient httpClient, String url, DatasetGraph dataset, RDFFormat format, Map httpHeaders) { BodyPublisher bodyPublisher = datasetToHttpBody(dataset, format); - pushBody(httpClient, url, Push.PUT, bodyPublisher, format, httpHeaders); + pushBody(httpClient, url, HttpMethod.PUT, bodyPublisher, format, httpHeaders); } // Shared between push* and put* - private static void pushBody(HttpClient httpClient, String url, Push style, BodyPublisher bodyPublisher, + private static void pushBody(HttpClient httpClient, String url, HttpMethod method, BodyPublisher bodyPublisher, RDFFormat format, Map httpHeaders) { String contentType = format.getLang().getHeaderString(); if ( httpHeaders == null ) httpHeaders = Collections.singletonMap(HttpNames.hContentType, contentType); else httpHeaders.put(HttpNames.hContentType, contentType); - HttpLib.httpPushData(httpClient, style, url, HttpLib.setHeaders(httpHeaders), bodyPublisher); + HttpLib.httpPushData(httpClient, method, url, HttpLib.setHeaders(httpHeaders), bodyPublisher); } - private static HttpResponse pushWithResponse(HttpClient httpClient, String url, Push style, BodyPublisher bodyPublisher, + private static HttpResponse pushWithResponse(HttpClient httpClient, String url, HttpMethod method, BodyPublisher bodyPublisher, RDFFormat format, Map httpHeaders) { String contentType = format.getLang().getHeaderString(); if ( httpHeaders == null ) httpHeaders = Collections.singletonMap(HttpNames.hContentType, contentType); else httpHeaders.put(HttpNames.hContentType, contentType); - return HttpLib.httpPushWithResponse(httpClient, style, url, HttpLib.setHeaders(httpHeaders), bodyPublisher); + return HttpLib.httpPushWithResponse(httpClient, method, url, HttpLib.setHeaders(httpHeaders), bodyPublisher); } public static void httpDeleteGraph(String url) { diff --git a/jena-arq/src/main/java/org/apache/jena/http/Push.java b/jena-arq/src/main/java/org/apache/jena/http/Push.java index 9fd9cac0842..efad46fe065 100644 --- a/jena-arq/src/main/java/org/apache/jena/http/Push.java +++ b/jena-arq/src/main/java/org/apache/jena/http/Push.java @@ -21,13 +21,13 @@ package org.apache.jena.http; -import static org.apache.jena.riot.web.HttpNames.METHOD_PATCH; -import static org.apache.jena.riot.web.HttpNames.METHOD_POST; -import static org.apache.jena.riot.web.HttpNames.METHOD_PUT; - -/** Enum for HTTP push operations */ +/** + * Enum for HTTP push operation + * @deprecated Use {@link HttpMethod} + */ +@Deprecated(forRemoval = true) public enum Push { - PUT(METHOD_PUT), POST(METHOD_POST), PATCH(METHOD_PATCH); + PUT(HttpMethod.METHOD_PUT), POST(HttpMethod.METHOD_POST), PATCH(HttpMethod.METHOD_PATCH); private final String method; Push(String method) { diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpNames.java b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpNames.java index 3d171521af0..8093410a84d 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpNames.java +++ b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpNames.java @@ -21,94 +21,121 @@ package org.apache.jena.riot.web; +import org.apache.jena.http.HttpMethod; + public class HttpNames { // See also Guava com.google.common.net.HttpHeaders // (org.apache.jena.ext.com.google.common.net.HttpHeaders) - public static final String hAccept = "Accept" ; - public static final String hAcceptCharset = "Accept-Charset" ; - public static final String hAcceptRanges = "Accept-Ranges" ; - public static final String hAcceptEncoding = "Accept-Encoding" ; + public static final String hAccept = "Accept"; + public static final String hAcceptCharset = "Accept-Charset"; + public static final String hAcceptRanges = "Accept-Ranges"; + public static final String hAcceptEncoding = "Accept-Encoding"; - public static final String hAllow = "Allow" ; + public static final String hAllow = "Allow"; public static final String hAuthorization = "Authorization"; public static final String hWWWAuthenticate = "WWW-Authenticate"; - public static final String hContentEncoding = "Content-Encoding" ; - public static final String hContentLength = "Content-Length" ; - public static final String hContentLocation = "Content-Location" ; - public static final String hContentRange = "Content-Range" ; - public static final String hContentType = "Content-Type" ; - public static final String hPragma = "Pragma" ; - public static final String hCacheControl = "Cache-Control" ; - public static final String hRetryAfter = "Retry-After" ; - public static final String hServer = "Server" ; - public static final String hLocation = "Location" ; - public static final String hVary = "Vary" ; - public static final String hUserAgent = "User-Agent" ; + public static final String hContentEncoding = "Content-Encoding"; + public static final String hContentLength = "Content-Length"; + public static final String hContentLocation = "Content-Location"; + public static final String hContentRange = "Content-Range"; + public static final String hContentType = "Content-Type"; + public static final String hPragma = "Pragma"; + public static final String hCacheControl = "Cache-Control"; + public static final String hRetryAfter = "Retry-After"; + public static final String hServer = "Server"; + public static final String hLocation = "Location"; + public static final String hVary = "Vary"; + public static final String hUserAgent = "User-Agent"; // CORS: // http://www.w3.org/TR/cors/ http://esw.w3.org/CORS_Enabled - public static final String hAccessControlAllowOrigin = "Access-Control-Allow-Origin" ; - public static final String hAccessControlExposeHeaders = "Access-Control-Expose-Headers" ; - public static final String hAccessControlMaxAge = "Access-Control-Max-Age" ; - public static final String hAccessControlAllowCredentials = "Access-Control-Allow-Credentials" ; - public static final String hAccessControlAllowMethods = "Access-Control-Allow-Methods" ; - public static final String hAccessControlAllowHeaders = "Access-Control-Allow-Headers" ; - public static final String hOrigin = "Origin" ; - public static final String hAccessControlRequestMethod = "Access-Control-Request-Method" ; - public static final String hAccessControlRequestHeaders = "Access-Control-Request-Headers" ; + public static final String hOrigin = "Origin"; + public static final String hAccessControlAllowOrigin = "Access-Control-Allow-Origin"; + public static final String hAccessControlExposeHeaders = "Access-Control-Expose-Headers"; + public static final String hAccessControlMaxAge = "Access-Control-Max-Age"; + public static final String hAccessControlAllowCredentials = "Access-Control-Allow-Credentials"; + public static final String hAccessControlAllowMethods = "Access-Control-Allow-Methods"; + public static final String hAccessControlAllowHeaders = "Access-Control-Allow-Headers"; + public static final String hAccessControlRequestMethod = "Access-Control-Request-Method"; + public static final String hAccessControlRequestHeaders = "Access-Control-Request-Headers"; // Media type parameters. // XXX Rename as mtCharset? - public static final String charset = "charset" ; + public static final String charset = "charset"; // RDF .1.2+ SPARQL 1.2+ HTTP header parameters - public static final String mtParamVersion = "version" ; + public static final String mtParamVersion = "version"; // RFC 6906 -- https://www.rfc-editor.org/rfc/rfc6906 - public static final String mtParamProfile = "profile" ; + public static final String mtParamProfile = "profile"; // GSP parameter names - public static final String paramGraph = "graph" ; - public static final String paramGraphDefault = "default" ; + public static final String paramGraph = "graph"; + public static final String paramGraphDefault = "default"; // Special names for GSP targets (use in ?graph=) - public static final String graphTargetDefault = "default" ; - public static final String graphTargetUnion = "union" ; + public static final String graphTargetDefault = "default"; + public static final String graphTargetUnion = "union"; // SPARQL query parameter names - public static final String paramQuery = "query" ; - public static final String paramQueryRef = "query-ref" ; - public static final String paramDefaultGraphURI = "default-graph-uri" ; - public static final String paramNamedGraphURI = "named-graph-uri" ; - public static final String paramTarget = "target" ; + public static final String paramQuery = "query"; + public static final String paramQueryRef = "query-ref"; + public static final String paramDefaultGraphURI = "default-graph-uri"; + public static final String paramNamedGraphURI = "named-graph-uri"; + public static final String paramTarget = "target"; // SPARQL Update parameter names - public static final String paramUpdate = "update" ; - public static final String paramRequest = "request" ; // Alternative name. - public static final String paramUsingGraphURI = "using-graph-uri" ; - public static final String paramUsingNamedGraphURI = "using-named-graph-uri" ; + public static final String paramUpdate = "update"; + public static final String paramRequest = "request"; // Alternative name. + public static final String paramUsingGraphURI = "using-graph-uri"; + public static final String paramUsingNamedGraphURI = "using-named-graph-uri"; // Jena parameter names (SPARQL protocol extensions) - public static final String paramStyleSheet = "stylesheet" ; - public static final String paramLang = "lang" ; - public static final String paramAccept = "accept" ; // Unused - public static final String paramOutput1 = "output" ; - public static final String paramOutput2 = "format" ; // Alternative name - public static final String paramOutput3 = "results" ; // Alternative name - public static final String paramCallback = "callback" ; - public static final String paramForceAccept = "force-accept" ; // Force the accept header at the last moment - public static final String paramTimeout = "timeout" ; - - // Replace by enum HttpMethod - public static final String METHOD_DELETE = "DELETE"; - public static final String METHOD_HEAD = "HEAD"; - public static final String METHOD_GET = "GET"; - public static final String METHOD_QUERY = "QUERY" ; - public static final String METHOD_OPTIONS = "OPTIONS"; - public static final String METHOD_PATCH = "PATCH" ; - public static final String METHOD_POST = "POST"; - public static final String METHOD_PUT = "PUT"; - public static final String METHOD_TRACE = "TRACE"; + public static final String paramStyleSheet = "stylesheet"; + public static final String paramLang = "lang"; + public static final String paramAccept = "accept"; // Unused + public static final String paramOutput1 = "output"; + public static final String paramOutput2 = "format"; // Alternative name + public static final String paramOutput3 = "results"; // Alternative name + public static final String paramCallback = "callback"; + public static final String paramForceAccept = "force-accept"; // Force the accept header at the last moment + public static final String paramTimeout = "timeout"; + + /** @deprecated Use {@link HttpMethod#METHOD_DELETE} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_DELETE = HttpMethod.METHOD_DELETE; + + /** @deprecated Use {@link HttpMethod#METHOD_HEAD} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_HEAD = HttpMethod.METHOD_HEAD; + + /** @deprecated Use {@link HttpMethod#METHOD_GET} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_GET = HttpMethod.METHOD_GET; + + /** @deprecated Use {@link HttpMethod#METHOD_QUERY} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_QUERY = HttpMethod.METHOD_QUERY; + + /** @deprecated Use {@link HttpMethod#METHOD_OPTIONS} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_OPTIONS = HttpMethod.METHOD_OPTIONS; + + /** @deprecated Use {@link HttpMethod#METHOD_PATCH} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_PATCH = HttpMethod.METHOD_PATCH; + + /** @deprecated Use {@link HttpMethod#METHOD_POST} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_POST = HttpMethod.METHOD_POST; + + /** @deprecated Use {@link HttpMethod#METHOD_PUT} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_PUT = HttpMethod.METHOD_PUT; + + /** @deprecated Use {@link HttpMethod#METHOD_TRACE} instead */ + @Deprecated(forRemoval = true) + public static final String METHOD_TRACE = HttpMethod.METHOD_TRACE; public static final String HEADER_IFMODSINCE = "If-Modified-Since"; public static final String HEADER_LASTMOD = "Last-Modified"; diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/DSP.java b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/DSP.java index fbd028e9754..9b4907c6a23 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/DSP.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/DSP.java @@ -26,8 +26,8 @@ import org.apache.jena.atlas.lib.FileOps; import org.apache.jena.http.HttpEnv; +import org.apache.jena.http.HttpMethod; import org.apache.jena.http.HttpRDF; -import org.apache.jena.http.Push; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFFormat; import org.apache.jena.riot.RDFLanguages; @@ -103,7 +103,7 @@ public void POST(String file) { throw new IllegalArgumentException("No such file: "+file); String fileExtContentType = contentTypeFromFilename(file); HttpClient hc = requestHttpClient(serviceEndpoint, serviceEndpoint); - uploadQuads(hc, serviceEndpoint, file, fileExtContentType, httpHeaders, Push.POST); + uploadQuads(hc, serviceEndpoint, file, fileExtContentType, httpHeaders, HttpMethod.POST); } /** POST a dataset */ @@ -124,7 +124,7 @@ public void PUT(String file) { throw new IllegalArgumentException("No such file: "+file); String fileExtContentType = contentTypeFromFilename(file); HttpClient hc = requestHttpClient(serviceEndpoint, serviceEndpoint); - uploadQuads(hc, serviceEndpoint, file, fileExtContentType, httpHeaders, Push.PUT); + uploadQuads(hc, serviceEndpoint, file, fileExtContentType, httpHeaders, HttpMethod.PUT); } /** PUT a dataset */ @@ -145,10 +145,10 @@ public void clear() { * Send a file of quads to a URL. The Content-Type is inferred from the file * extension. */ - private static void uploadQuads(HttpClient httpClient, String endpoint, String file, String fileExtContentType, Map headers, Push mode) { + private static void uploadQuads(HttpClient httpClient, String endpoint, String file, String fileExtContentType, Map headers, HttpMethod method) { Lang lang = RDFLanguages.contentTypeToLang(fileExtContentType); if ( !RDFLanguages.isQuads(lang) && !RDFLanguages.isTriples(lang) ) throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")"); - pushFile(httpClient, endpoint, file, fileExtContentType, headers, mode); + pushFile(httpClient, endpoint, file, fileExtContentType, headers, method); } } diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/GSP.java b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/GSP.java index 22a10b96edd..34503a7c3de 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/GSP.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/GSP.java @@ -29,8 +29,8 @@ import org.apache.jena.graph.Node; import org.apache.jena.http.HttpEnv; import org.apache.jena.http.HttpLib; +import org.apache.jena.http.HttpMethod; import org.apache.jena.http.HttpRDF; -import org.apache.jena.http.Push; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFFormat; import org.apache.jena.riot.RDFLanguages; @@ -184,7 +184,7 @@ public void POST(String file) { String url = graphRequestURL(); String fileExtContentType = contentTypeFromFilename(file); HttpClient hc = requestHttpClient(serviceEndpoint, url); - uploadTriples(hc, url, file, fileExtContentType, httpHeaders, Push.POST); + uploadTriples(hc, url, file, fileExtContentType, httpHeaders, HttpMethod.POST); } // /** @@ -234,7 +234,7 @@ public void PUT(String file) { String url = graphRequestURL(); String fileExtContentType = contentTypeFromFilename(file); HttpClient hc = requestHttpClient(serviceEndpoint, url); - uploadTriples(hc, url, file, fileExtContentType, httpHeaders, Push.PUT); + uploadTriples(hc, url, file, fileExtContentType, httpHeaders, HttpMethod.PUT); } // /** @@ -330,7 +330,7 @@ private String graphRequestURL() { /** Send a file of triples to a URL. */ private static void uploadTriples(HttpClient httpClient, String gspUrl, String file, String fileExtContentType, - Map headers, Push mode) { + Map headers, HttpMethod method) { Lang lang = RDFLanguages.contentTypeToLang(fileExtContentType); if ( lang == null ) throw new ARQException("Not a recognized as an RDF format: "+fileExtContentType); @@ -338,6 +338,6 @@ private static void uploadTriples(HttpClient httpClient, String gspUrl, String f throw new ARQException("Can't load quads into a graph"); if ( ! RDFLanguages.isTriples(lang) ) throw new ARQException("Not an RDF format: "+file+" (lang="+lang+")"); - pushFile(httpClient, gspUrl, file, fileExtContentType, headers, mode); + pushFile(httpClient, gspUrl, file, fileExtContentType, headers, method); } } diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/StoreProtocol.java b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/StoreProtocol.java index ed46fd6c65d..835b296d27e 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/StoreProtocol.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/StoreProtocol.java @@ -35,7 +35,7 @@ import org.apache.jena.atlas.web.HttpException; import org.apache.jena.http.HttpEnv; import org.apache.jena.http.HttpLib; -import org.apache.jena.http.Push; +import org.apache.jena.http.HttpMethod; import org.apache.jena.riot.*; import org.apache.jena.riot.system.StreamRDFWriter; import org.apache.jena.riot.web.HttpNames; @@ -236,13 +236,13 @@ protected String contentTypeFromFilename(String filename) { /** Send a file. fileContentType takes precedence over this.contentType.*/ protected static void pushFile(HttpClient httpClient, String endpoint, String file, String fileContentType, - Map httpHeaders, Push style) { + Map httpHeaders, HttpMethod method) { try { Path path = Path.of(file); if ( fileContentType != null ) httpHeaders.put(HttpNames.hContentType, fileContentType); BodyPublisher body = BodyPublishers.ofFile(path); - HttpLib.httpPushData(httpClient, style, endpoint, HttpLib.setHeaders(httpHeaders), body); + HttpLib.httpPushData(httpClient, method, endpoint, HttpLib.setHeaders(httpHeaders), body); } catch (FileNotFoundException ex) { throw new NotFoundException(file); } diff --git a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AccessCtl_AllowGET.java b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AccessCtl_AllowGET.java index 4b6cac2ec2d..1edef1ffd0d 100644 --- a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AccessCtl_AllowGET.java +++ b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AccessCtl_AllowGET.java @@ -25,6 +25,7 @@ import org.apache.jena.fuseki.servlets.ActionService; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; +import org.apache.jena.http.HttpMethod; /** * Wrapper for an {@link ActionService} that rejects a request with a "write" HTTP @@ -50,7 +51,7 @@ public void validate(HttpAction action) { public void execute(HttpAction action) { other.execute(action); } - + // Allow @Override public void execHead(HttpAction action) { @@ -62,17 +63,14 @@ public void execHead(HttpAction action) { public void execGet(HttpAction action) { executeLifecycle(action); } - + // Deny all others. @Override - public void execAny(String methodName, HttpAction action) { + public void execAny(HttpMethod method, HttpAction action) { if ( label == null ) ServletOps.errorBadRequest("Not supported"); else ServletOps.errorBadRequest(label+" : not supported"); throw new InternalErrorException("AccessCtl_AllowGET: "+ "didn't reject request"); } - - - } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionContainerItem.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionContainerItem.java index 5280a62c79c..e9e6d84b2ba 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionContainerItem.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionContainerItem.java @@ -21,14 +21,11 @@ package org.apache.jena.fuseki.ctl; -import static org.apache.jena.riot.web.HttpNames.METHOD_DELETE; -import static org.apache.jena.riot.web.HttpNames.METHOD_GET; -import static org.apache.jena.riot.web.HttpNames.METHOD_POST; - import org.apache.jena.atlas.json.JsonValue; import org.apache.jena.fuseki.servlets.ActionLib; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; +import org.apache.jena.http.HttpMethod; /** Base for actions that are container and also have actions on items */ public abstract class ActionContainerItem extends ActionCtl { @@ -39,11 +36,11 @@ public abstract class ActionContainerItem extends ActionCtl { final public void execute(HttpAction action) { String method = action.getRequestMethod(); - if ( method.equals(METHOD_GET) ) + if ( method.equals(HttpMethod.METHOD_GET) ) performGet(action); - else if ( method.equals(METHOD_POST) ) + else if ( method.equals(HttpMethod.METHOD_POST) ) performPost(action); - else if ( method.equals(METHOD_DELETE) ) + else if ( method.equals(HttpMethod.METHOD_DELETE) ) performDelete(action); else ServletOps.errorMethodNotAllowed(action.getRequestMethod()); @@ -103,11 +100,11 @@ protected void performDelete(HttpAction action) { /** DELETE request on an item in the container */ protected void execDeleteContainer(HttpAction action) { - ServletOps.errorMethodNotAllowed(METHOD_DELETE, "DELETE applied to a container"); + ServletOps.errorMethodNotAllowed(HttpMethod.METHOD_DELETE, "DELETE applied to a container"); } /** DELETE request on an item in the container */ protected void execDeleteItem(HttpAction action) { - ServletOps.errorMethodNotAllowed(METHOD_DELETE); + ServletOps.errorMethodNotAllowed(HttpMethod.METHOD_DELETE); } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionStatsText.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionStatsText.java index 097cb074c4d..b4d15c4890f 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionStatsText.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionStatsText.java @@ -31,8 +31,8 @@ import org.apache.jena.fuseki.server.*; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; +import org.apache.jena.http.HttpMethod; import org.apache.jena.riot.WebContent; -import org.apache.jena.riot.web.HttpNames; // Unused - left in case it should be resurrected. public class ActionStatsText extends ActionCtl @@ -42,8 +42,8 @@ public class ActionStatsText extends ActionCtl @Override public void validate(HttpAction action) { switch(action.getRequestMethod() ) { - case HttpNames.METHOD_GET: - case HttpNames.METHOD_POST: + case HttpMethod.METHOD_GET: + case HttpMethod.METHOD_POST: return; default: ServletOps.errorMethodNotAllowed(action.getRequestMethod()); diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionTasks.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionTasks.java index 1b8046132a3..92d1f810134 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionTasks.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/ctl/ActionTasks.java @@ -21,8 +21,6 @@ package org.apache.jena.fuseki.ctl; import static java.lang.String.format; -import static org.apache.jena.riot.web.HttpNames.METHOD_GET; -import static org.apache.jena.riot.web.HttpNames.METHOD_POST; import org.apache.jena.atlas.json.JsonBuilder; import org.apache.jena.atlas.json.JsonValue; @@ -31,6 +29,7 @@ import org.apache.jena.fuseki.servlets.ActionLib; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; +import org.apache.jena.http.HttpMethod; public class ActionTasks extends ActionCtl { @@ -60,9 +59,9 @@ public void execute(HttpAction action) { } String method = action.getRequestMethod(); - if ( method.equals(METHOD_GET) ) + if ( method.equals(HttpMethod.METHOD_GET) ) execGet(action, name); - else if ( method.equals(METHOD_POST) ) + else if ( method.equals(HttpMethod.METHOD_POST) ) execPost(action, name); else ServletOps.errorMethodNotAllowed(action.getRequestMethod()); diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/Dispatcher.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/Dispatcher.java index f41492e9c90..c07946f0ea5 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/Dispatcher.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/Dispatcher.java @@ -40,6 +40,7 @@ import org.apache.jena.fuseki.auth.Auth; import org.apache.jena.fuseki.servlets.*; import org.apache.jena.fuseki.system.ActionCategory; +import org.apache.jena.http.HttpMethod; import org.apache.jena.riot.web.HttpNames; import org.apache.jena.web.HttpSC; import org.slf4j.Logger; @@ -438,7 +439,7 @@ private static Operation mapGSP(HttpAction action, Operation operation, Endpoint } else if ( GSP_RW.equals(operation) ) { // If asking for GSP_RW, but only GSP_R is available ... // ... if OPTIONS, use GSP_R. - if ( action.getRequestMethod().equals(HttpNames.METHOD_OPTIONS) && epSet.contains(GSP_R) ) + if ( action.getRequestMethod().equals(HttpMethod.METHOD_OPTIONS) && epSet.contains(GSP_R) ) return GSP_R; // ... else 405 if ( epSet.contains(GSP_R) ) @@ -569,8 +570,8 @@ private static Operation gspOperation(HttpAction action, HttpServletRequest requ private static boolean isReadMethod(HttpServletRequest request) { String method = request.getMethod(); // REST dataset. - boolean isGET = method.equals(HttpNames.METHOD_GET); - boolean isHEAD = method.equals(HttpNames.METHOD_HEAD); + boolean isGET = method.equals(HttpMethod.METHOD_GET); + boolean isHEAD = method.equals(HttpMethod.METHOD_HEAD); return isGET || isHEAD; } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionExecLib.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionExecLib.java index 217ef94ed87..d5eba960293 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionExecLib.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionExecLib.java @@ -35,6 +35,7 @@ import org.apache.jena.fuseki.Fuseki; import org.apache.jena.fuseki.server.*; import org.apache.jena.fuseki.system.ActionCategory; +import org.apache.jena.http.HttpMethod; import org.apache.jena.query.QueryCancelledException; import org.apache.jena.query.QueryDeniedException; import org.apache.jena.riot.web.HttpNames; @@ -333,7 +334,7 @@ public static void initResponse(HttpAction action) { ActionLib.setCommonHeaders(action); String method = action.getRequestMethod(); // All GET and HEAD operations are sensitive to conneg so ... - if ( HttpNames.METHOD_GET.equalsIgnoreCase(method) || HttpNames.METHOD_HEAD.equalsIgnoreCase(method) ) + if ( HttpMethod.METHOD_GET.equalsIgnoreCase(method) || HttpMethod.METHOD_HEAD.equalsIgnoreCase(method) ) ServletBase.setVaryHeader(action.getResponse()); } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionLib.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionLib.java index 884c50de4ca..2b16b796525 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionLib.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionLib.java @@ -23,7 +23,6 @@ import static java.lang.String.format; import static org.apache.jena.atlas.lib.Lib.equalsOrNulls; -import static org.apache.jena.riot.web.HttpNames.METHOD_POST; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -49,6 +48,7 @@ import org.apache.jena.fuseki.system.ConNeg; import org.apache.jena.fuseki.system.FusekiNetLib; import org.apache.jena.graph.Graph; +import org.apache.jena.http.HttpMethod; import org.apache.jena.riot.*; import org.apache.jena.riot.system.*; import org.apache.jena.riot.web.HttpNames; @@ -398,7 +398,7 @@ public static ContentType getContentType(HttpAction action) { public static boolean isHTMLForm(HttpAction action) { String method = action.getRequestMethod(); - if ( ! method.equals(METHOD_POST) ) + if ( ! method.equals(HttpMethod.METHOD_POST) ) return false; String ct = getContentMediaType(action); @@ -466,6 +466,7 @@ private static void setCommonHeaders(HttpServletResponse httpResponse) { } // Packing of OPTIONS. + // Query (SPARQLQueryProcessor) and update (SPARQL_Update) do special handling to allow add QUERY and PATCH. public static void doOptionsGet(HttpAction action) { setCommonHeadersForOptions(action); diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java index 728241fcac5..56e5d287205 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java @@ -21,7 +21,7 @@ package org.apache.jena.fuseki.servlets; -import static org.apache.jena.riot.web.HttpNames.*; +import org.apache.jena.http.HttpMethod; /** Interface for executing {@link HttpAction}. */ public interface ActionProcessor { @@ -33,33 +33,37 @@ public interface ActionProcessor { */ public default void process(HttpAction action) { switch ( action.getRequestMethod() ) { - case METHOD_GET -> execGet(action); - case METHOD_QUERY -> execQuery(action); - case METHOD_POST -> execPost(action); - case METHOD_PATCH -> execPatch(action); - case METHOD_PUT -> execPut(action); - case METHOD_DELETE -> execDelete(action); - case METHOD_HEAD -> execHead(action); - case METHOD_OPTIONS -> execOptions(action); - case METHOD_TRACE -> execTrace(action); - default -> execAny(action.getRequestMethod(), action); + case HttpMethod.METHOD_GET -> execGet(action); + case HttpMethod.METHOD_QUERY -> execQuery(action); + case HttpMethod.METHOD_POST -> execPost(action); + case HttpMethod.METHOD_PATCH -> execPatch(action); + case HttpMethod.METHOD_PUT -> execPut(action); + case HttpMethod.METHOD_DELETE -> execDelete(action); + case HttpMethod.METHOD_HEAD -> execHead(action); + case HttpMethod.METHOD_OPTIONS -> execOptions(action); + case HttpMethod.METHOD_TRACE -> execTrace(action); + default -> execUnknown(action.getRequestMethod(), action); } } // Override to support the operation. // A common override is "executeLifecycle(action);" - public default void execHead(HttpAction action) { execAny(METHOD_HEAD, action); } - public default void execGet(HttpAction action) { execAny(METHOD_GET, action); } - public default void execQuery(HttpAction action) { execAny(METHOD_QUERY, action); } - public default void execPost(HttpAction action) { execAny(METHOD_POST, action); } - public default void execPatch(HttpAction action) { execAny(METHOD_PATCH, action); } - public default void execPut(HttpAction action) { execAny(METHOD_PUT, action); } - public default void execDelete(HttpAction action) { execAny(METHOD_DELETE, action); } - public default void execOptions(HttpAction action) { execAny(METHOD_OPTIONS, action); } - public default void execTrace(HttpAction action) { execAny(METHOD_TRACE, action); } + public default void execHead(HttpAction action) { execAny(HttpMethod.HEAD, action); } + public default void execGet(HttpAction action) { execAny(HttpMethod.GET, action); } + public default void execQuery(HttpAction action) { execAny(HttpMethod.QUERY, action); } + public default void execPost(HttpAction action) { execAny(HttpMethod.POST, action); } + public default void execPatch(HttpAction action) { execAny(HttpMethod.PATCH, action); } + public default void execPut(HttpAction action) { execAny(HttpMethod.PUT, action); } + public default void execDelete(HttpAction action) { execAny(HttpMethod.DELETE, action); } + public default void execOptions(HttpAction action) { execAny(HttpMethod.OPTIONS, action); } + public default void execTrace(HttpAction action) { execAny(HttpMethod.TRACE, action); } // Override this for all HTTP verbs. - public default void execAny(String methodName, HttpAction action) { - ServletOps.errorMethodNotAllowed(methodName); + public default void execAny(HttpMethod method, HttpAction action) { + ServletOps.errorMethodNotAllowed(method.method()); + } + + public default void execUnknown(String unknownMethod, HttpAction action) { + ServletOps.errorMethodNotAllowed(unknownMethod); } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java index e26e47d0543..576df7721b6 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java @@ -23,9 +23,9 @@ import static org.apache.jena.atlas.lib.Lib.uppercase; import static org.apache.jena.fuseki.servlets.ActionExecLib.incCounter; -import static org.apache.jena.riot.web.HttpNames.*; import org.apache.jena.fuseki.server.CounterName; +import org.apache.jena.http.HttpMethod; import org.apache.jena.sparql.core.DatasetGraph; /** Common point for operations that are "REST"ish (use GET/PUT etc as operations). */ @@ -40,14 +40,14 @@ public void execute(HttpAction action) { // Intercept to put counters around calls. String method = uppercase(action.getRequestMethod()); switch(method) { - case METHOD_GET -> doGet$(action); - case METHOD_HEAD -> doHead$(action); - case METHOD_POST -> doPost$(action); - case METHOD_PATCH -> doPatch$(action); - case METHOD_PUT -> doPut$(action); - case METHOD_DELETE -> doDelete$(action); - case METHOD_OPTIONS -> doOptions$(action); - case METHOD_TRACE -> doTrace$(action); + case HttpMethod.METHOD_GET -> doGet$(action); + case HttpMethod.METHOD_HEAD -> doHead$(action); + case HttpMethod.METHOD_POST -> doPost$(action); + case HttpMethod.METHOD_PATCH -> doPatch$(action); + case HttpMethod.METHOD_PUT -> doPut$(action); + case HttpMethod.METHOD_DELETE -> doDelete$(action); + case HttpMethod.METHOD_OPTIONS -> doOptions$(action); + case HttpMethod.METHOD_TRACE -> doTrace$(action); default -> ServletOps.errorNotImplemented("Unknown method: "+method); } } @@ -152,7 +152,6 @@ protected DatasetGraph decideDataset(HttpAction action) { protected abstract void doOptions(HttpAction action); // If not final in ActionBase - @Override public void process(HttpAction action) { executeLifecycle(action); } - - @Override public void execAny(String methodName, HttpAction action) { executeLifecycle(action); } + @Override public void process(HttpAction action) { executeLifecycle(action); } + @Override public void execAny(HttpMethod method, HttpAction action) { executeLifecycle(action); } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/NoOpActionService.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/NoOpActionService.java index f306d5521a0..63375f208d1 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/NoOpActionService.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/NoOpActionService.java @@ -21,9 +21,11 @@ package org.apache.jena.fuseki.servlets; -/** An {@link ActionService} that denies service. +import org.apache.jena.http.HttpMethod; + +/** An {@link ActionService} that denies service. * Used to turn things off. - */ + */ public class NoOpActionService extends ActionService { @Override @@ -35,6 +37,9 @@ public void validate(HttpAction action) { public void execute(HttpAction action) { ServletOps.errorBadRequest(action.getActionURI()); } - - @Override public void execAny(String methodName, HttpAction action) { executeLifecycle(action); } + + @Override + public void execAny(HttpMethod method, HttpAction action) { + executeLifecycle(action); + } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/PatchApply.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/PatchApply.java index b249ba6fb0e..ef840bd21c2 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/PatchApply.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/PatchApply.java @@ -29,6 +29,7 @@ import org.apache.jena.atlas.web.ContentType; import org.apache.jena.fuseki.server.CounterName; +import org.apache.jena.http.HttpMethod; import org.apache.jena.rdfpatch.PatchException; import org.apache.jena.rdfpatch.RDFChanges; import org.apache.jena.rdfpatch.changes.*; @@ -57,8 +58,8 @@ public PatchApply() { public void validate(HttpAction action) { String method = action.getRequest().getMethod(); switch(method) { - case HttpNames.METHOD_POST: - case HttpNames.METHOD_PATCH: + case HttpMethod.METHOD_POST: + case HttpMethod.METHOD_PATCH: break; default: ServletOps.errorMethodNotAllowed(method+" : Patch must use POST or PATCH"); diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQLQueryProcessor.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQLQueryProcessor.java index 7ba6662a1f1..1cf1606a03b 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQLQueryProcessor.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQLQueryProcessor.java @@ -44,6 +44,7 @@ import org.apache.jena.fuseki.Fuseki; import org.apache.jena.fuseki.system.FusekiNetLib; import org.apache.jena.graph.Graph; +import org.apache.jena.http.HttpMethod; import org.apache.jena.query.*; import org.apache.jena.riot.web.HttpNames; import org.apache.jena.sparql.core.DatasetGraph; @@ -72,7 +73,8 @@ public SPARQLQueryProcessor() { } @Override public void execOptions(HttpAction action) { - ActionLib.doOptionsGetPost(action); + ActionLib.setCommonHeadersForOptions(action); + action.setResponseHeader(HttpNames.hAllow, "HEAD,GET,QUERY,POST,OPTIONS"); ServletOps.success(action); } @@ -87,6 +89,10 @@ public void execOptions(HttpAction action) { executeLifecycle(action); } + @Override public void execQuery(HttpAction action) { + executeLifecycle(action); + } + /** * All the query parameters that are acceptable in a given request. * This is comprised of, by default, @@ -116,13 +122,17 @@ protected Collection acceptedParams(HttpAction action) { public void validate(HttpAction action) { String method = uppercase(action.getRequestMethod()); - if ( HttpNames.METHOD_OPTIONS.equals(method) ) + if ( HttpMethod.METHOD_OPTIONS.equals(method) ) return; - if ( !HttpNames.METHOD_POST.equals(method) && !HttpNames.METHOD_GET.equals(method) ) - ServletOps.errorMethodNotAllowed("Not a GET or POST request"); + switch(method) { + case HttpMethod.METHOD_POST, HttpMethod.METHOD_GET,HttpMethod.METHOD_QUERY -> {} + default -> { + ServletOps.errorMethodNotAllowed("Not a GET, POST or QUERY request"); + } + } - if ( HttpNames.METHOD_GET.equals(method) && action.getRequestQueryString() == null ) { + if ( HttpMethod.METHOD_GET.equals(method) && action.getRequestQueryString() == null ) { ServletOps.warning(action, "Service Description / SPARQL Query / " + action.getRequestRequestURI()); ServletOps.errorNotFound("Service Description: " + action.getRequestRequestURI()); } @@ -154,8 +164,6 @@ protected void validateParams(HttpAction action, Collection params) { ContentType ct = FusekiNetLib.getContentType(request); boolean mustHaveQueryParam = true; if ( ct != null ) { - String incoming = ct.getContentTypeStr(); - if ( matchContentType(ctSPARQLQuery, ct) ) { mustHaveQueryParam = false; // Drop through. @@ -163,7 +171,7 @@ protected void validateParams(HttpAction action, Collection params) { // Nothing specific to do } else - ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unsupported: " + incoming); + ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unsupported: " + ct); } // GET/POST of a form at this point. @@ -198,7 +206,7 @@ protected void validateParams(HttpAction action, Collection params) { @Override public final void execute(HttpAction action) { // GET - if ( action.getRequestMethod().equals(HttpNames.METHOD_GET) ) { + if ( action.getRequestMethod().equals(HttpMethod.METHOD_GET) ) { executeWithParameter(action); return; } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java index 59b5ae52134..1f316c0eaff 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java @@ -49,6 +49,7 @@ import org.apache.jena.fuseki.Fuseki; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; +import org.apache.jena.http.HttpMethod; import org.apache.jena.irix.IRIx; import org.apache.jena.irix.IRIxResolver; import org.apache.jena.query.QueryException; @@ -79,13 +80,13 @@ public class SPARQL_Update extends ActionService @Override public void execOptions(HttpAction action) { ActionLib.setCommonHeadersForOptions(action); - action.setResponseHeader(HttpNames.hAllow, "POST,PATCH,OPTIONS"); + action.setResponseHeader(HttpNames.hAllow, "HEAD,POST,PATCH,OPTIONS"); ServletOps.success(action); } @Override public void execGet(HttpAction action) { - ServletOps.errorMethodNotAllowed(HttpNames.METHOD_GET, "SPARQL Update is not supported with GET. Use POST or PATCH instead"); + ServletOps.errorMethodNotAllowed(HttpMethod.METHOD_GET, "SPARQL Update is not supported with GET. Use POST or PATCH instead"); } @Override @@ -121,10 +122,10 @@ public void execute(HttpAction action) { public void validate(HttpAction action) { //HttpServletRequest request = action.getRequest(); - if ( HttpNames.METHOD_OPTIONS.equals(action.getRequestMethod()) ) + if ( HttpMethod.METHOD_OPTIONS.equals(action.getRequestMethod()) ) return; - if ( ! HttpNames.METHOD_POST.equalsIgnoreCase(action.getRequestMethod()) && ! HttpNames.METHOD_PATCH.equalsIgnoreCase(action.getRequestMethod()) ) + if ( ! HttpMethod.METHOD_POST.equalsIgnoreCase(action.getRequestMethod()) && ! HttpMethod.METHOD_PATCH.equalsIgnoreCase(action.getRequestMethod()) ) ServletOps.errorMethodNotAllowed("SPARQL Update : use POST or PATCH"); ContentType ct = updateContentType(action); diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/prefixes/ActionPrefixesBase.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/prefixes/ActionPrefixesBase.java index 267aba8f011..1aa3d959e87 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/prefixes/ActionPrefixesBase.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/prefixes/ActionPrefixesBase.java @@ -26,7 +26,7 @@ import org.apache.jena.fuseki.servlets.BaseActionREST; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; -import org.apache.jena.riot.web.HttpNames; +import org.apache.jena.http.HttpMethod; import org.apache.jena.sparql.core.DatasetGraph; import java.util.Iterator; @@ -84,9 +84,9 @@ public final void validate(HttpAction action) { // Per HTTP operation validation. switch ( action.getRequestMethod() ) { - case HttpNames.METHOD_GET -> validatePrefixesGET(action); - case HttpNames.METHOD_POST -> validatePrefixesPOST(action); - case HttpNames.METHOD_DELETE -> validatePrefixesDELETE(action); + case HttpMethod.METHOD_GET -> validatePrefixesGET(action); + case HttpMethod.METHOD_POST -> validatePrefixesPOST(action); + case HttpMethod.METHOD_DELETE -> validatePrefixesDELETE(action); } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBase.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBase.java index 2c095e2d5da..8a67229a29b 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBase.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBase.java @@ -45,7 +45,7 @@ import org.apache.jena.fuseki.servlets.ServletOps; import org.apache.jena.fuseki.system.ConNeg; import org.apache.jena.fuseki.validation.json.ValidationAction; -import org.apache.jena.riot.web.HttpNames; +import org.apache.jena.http.HttpMethod; import org.apache.jena.web.HttpSC; import org.slf4j.Logger; @@ -107,7 +107,7 @@ static void initResponse(HttpServletRequest request, HttpServletResponse respons setCommonHeaders(response); String method = request.getMethod(); // All GET and HEAD operations are sensitive to conneg so ... - if ( HttpNames.METHOD_GET.equalsIgnoreCase(method) || HttpNames.METHOD_HEAD.equalsIgnoreCase(method) ) + if ( HttpMethod.METHOD_GET.equalsIgnoreCase(method) || HttpMethod.METHOD_HEAD.equalsIgnoreCase(method) ) setVaryHeader(response); } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java index 6c14b008275..9caaa4a0f71 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java @@ -32,12 +32,12 @@ import org.apache.jena.atlas.json.JSON; import org.apache.jena.atlas.json.JsonObject; import org.apache.jena.fuseki.Fuseki; -import org.apache.jena.riot.web.HttpNames; import org.apache.jena.fuseki.servlets.ActionErrorException; import org.apache.jena.fuseki.servlets.ActionLib; import org.apache.jena.fuseki.servlets.ServletBase; import org.apache.jena.fuseki.servlets.ServletOps; import org.apache.jena.fuseki.validation.json.ValidationAction; +import org.apache.jena.http.HttpMethod; import static org.apache.jena.riot.WebContent.*; import org.apache.jena.web.HttpSC; @@ -96,7 +96,7 @@ static void initResponse(HttpServletRequest request, HttpServletResponse respons setCommonHeaders(response); String method = request.getMethod(); // All GET and HEAD operations are sensitive to conneg so ... - if ( HttpNames.METHOD_GET.equalsIgnoreCase(method) || HttpNames.METHOD_HEAD.equalsIgnoreCase(method) ) + if ( HttpMethod.METHOD_GET.equalsIgnoreCase(method) || HttpMethod.METHOD_HEAD.equalsIgnoreCase(method) ) setVaryHeader(response); } diff --git a/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/mgt/ActionReload.java b/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/mgt/ActionReload.java index d668a5eae16..cb7b0592976 100644 --- a/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/mgt/ActionReload.java +++ b/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/mgt/ActionReload.java @@ -28,9 +28,9 @@ import org.apache.jena.fuseki.main.FusekiServer; import org.apache.jena.fuseki.servlets.HttpAction; import org.apache.jena.fuseki.servlets.ServletOps; +import org.apache.jena.http.HttpMethod; import org.apache.jena.rdf.model.Model; import org.apache.jena.riot.RDFParser; -import org.apache.jena.riot.web.HttpNames; /** * Administration action to reload the server's dataset configuration. @@ -43,7 +43,7 @@ public class ActionReload extends ActionCtl { @Override public void validate(HttpAction action) { - if ( action.getRequestMethod() != HttpNames.METHOD_POST ) { + if ( action.getRequestMethod() != HttpMethod.METHOD_POST ) { ServletOps.errorMethodNotAllowed(action.getRequestMethod()); } } diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestCrossOriginFilter.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestCrossOriginFilter.java index 7e7bbdcdf47..aaf29bde8e7 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestCrossOriginFilter.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestCrossOriginFilter.java @@ -22,7 +22,6 @@ import static org.apache.jena.fuseki.servlets.CrossOriginFilter.*; import static org.apache.jena.http.HttpLib.handleResponseNoBody; -import static org.apache.jena.riot.web.HttpNames.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -50,6 +49,7 @@ import org.apache.jena.atlas.web.WebLib; import org.apache.jena.fuseki.system.FusekiLogging; import org.apache.jena.http.HttpLib; +import org.apache.jena.http.HttpMethod; import org.apache.jena.riot.web.HttpNames; /** @@ -110,7 +110,7 @@ public void test_corsWithOrigin() { assertSetEquals(allowHeaders, expectedHeaders); Set allowMethods = getHeaderSet(response, ACCESS_CONTROL_ALLOW_METHODS_HEADER); - Set expectedMethods = Set.of(METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD, METHOD_PATCH, METHOD_OPTIONS); + Set expectedMethods = Set.of(HttpMethod.METHOD_GET, HttpMethod.METHOD_POST, HttpMethod.METHOD_PUT, HttpMethod.METHOD_DELETE, HttpMethod.METHOD_HEAD, HttpMethod.METHOD_PATCH, HttpMethod.METHOD_OPTIONS); assertSetEquals(allowMethods, expectedMethods); String allowOriginHeader = getHeader(response, ACCESS_CONTROL_ALLOW_ORIGIN_HEADER); @@ -284,7 +284,7 @@ private static HttpResponse httpOptions(String URL, String...header HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(URL)) - .method(METHOD_OPTIONS, BodyPublishers.noBody()) + .method(HttpMethod.METHOD_OPTIONS, BodyPublishers.noBody()) .headers(headers) .build(); try { diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestHttpOptions.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestHttpOptions.java index 30838fe79cb..dd2d1189570 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestHttpOptions.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestHttpOptions.java @@ -30,26 +30,26 @@ public class TestHttpOptions extends AbstractFusekiTest @Test public void options_query() { String v = HttpOp.httpOptions(serviceQuery()); - FusekiTest.assertStringList(v, "GET", "OPTIONS", "POST"); + FusekiTest.assertStringList(v, "GET", "QUERY", "OPTIONS", "POST", "HEAD"); } @Test public void options_update() { String v = HttpOp.httpOptions(serviceUpdate()); - FusekiTest.assertStringList(v, "OPTIONS", "POST", "PATCH"); + FusekiTest.assertStringList(v, "OPTIONS", "POST", "PATCH", "HEAD"); } @Test public void options_dataset_01() { String v = HttpOp.httpOptions(databaseURL()); // Not DELETE - FusekiTest.assertStringList(v, "HEAD", "GET", "OPTIONS", "POST", "PUT"); + FusekiTest.assertStringList(v, "GET", "OPTIONS", "POST", "PUT", "HEAD"); } @Test public void options_dataset_02() { String v = HttpOp.httpOptions(serviceGSP()); - FusekiTest.assertStringList(v, "GET", "OPTIONS", "HEAD", "POST", "PUT"); + FusekiTest.assertStringList(v, "GET", "OPTIONS", "POST", "PUT", "HEAD"); } @Test diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestSPARQLProtocol.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestSPARQLProtocol.java index d06b6b7ebd6..02fb2bf46ab 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestSPARQLProtocol.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestSPARQLProtocol.java @@ -21,17 +21,25 @@ package org.apache.jena.fuseki.main; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.net.http.HttpRequest.BodyPublisher; +import java.net.http.HttpRequest.BodyPublishers; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.apache.jena.atlas.web.TypedInputStream; import org.apache.jena.graph.Graph; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; +import org.apache.jena.http.HttpOp; import org.apache.jena.query.Query; import org.apache.jena.query.QueryFactory; import org.apache.jena.riot.WebContent; +import org.apache.jena.riot.resultset.ResultSetLang; +import org.apache.jena.riot.rowset.RowSetReader; import org.apache.jena.sparql.exec.QueryExec; import org.apache.jena.sparql.exec.RowSet; import org.apache.jena.sparql.exec.RowSetOps; @@ -60,8 +68,8 @@ public void before() { GSP.service(serviceGSP()).graphName(gn1).PUT(graph2); } - static String query(String base, String queryString) { - return base + "?query=" + Convert.encWWWForm(queryString); + static String httpQueryString(String queryString) { + return "?query=" + Convert.encWWWForm(queryString); } @Test @@ -98,4 +106,39 @@ public void update_02() { UpdateExecution proc = UpdateExecutionFactory.createRemoteForm(update, serviceUpdate()); proc.execute(); } + + // Using QUERY and query string in URL + @Test + public void query_method_qs() { + String qs = httpQueryString("SELECT * { ?s ?p ?o }"); + String URL = serviceQuery()+qs; + + TypedInputStream results = HttpOp.httpQueryStream(URL, WebContent.contentTypeResultsJSON); + + assertEquals(results.getContentType(), WebContent.contentTypeResultsJSON); + RowSet rs = RowSetReader.createReader(ResultSetLang.RS_JSON).read(results.getInputStream(), null); + } + + // Using QUERY + body + @Test + public void query_method_body() { + String qs = "SELECT * { ?s ?p ?o }"; + String URL = serviceQuery(); + BodyPublisher bodyPublisher = BodyPublishers.ofString(qs); + + try ( TypedInputStream results = + HttpOp.httpQueryStream(URL, WebContent.contentTypeSPARQLQuery, bodyPublisher, WebContent.contentTypeResultsJSON) ) { + assertEquals(results.getContentType(), WebContent.contentTypeResultsJSON); + RowSet rs = RowSetReader.createReader(ResultSetLang.RS_JSON).read(results.getInputStream(), null); + } + } + + // Using PATCH + body + @Test + public void patch_method_body() { + String update = "INSERT DATA {}"; + String URL = serviceUpdate(); + BodyPublisher bodyPublisher = BodyPublishers.ofString(update); + HttpOp.httpPatch(URL, WebContent.contentTypeSPARQLUpdate, bodyPublisher); + } } diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestUpdate.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestUpdate.java index 88f67f62192..77506b20703 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestUpdate.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestUpdate.java @@ -61,7 +61,6 @@ private FusekiServer server() { } } - @Test public void update1() { FusekiServer server = server(); String serviceURL = server.datasetURL(DS); diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/test/FusekiTest.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/test/FusekiTest.java index 317dfe64d62..d5b8d348870 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/test/FusekiTest.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/test/FusekiTest.java @@ -28,14 +28,14 @@ public class FusekiTest { /** Check whether str is a comma separated list of expected (unordered) */ - public static void assertStringList(String str, String... expected) { - str = str.replace(" ", ""); + public static void assertStringList(String actual, String... expected) { + String str = actual.replace(" ", ""); String[] x = str.split(","); for ( String ex : expected ) { assertTrue(containsStr(ex, x), "Got: "+str+" - Does not contain "+ex); } for ( String s : x ) { - assertTrue(containsStr(s, expected), "Got: "+str+" - Not expected "+s); + assertTrue(containsStr(s, expected), "Got: "+str+" - Did not expect "+s); } }