From 3edf511c8134171bef5af3c073dd7b29f28e83a4 Mon Sep 17 00:00:00 2001 From: Jonathan Roques Date: Mon, 6 Jan 2025 16:17:13 +0100 Subject: [PATCH] feat(rest): PR comments fixes --- .../store/DocumentCreationRequest.java | 3 +- .../http/base/ExecutionEnvironment.java | 28 ++++++------------- .../connector/http/base/HttpService.java | 5 ++-- .../client/apache/CustomApacheHttpClient.java | 5 +++- .../HttpCommonResultResponseHandler.java | 15 ++++++---- .../base/document/FileResponseHandler.java | 7 +++-- .../apache/CustomApacheHttpClientTest.java | 4 +-- .../HttpCommonResultResponseHandlerTest.java | 13 ++++----- 8 files changed, 37 insertions(+), 43 deletions(-) diff --git a/connector-sdk/document/src/main/java/io/camunda/document/store/DocumentCreationRequest.java b/connector-sdk/document/src/main/java/io/camunda/document/store/DocumentCreationRequest.java index dda9821953..acdd1445f5 100644 --- a/connector-sdk/document/src/main/java/io/camunda/document/store/DocumentCreationRequest.java +++ b/connector-sdk/document/src/main/java/io/camunda/document/store/DocumentCreationRequest.java @@ -21,7 +21,6 @@ import java.io.InputStream; import java.time.Duration; import java.util.Map; -import java.util.UUID; public record DocumentCreationRequest( InputStream content, @@ -46,7 +45,7 @@ public static class BuilderFinalStep { private String documentId; private String storeId; private String contentType; - private String fileName = UUID.randomUUID().toString(); + private String fileName; private Duration timeToLive; private Map customProperties; diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/ExecutionEnvironment.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/ExecutionEnvironment.java index 6143c02cb9..e893dc2f67 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/ExecutionEnvironment.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/ExecutionEnvironment.java @@ -19,32 +19,25 @@ import io.camunda.connector.api.outbound.OutboundConnectorContext; public sealed interface ExecutionEnvironment - permits ExecutionEnvironment.SaaSCallerSideEnvironment, - ExecutionEnvironment.SaaSCloudFunctionSideEnvironment, - ExecutionEnvironment.SelfManagedEnvironment { - - /** - * Indicates whether the option to store the response as a document was selected in the Element - * Template. - */ - boolean storeResponseSelected(); + permits ExecutionEnvironment.SaaSCluster, + ExecutionEnvironment.SaaSCloudFunction, + ExecutionEnvironment.SelfManaged { /** * The connector is executed in the context of the cloud function. This is where the * HttpCommonRequest will be executed. */ - record SaaSCloudFunctionSideEnvironment(boolean storeResponseSelected) - implements ExecutionEnvironment {} + record SaaSCloudFunction() implements ExecutionEnvironment {} /** * The connector is executed in the context of the caller, i.e. in the C8 Cluster. When executed * here, the initial HttpCommonRequest will be serialized as JSON and passed to the Cloud * Function. */ - record SaaSCallerSideEnvironment(boolean storeResponseSelected, OutboundConnectorContext context) + record SaaSCluster(OutboundConnectorContext context) implements ExecutionEnvironment, StoresDocument {} - record SelfManagedEnvironment(boolean storeResponseSelected, OutboundConnectorContext context) + record SelfManaged(OutboundConnectorContext context) implements ExecutionEnvironment, StoresDocument {} /** @@ -52,21 +45,18 @@ record SelfManagedEnvironment(boolean storeResponseSelected, OutboundConnectorCo * * @param cloudFunctionEnabled whether the connector is executed in the context of a cloud * @param isRunningInCloudFunction whether the connector is executed in the cloud function - * @param storeResponseSelected whether the response should be stored as a Document (this property - * comes from the Element Template) */ static ExecutionEnvironment from( boolean cloudFunctionEnabled, boolean isRunningInCloudFunction, - boolean storeResponseSelected, OutboundConnectorContext context) { if (cloudFunctionEnabled) { - return new SaaSCallerSideEnvironment(storeResponseSelected, context); + return new SaaSCluster(context); } if (isRunningInCloudFunction) { - return new SaaSCloudFunctionSideEnvironment(storeResponseSelected); + return new SaaSCloudFunction(); } - return new SelfManagedEnvironment(storeResponseSelected, context); + return new SelfManaged(context); } interface StoresDocument { diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/HttpService.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/HttpService.java index 3725f4d6f0..621ca20c21 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/HttpService.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/HttpService.java @@ -58,10 +58,9 @@ public HttpCommonResult executeConnectorRequest( ExecutionEnvironment.from( cloudFunctionService.isCloudFunctionEnabled(), cloudFunctionService.isRunningInCloudFunction(), - request.isStoreResponse(), context); - if (executionEnvironment instanceof ExecutionEnvironment.SaaSCallerSideEnvironment) { + if (executionEnvironment instanceof ExecutionEnvironment.SaaSCluster) { // Wrap the request in a proxy request request = cloudFunctionService.toCloudFunctionRequest(request); } @@ -76,7 +75,7 @@ private HttpCommonResult executeRequest( return jsonResult; } catch (ConnectorException e) { LOGGER.debug("Failed to execute request {}", request, e); - if (executionEnvironment instanceof ExecutionEnvironment.SaaSCallerSideEnvironment) { + if (executionEnvironment instanceof ExecutionEnvironment.SaaSCluster) { throw cloudFunctionService.parseCloudFunctionError(e); } throw e; diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java index a0cb9204e1..addbe27f91 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java @@ -106,7 +106,10 @@ public HttpCommonResult execute( // (http.proxyHost, http.proxyPort, etc) .useSystemProperties() .build() - .execute(apacheRequest, new HttpCommonResultResponseHandler(executionEnvironment)); + .execute( + apacheRequest, + new HttpCommonResultResponseHandler( + executionEnvironment, request.isStoreResponse())); if (HttpStatusHelper.isError(result.status())) { throw ConnectorExceptionMapper.from(result); } diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandler.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandler.java index 5965fd4fb8..b989167ce4 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandler.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandler.java @@ -49,9 +49,14 @@ public class HttpCommonResultResponseHandler private final ExecutionEnvironment executionEnvironment; - public HttpCommonResultResponseHandler(@Nullable ExecutionEnvironment executionEnvironment) { + private final boolean isStoreResponseSelected; + + public HttpCommonResultResponseHandler( + @Nullable ExecutionEnvironment executionEnvironment, boolean isStoreResponseSelected) { this.executionEnvironment = executionEnvironment; - this.fileResponseHandler = new FileResponseHandler(executionEnvironment); + this.isStoreResponseSelected = isStoreResponseSelected; + this.fileResponseHandler = + new FileResponseHandler(executionEnvironment, isStoreResponseSelected); } @Override @@ -65,7 +70,7 @@ public HttpCommonResult handleResponse(ClassicHttpResponse response) { Collectors.toMap(Header::getName, Header::getValue, (first, second) -> first)); if (response.getEntity() != null) { try (InputStream content = response.getEntity().getContent()) { - if (executionEnvironment instanceof ExecutionEnvironment.SaaSCallerSideEnvironment) { + if (executionEnvironment instanceof ExecutionEnvironment.SaaSCluster) { return getResultForCloudFunction(code, content, headers, reason); } var bytes = content.readAllBytes(); @@ -121,8 +126,8 @@ private HttpCommonResult getResultForCloudFunction( * @param content the response content */ private Object extractBody(byte[] content) throws IOException { - if (executionEnvironment instanceof ExecutionEnvironment.SaaSCloudFunctionSideEnvironment - && executionEnvironment.storeResponseSelected()) { + if (executionEnvironment instanceof ExecutionEnvironment.SaaSCloudFunction + && isStoreResponseSelected) { return Base64.getEncoder().encodeToString(content); } diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/document/FileResponseHandler.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/document/FileResponseHandler.java index 4dfeafcf4a..4d5281f684 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/document/FileResponseHandler.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/document/FileResponseHandler.java @@ -33,9 +33,12 @@ public class FileResponseHandler { private static final Logger LOGGER = LoggerFactory.getLogger(FileResponseHandler.class); public static final String CONTENT_TYPE = "Content-Type"; private final ExecutionEnvironment executionEnvironment; + private final boolean isStoreResponseSelected; - public FileResponseHandler(@Nullable ExecutionEnvironment executionEnvironment) { + public FileResponseHandler( + @Nullable ExecutionEnvironment executionEnvironment, boolean isStoreResponseSelected) { this.executionEnvironment = executionEnvironment; + this.isStoreResponseSelected = isStoreResponseSelected; } public Document handleCloudFunctionResult(HttpCommonResult result) { @@ -78,6 +81,6 @@ private String getContentType(Map headers) { } private boolean storeResponseSelected() { - return executionEnvironment != null && executionEnvironment.storeResponseSelected(); + return executionEnvironment != null && isStoreResponseSelected; } } diff --git a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java index 1a8b377180..c84c34c409 100644 --- a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java +++ b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java @@ -93,9 +93,7 @@ public void shouldStoreDocument_whenStoreResponseEnabled(WireMockRuntimeInfo wmR request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/download"); HttpCommonResult result = customApacheHttpClient.execute( - request, - new ExecutionEnvironment.SelfManagedEnvironment( - request.isStoreResponse(), new DocumentOutboundContext())); + request, new ExecutionEnvironment.SelfManaged(new DocumentOutboundContext())); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); assertThat(result.headers().get(HttpHeaders.CONTENT_TYPE)) diff --git a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandlerTest.java b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandlerTest.java index b9eb64c873..e612211e0f 100644 --- a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandlerTest.java +++ b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/HttpCommonResultResponseHandlerTest.java @@ -35,7 +35,7 @@ public class HttpCommonResultResponseHandlerTest { @Test public void shouldHandleJsonResponse_whenCloudFunctionDisabled() throws Exception { // given - HttpCommonResultResponseHandler handler = new HttpCommonResultResponseHandler(null); + HttpCommonResultResponseHandler handler = new HttpCommonResultResponseHandler(null, false); ClassicHttpResponse response = new BasicClassicHttpResponse(200); Header[] headers = new Header[] {new BasicHeader("Content-Type", "application/json")}; response.setHeaders(headers); @@ -55,7 +55,7 @@ public void shouldHandleJsonResponse_whenCloudFunctionDisabled() throws Exceptio @Test public void shouldHandleTextResponse_whenCloudFunctionDisabled() throws Exception { // given - HttpCommonResultResponseHandler handler = new HttpCommonResultResponseHandler(null); + HttpCommonResultResponseHandler handler = new HttpCommonResultResponseHandler(null, false); ClassicHttpResponse response = new BasicClassicHttpResponse(200); Header[] headers = new Header[] {new BasicHeader("Content-Type", "text/plain")}; response.setHeaders(headers); @@ -76,8 +76,7 @@ public void shouldHandleTextResponse_whenCloudFunctionDisabled() throws Exceptio public void shouldHandleJsonResponse_whenCloudFunctionEnabled() throws Exception { // given HttpCommonResultResponseHandler handler = - new HttpCommonResultResponseHandler( - new ExecutionEnvironment.SaaSCallerSideEnvironment(false, null)); + new HttpCommonResultResponseHandler(new ExecutionEnvironment.SaaSCluster(null), false); ClassicHttpResponse response = new BasicClassicHttpResponse(201); Header[] headers = new Header[] {new BasicHeader("Content-Type", "application/json")}; response.setHeaders(headers); @@ -102,8 +101,7 @@ public void shouldHandleJsonResponse_whenCloudFunctionEnabled() throws Exception public void shouldHandleError_whenCloudFunctionEnabled() throws Exception { // given HttpCommonResultResponseHandler handler = - new HttpCommonResultResponseHandler( - new ExecutionEnvironment.SaaSCallerSideEnvironment(false, null)); + new HttpCommonResultResponseHandler(new ExecutionEnvironment.SaaSCluster(null), false); ClassicHttpResponse response = new BasicClassicHttpResponse(500); Header[] headers = new Header[] { @@ -132,8 +130,7 @@ public void shouldHandleError_whenCloudFunctionEnabled() throws Exception { public void shouldHandleJsonAsTextResponse_whenCloudFunctionEnabled() throws Exception { // given HttpCommonResultResponseHandler handler = - new HttpCommonResultResponseHandler( - new ExecutionEnvironment.SaaSCallerSideEnvironment(false, null)); + new HttpCommonResultResponseHandler(new ExecutionEnvironment.SaaSCluster(null), false); ClassicHttpResponse response = new BasicClassicHttpResponse(201); Header[] headers = new Header[] {new BasicHeader("Content-Type", "application/json")}; response.setHeaders(headers);