Skip to content

Commit

Permalink
feat(rest): PR comments fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBgood committed Jan 6, 2025
1 parent fbe8d10 commit 3edf511
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, Object> customProperties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,44 @@
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 {}

/**
* Factory method to create an ExecutionEnvironment based on the given parameters.
*
* @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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -78,6 +81,6 @@ private String getContentType(Map<String, String> headers) {
}

private boolean storeResponseSelected() {
return executionEnvironment != null && executionEnvironment.storeResponseSelected();
return executionEnvironment != null && isStoreResponseSelected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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[] {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3edf511

Please sign in to comment.