From e21682dc41dceee4f6057bdc6c19fd8f586fb1f3 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 7 Nov 2024 11:08:21 +0100 Subject: [PATCH 1/3] NIFI-13982 - Web client - body as string and get request URI for response entity --- .../nifi/web/client/api/HttpRequestBodySpec.java | 10 ++++++++++ .../nifi/web/client/api/HttpResponseEntity.java | 8 ++++++++ .../nifi/web/client/StandardHttpResponseEntity.java | 12 +++++++++++- .../nifi/web/client/StandardWebClientService.java | 12 ++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java index 9ea80bab4d20..645e0cff6723 100644 --- a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java +++ b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java @@ -31,4 +31,14 @@ public interface HttpRequestBodySpec extends HttpRequestHeadersSpec { * @return HTTP Request Headers Specification builder */ HttpRequestHeadersSpec body(InputStream inputStream, OptionalLong contentLength); + + /** + * Set Request Body as provided string + * This should be used only when the payload is small. For large amount of data, + * @see HttpRequestBodySpec#body(InputStream, OptionalLong) + * + * @param body String representation of the payload + * @return HTTP Request Headers Specification builder + */ + HttpRequestHeadersSpec body(String body); } diff --git a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java index f0386aece14a..ea8ca3c933e7 100644 --- a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java +++ b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java @@ -18,6 +18,7 @@ import java.io.Closeable; import java.io.InputStream; +import java.net.URI; /** * HTTP Response Entity extends Closeable to handle closing Response Body @@ -43,4 +44,11 @@ public interface HttpResponseEntity extends Closeable { * @return HTTP Response Body stream can be empty */ InputStream body(); + + /** + * Get the HTTP request endpoint that was accessed to generate this HTTP + * + * @return the HTTP request endpoint + */ + URI getRequestEndpoint(); } diff --git a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java index c1e8bd1825b7..f5377967fc42 100644 --- a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java +++ b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URI; /** * Standard implementation of HTTP Response Entity for Standard Web Client Service @@ -32,14 +33,18 @@ class StandardHttpResponseEntity implements HttpResponseEntity { private final InputStream body; + private final URI requestEndpoint; + StandardHttpResponseEntity( final int statusCode, final HttpEntityHeaders headers, - final InputStream body + final InputStream body, + final URI requestEndpoint ) { this.statusCode = statusCode; this.headers = headers; this.body = body; + this.requestEndpoint = requestEndpoint; } @Override @@ -61,4 +66,9 @@ public InputStream body() { public void close() throws IOException { body.close(); } + + @Override + public URI getRequestEndpoint() { + return requestEndpoint; + } } diff --git a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java index 2295e52f5a13..c9609925bbfc 100644 --- a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java +++ b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java @@ -31,7 +31,6 @@ import org.apache.nifi.web.client.ssl.StandardSSLContextProvider; import org.apache.nifi.web.client.ssl.TlsContext; -import javax.net.ssl.SSLContext; import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.IOException; @@ -48,12 +47,15 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Objects; import java.util.Optional; import java.util.OptionalLong; import java.util.concurrent.Flow; +import javax.net.ssl.SSLContext; + /** * Standard implementation of Web Client Service using Java HttpClient */ @@ -317,6 +319,12 @@ public HttpRequestHeadersSpec body(final InputStream body, final OptionalLong co return this; } + @Override + public HttpRequestHeadersSpec body(final String body) { + final byte[] bytes = body.getBytes(StandardCharsets.UTF_8); + return body(new ByteArrayInputStream(bytes), OptionalLong.of(bytes.length)); + } + @Override public HttpRequestBodySpec header(final String headerName, final String headerValue) { Objects.requireNonNull(headerName, "Header Name required"); @@ -338,7 +346,7 @@ public HttpResponseEntity retrieve() { final InputStream responseBody = response.body(); final InputStream body = responseBody == null ? new ByteArrayInputStream(EMPTY_BYTES) : responseBody; - return new StandardHttpResponseEntity(code, headers, body); + return new StandardHttpResponseEntity(code, headers, body, request.uri()); } private HttpResponse getResponse(final HttpRequest request) { From 4999bc079291375cf303027070be23c37591d844 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 7 Nov 2024 14:56:26 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: David Handermann --- .../nifi/web/client/api/HttpRequestBodySpec.java | 4 ++-- .../apache/nifi/web/client/api/HttpResponseEntity.java | 6 +++--- .../nifi/web/client/StandardHttpResponseEntity.java | 10 +++++----- .../nifi/web/client/StandardWebClientService.java | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java index 645e0cff6723..71ed1f0ace9b 100644 --- a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java +++ b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpRequestBodySpec.java @@ -33,11 +33,11 @@ public interface HttpRequestBodySpec extends HttpRequestHeadersSpec { HttpRequestHeadersSpec body(InputStream inputStream, OptionalLong contentLength); /** - * Set Request Body as provided string + * Set Request Body as provided string encoded as UTF-8. * This should be used only when the payload is small. For large amount of data, * @see HttpRequestBodySpec#body(InputStream, OptionalLong) * - * @param body String representation of the payload + * @param body String representation of the payload encoded as UTF-8 * @return HTTP Request Headers Specification builder */ HttpRequestHeadersSpec body(String body); diff --git a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java index ea8ca3c933e7..e4aaada74f89 100644 --- a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java +++ b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java @@ -46,9 +46,9 @@ public interface HttpResponseEntity extends Closeable { InputStream body(); /** - * Get the HTTP request endpoint that was accessed to generate this HTTP + * Get the endpoint URI that was accessed to generate this HTTP response * - * @return the HTTP request endpoint + * @return HTTP URI from which the response was retrieved */ - URI getRequestEndpoint(); + URI getUri(); } diff --git a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java index f5377967fc42..ca2ac4ca3aa9 100644 --- a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java +++ b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpResponseEntity.java @@ -33,18 +33,18 @@ class StandardHttpResponseEntity implements HttpResponseEntity { private final InputStream body; - private final URI requestEndpoint; + private final URI uri; StandardHttpResponseEntity( final int statusCode, final HttpEntityHeaders headers, final InputStream body, - final URI requestEndpoint + final URI uri ) { this.statusCode = statusCode; this.headers = headers; this.body = body; - this.requestEndpoint = requestEndpoint; + this.uri = uri; } @Override @@ -68,7 +68,7 @@ public void close() throws IOException { } @Override - public URI getRequestEndpoint() { - return requestEndpoint; + public URI getUri() { + return uri; } } diff --git a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java index c9609925bbfc..9d2fae6d77e2 100644 --- a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java +++ b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java @@ -346,7 +346,7 @@ public HttpResponseEntity retrieve() { final InputStream responseBody = response.body(); final InputStream body = responseBody == null ? new ByteArrayInputStream(EMPTY_BYTES) : responseBody; - return new StandardHttpResponseEntity(code, headers, body, request.uri()); + return new StandardHttpResponseEntity(code, headers, body, response.uri()); } private HttpResponse getResponse(final HttpRequest request) { From d9b2f16366f7a4c9195f463ca7778bc8e0dfdd11 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 7 Nov 2024 14:57:15 +0100 Subject: [PATCH 3/3] Update nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java --- .../java/org/apache/nifi/web/client/api/HttpResponseEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java index e4aaada74f89..5cc348032e2c 100644 --- a/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java +++ b/nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseEntity.java @@ -50,5 +50,5 @@ public interface HttpResponseEntity extends Closeable { * * @return HTTP URI from which the response was retrieved */ - URI getUri(); + URI getUri(); }