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

feat(webhook): Replace DocumentReference by Document #3757

Merged
merged 3 commits into from
Dec 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import io.camunda.connector.feel.FeelEngineWrapperException;
import io.camunda.connector.runtime.inbound.executable.RegisteredExecutable;
import io.camunda.connector.runtime.inbound.webhook.model.HttpServletRequestWebhookProcessingPayload;
import io.camunda.document.reference.DocumentReference;
import io.camunda.document.Document;
import io.camunda.document.store.DocumentCreationRequest;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -137,12 +137,11 @@
// Step 2: trigger and correlate
var webhookResult = connectorHook.triggerWebhook(payload);
// create documents if the connector is activable
var documentReferences =
createDocuments(connector.context(), webhookResult, payload.parts());
var ctxData = toWebhookTriggerResultContext(webhookResult, documentReferences);
var documents = createDocuments(connector.context(), webhookResult, payload.parts());
var ctxData = toWebhookTriggerResultContext(webhookResult, documents);
// correlate
var correlationResult = connector.context().correlateWithResult(ctxData);
response = buildResponse(webhookResult, documentReferences, correlationResult);
response = buildResponse(webhookResult, documents, correlationResult);
}
} catch (Exception e) {
LOG.info("Webhook: {} failed with exception", connector.context().getDefinition(), e);
Expand All @@ -151,7 +150,7 @@
return response;
}

private List<DocumentReference> createDocuments(
private List<Document> createDocuments(
InboundConnectorContext context,
WebhookResult webhookResult,
Collection<io.camunda.connector.api.inbound.webhook.Part> parts) {
Expand All @@ -162,13 +161,11 @@
return parts.stream()
.map(
part ->
context
.createDocument(
DocumentCreationRequest.from(part.inputStream())
.fileName(part.submittedFileName())
.contentType(part.contentType())
.build())
.reference())
context.createDocument(
DocumentCreationRequest.from(part.inputStream())
.fileName(part.submittedFileName())
.contentType(part.contentType())
.build()))
.toList();
}

Expand All @@ -183,18 +180,15 @@
}

private ResponseEntity<?> buildResponse(
WebhookResult webhookResult,
List<DocumentReference> documentReferences,
CorrelationResult correlationResult) {
WebhookResult webhookResult, List<Document> documents, CorrelationResult correlationResult) {
ResponseEntity<?> response;
if (correlationResult instanceof CorrelationResult.Success success) {
response = buildSuccessfulResponse(webhookResult, documentReferences, success);
response = buildSuccessfulResponse(webhookResult, documents, success);
} else {
if (correlationResult instanceof CorrelationResult.Failure failure) {
switch (failure.handlingStrategy()) {
case ForwardErrorToUpstream ignored -> response = buildErrorResponse(failure);
case Ignore ignored ->
response = buildSuccessfulResponse(webhookResult, documentReferences, null);
case Ignore ignored -> response = buildSuccessfulResponse(webhookResult, documents, null);

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'Ignore ignored' is never read.
}
} else {
throw new IllegalStateException("Illegal correlation result : " + correlationResult);
Expand All @@ -215,12 +209,12 @@

private ResponseEntity<?> buildSuccessfulResponse(
WebhookResult webhookResult,
List<DocumentReference> documentReferences,
List<Document> documents,
CorrelationResult.Success correlationResult) {
ResponseEntity<?> response;
if (webhookResult.response() != null) {
var processVariablesContext =
toWebhookResultContext(webhookResult, documentReferences, correlationResult);
toWebhookResultContext(webhookResult, documents, correlationResult);
var httpResponseData = webhookResult.response().apply(processVariablesContext);
if (httpResponseData != null) {
response = toResponseEntity(httpResponseData);
Expand Down Expand Up @@ -292,7 +286,7 @@
// This will be used to correlate data returned from connector.
// In other words, we pass this data to Zeebe.
private WebhookTriggerResultContext toWebhookTriggerResultContext(
WebhookResult processedResult, List<DocumentReference> documentReferences) {
WebhookResult processedResult, List<Document> documents) {
WebhookTriggerResultContext ctx = new WebhookTriggerResultContext(null, null, List.of());
if (processedResult != null) {
ctx =
Expand All @@ -302,7 +296,7 @@
Optional.ofNullable(processedResult.request().headers()).orElse(emptyMap()),
Optional.ofNullable(processedResult.request().params()).orElse(emptyMap())),
Optional.ofNullable(processedResult.connectorData()).orElse(emptyMap()),
documentReferences);
documents);
}
return ctx;
}
Expand All @@ -312,7 +306,7 @@
// this data may be returned to the webhook caller.
private WebhookResultContext toWebhookResultContext(
WebhookResult processedResult,
List<DocumentReference> documents,
List<Document> documents,
CorrelationResult.Success correlationResult) {
WebhookResultContext ctx = new WebhookResultContext(null, null, null);
if (processedResult != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package io.camunda.connector.api.inbound.webhook;

import io.camunda.document.reference.DocumentReference;
import io.camunda.document.Document;
import java.util.List;
import java.util.Map;

public record WebhookResultContext(
MappedHttpRequest request,
Map<String, Object> connectorData,
Object correlation,
List<DocumentReference> documents) {
List<Document> documents) {

public WebhookResultContext(
MappedHttpRequest request, Map<String, Object> connectorData, Object correlation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package io.camunda.connector.api.inbound.webhook;

import io.camunda.document.reference.DocumentReference;
import io.camunda.document.Document;
import java.util.List;
import java.util.Map;

Expand All @@ -25,9 +25,7 @@
* variables.
*/
public record WebhookTriggerResultContext(
MappedHttpRequest request,
Map<String, Object> connectorData,
List<DocumentReference> documents) {
MappedHttpRequest request, Map<String, Object> connectorData, List<Document> documents) {
public WebhookTriggerResultContext(MappedHttpRequest request, Map<String, Object> connectorData) {
this(request, connectorData, List.of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected Function<IN, OUT> doDeserialize(
deserializationContext,
node.textValue(),
deserializationContext.getTypeFactory().constructType(JsonNode.class),
mergeContexts(input, feelContext));
input,
feelContext);
try {
if (jsonNode == null || jsonNode.isNull()) {
return null;
Expand All @@ -68,23 +69,6 @@ protected Function<IN, OUT> doDeserialize(
};
}

private Object mergeContexts(Object inputContext, Object feelContext) {
try {
var wrappedInput =
new MergedContext(BLANK_OBJECT_MAPPER.convertValue(inputContext, MAP_TYPE_REF));
var wrappedFeelContext =
new MergedContext(BLANK_OBJECT_MAPPER.convertValue(feelContext, MAP_TYPE_REF));
var merged =
BLANK_OBJECT_MAPPER
.readerForUpdating(wrappedInput)
.treeToValue(
BLANK_OBJECT_MAPPER.valueToTree(wrappedFeelContext), MergedContext.class);
return merged.context;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
if (property.getType().containedTypeCount() == 2) {
Expand Down
Loading