Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions jena-arq/src/main/java/org/apache/jena/http/HttpLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,19 +695,49 @@ public static <T> CompletableFuture<HttpResponse<T>> 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<HttpRequest.Builder> modifier, BodyPublisher body) {
HttpResponse<InputStream> 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<HttpRequest.Builder> modifier, BodyPublisher body) {
HttpResponse<InputStream> response = httpPushWithResponse(httpClient, style, url, modifier, body);
public static void httpPushData(HttpClient httpClient, HttpMethod method, String url, Consumer<HttpRequest.Builder> modifier, BodyPublisher body) {
HttpResponse<InputStream> 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<InputStream> httpPushWithResponse(HttpClient httpClient, @SuppressWarnings("removal") Push style, String url,
Consumer<HttpRequest.Builder> modifier, BodyPublisher body) {
return httpPushWithResponse(httpClient, style.method(), url, modifier, body);

}
// Worker
public static HttpResponse<InputStream> httpPushWithResponse(HttpClient httpClient, Push style, String url,
public static HttpResponse<InputStream> httpPushWithResponse(HttpClient httpClient, HttpMethod method, String url,
Consumer<HttpRequest.Builder> 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<InputStream> httpPushWithResponse(HttpClient httpClient, String method, String url,
Consumer<HttpRequest.Builder> 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<InputStream> response = execute(httpClient, builder.build());
Expand Down Expand Up @@ -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<InputStream> response = execute(httpClient, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 34 additions & 13 deletions jena-arq/src/main/java/org/apache/jena/http/HttpOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<InputStream> response = HttpLib.execute(httpClient, request);
return HttpLib.handleResponseTypedInputStream(response);
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<InputStream> response = execute(httpClient, request);
String allowValue = HttpLib.responseHeader(response, HttpNames.hAllow);
Expand Down Expand Up @@ -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<InputStream> response = execute(httpClient, request);
Expand Down
18 changes: 9 additions & 9 deletions jena-arq/src/main/java/org/apache/jena/http/HttpRDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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. */
Expand All @@ -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<String, String> httpHeaders) {
BodyPublisher bodyPublisher = graphToHttpBody(graph, HttpEnv.defaultTriplesFormat);
HttpResponse<InputStream> httpResponse = pushWithResponse(httpClient, url, Push.POST, bodyPublisher, format, httpHeaders);
HttpResponse<InputStream> httpResponse = pushWithResponse(httpClient, url, HttpMethod.POST, bodyPublisher, format, httpHeaders);
Graph graphResponse = GraphFactory.createDefaultGraph();
StreamRDF dest = StreamRDFLib.graph(graphResponse);
httpResponseToStreamRDF(url, httpResponse, dest);
Expand All @@ -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<String, String> 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) {
Expand All @@ -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<String, String> 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) {
Expand All @@ -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<String, String> 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<String, String> 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<InputStream> pushWithResponse(HttpClient httpClient, String url, Push style, BodyPublisher bodyPublisher,
private static HttpResponse<InputStream> pushWithResponse(HttpClient httpClient, String url, HttpMethod method, BodyPublisher bodyPublisher,
RDFFormat format, Map<String, String> 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) {
Expand Down
12 changes: 6 additions & 6 deletions jena-arq/src/main/java/org/apache/jena/http/Push.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading