Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-13982 - Web client - body as string and get request URI for response entity #9500

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
* 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
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
* @return HTTP Request Headers Specification builder
*/
HttpRequestHeadersSpec body(String body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the HTTP request endpoint
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
*/
URI getRequestEndpoint();
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,14 +33,18 @@ class StandardHttpResponseEntity implements HttpResponseEntity {

private final InputStream body;

private final URI requestEndpoint;
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved

StandardHttpResponseEntity(
final int statusCode,
final HttpEntityHeaders headers,
final InputStream body
final InputStream body,
final URI requestEndpoint
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
) {
this.statusCode = statusCode;
this.headers = headers;
this.body = body;
this.requestEndpoint = requestEndpoint;
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand All @@ -61,4 +66,9 @@ public InputStream body() {
public void close() throws IOException {
body.close();
}

@Override
public URI getRequestEndpoint() {
return requestEndpoint;
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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");
Expand All @@ -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());
pvillard31 marked this conversation as resolved.
Show resolved Hide resolved
}

private HttpResponse<InputStream> getResponse(final HttpRequest request) {
Expand Down