This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch ex-field-strict-check
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git

commit c83d05ec9350ebe10b2ad704342cc50fcc2505d6
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Jan 10 14:45:17 2022 +0100

    Extract Field Action: Sanitize/check the header name if set and avoid 
clashes
---
 kamelets/extract-field-action.kamelet.yaml         | 10 ++++++++
 .../kamelets/utils/transform/ExtractField.java     | 28 ++++++++++++++++++----
 .../kamelets/extract-field-action.kamelet.yaml     | 10 ++++++++
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/kamelets/extract-field-action.kamelet.yaml 
b/kamelets/extract-field-action.kamelet.yaml
index 32e83b8..562952c 100644
--- a/kamelets/extract-field-action.kamelet.yaml
+++ b/kamelets/extract-field-action.kamelet.yaml
@@ -61,6 +61,13 @@ spec:
         description: A custom name for the header containing the extracted 
field
         default: "none"
         type: string
+      strictHeaderCheck:
+        title: Strict Header Check
+        description: If enable the action will check if the header output name 
(custom or default) has been used already in the exchange. If so, the extracted 
field will be stored in the message body, if not, the extracted field will be 
stored in the selected header (custom or default).
+        type: boolean
+        default: false
+        x-descriptors:
+        - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
     type: object
   dependencies:
   - "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"
@@ -91,6 +98,9 @@ spec:
       - set-property:
           name: "headerOutputName"
           constant: "{{headerOutputName}}"
+      - set-property:
+          name: "strictHeaderCheck"
+          constant: "{{strictHeaderCheck}}"
       - bean: "org.apache.camel.kamelets.utils.transform.ExtractField"
       - choice:
           when:
diff --git 
a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/transform/ExtractField.java
 
b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/transform/ExtractField.java
index fcfc675..72a2758 100644
--- 
a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/transform/ExtractField.java
+++ 
b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/transform/ExtractField.java
@@ -27,7 +27,7 @@ import java.util.Map;
 
 public class ExtractField {
 
-    public void process(@ExchangeProperty("field") String field, 
@ExchangeProperty("headerOutput") boolean headerOutput, 
@ExchangeProperty("headerOutputName") String headerOutputName, Exchange ex) {
+    public void process(@ExchangeProperty("field") String field, 
@ExchangeProperty("headerOutput") boolean headerOutput, 
@ExchangeProperty("headerOutputName") String headerOutputName, 
@ExchangeProperty("strictHeaderCheck") boolean strictHeaderCheck, Exchange ex) {
         final String EXTRACTED_FIELD_HEADER = "CamelKameletsExtractFieldName";
         ObjectMapper mapper = new ObjectMapper();
         JsonNode jsonNodeBody = ex.getMessage().getBody(JsonNode.class);
@@ -35,12 +35,32 @@ public class ExtractField {
         if (!headerOutput) {
             ex.getMessage().setBody(body.get(field));
         } else {
-            if ("none".equalsIgnoreCase(headerOutputName)) {
-                ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, 
body.get(field));
+            if (!strictHeaderCheck) {
+                if ("none".equalsIgnoreCase(headerOutputName)) {
+                    ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, 
body.get(field));
+                } else {
+                    ex.getMessage().setHeader(headerOutputName, 
body.get(field));
+                }
             } else {
-                ex.getMessage().setHeader(headerOutputName, body.get(field));
+                if (checkHeaderExistence(EXTRACTED_FIELD_HEADER, ex) || 
checkHeaderExistence(headerOutputName, ex)) {
+                    ex.getMessage().setBody(body.get(field));
+                } else {
+                    if ("none".equalsIgnoreCase(headerOutputName)) {
+                        ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, 
body.get(field));
+                    } else {
+                        ex.getMessage().setHeader(headerOutputName, 
body.get(field));
+                    }
+                }
             }
         }
     }
 
+    private final boolean checkHeaderExistence(String headerName, Exchange 
exchange) {
+        if (exchange.getMessage().getHeaders().containsKey(headerName)) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
 }
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
index 32e83b8..562952c 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
@@ -61,6 +61,13 @@ spec:
         description: A custom name for the header containing the extracted 
field
         default: "none"
         type: string
+      strictHeaderCheck:
+        title: Strict Header Check
+        description: If enable the action will check if the header output name 
(custom or default) has been used already in the exchange. If so, the extracted 
field will be stored in the message body, if not, the extracted field will be 
stored in the selected header (custom or default).
+        type: boolean
+        default: false
+        x-descriptors:
+        - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
     type: object
   dependencies:
   - "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"
@@ -91,6 +98,9 @@ spec:
       - set-property:
           name: "headerOutputName"
           constant: "{{headerOutputName}}"
+      - set-property:
+          name: "strictHeaderCheck"
+          constant: "{{strictHeaderCheck}}"
       - bean: "org.apache.camel.kamelets.utils.transform.ExtractField"
       - choice:
           when:

Reply via email to