You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/01/07 13:36:12 UTC

[camel-kamelets] 01/02: Extract Field Action: Make it possible to set if the extracted field should be in the body or in a particular header

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

acosentino pushed a commit to branch extract-action
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git

commit a0dfcfb37a35c6b6798b81e1c93030c0469a80cb
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Jan 7 14:34:01 2022 +0100

    Extract Field Action: Make it possible to set if the extracted field should be in the body or in a particular header
---
 kamelets/extract-field-action.kamelet.yaml         | 23 ++++++++++++++++++++--
 .../kamelets/utils/transform/ExtractField.java     |  9 +++++++--
 .../kamelets/extract-field-action.kamelet.yaml     | 10 ++++++++++
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/kamelets/extract-field-action.kamelet.yaml b/kamelets/extract-field-action.kamelet.yaml
index 2ff0f3c..33d4e23 100644
--- a/kamelets/extract-field-action.kamelet.yaml
+++ b/kamelets/extract-field-action.kamelet.yaml
@@ -29,14 +29,30 @@ metadata:
 spec:
   definition:
     title: "Extract Field Action"
-    description: "Extract a field from the body"
+    description: |-
+      Extract a field from the message body.
+
+      The extract field action expected an application/json content type. 
+
+      The field parameter allows to specify which field of the json the user wants to extract. By default the message body will be overriden with the extracted field.
+
+      The optional parameter headerOutput allows the user to specify wheter the extracted field should be stored in a message header named 'CamelKameletsExtractFieldName', leaving the message body untouched.
+
+      The headerOutput is particulary useful in case you would like to reuse an extracted field as parameters for another header, for example.
     required:
       - field
     properties:
       field:
         title: Field
-        description: The name of the field to be added
+        description: The name of the field to extract
         type: string
+      headerOutput:
+        title: Header Output
+        description: If enable the action will store the extracted field in an header named CamelKameletsExtractFieldName
+        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"
@@ -61,6 +77,9 @@ spec:
       - set-property:
           name: "field"
           constant: "{{field}}"
+      - set-property:
+          name: "headerOutput"
+          constant: "{{headerOutput}}"
       - 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 a0a396e..36d2416 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,11 +27,16 @@ import java.util.Map;
 
 public class ExtractField {
 
-    public void process(@ExchangeProperty("field") String field, Exchange ex) {
+    public void process(@ExchangeProperty("field") String field, @ExchangeProperty("headerOutput") boolean headerOutput, Exchange ex) {
+        final String EXTRACTED_FIELD_HEADER = "CamelKameletsExtractFieldName";
         ObjectMapper mapper = new ObjectMapper();
         JsonNode jsonNodeBody = ex.getMessage().getBody(JsonNode.class);
         Map<Object, Object> body = mapper.convertValue(jsonNodeBody, new TypeReference<Map<Object, Object>>(){});
-        ex.getMessage().setBody(body.get(field));
+        if (!headerOutput) {
+            ex.getMessage().setBody(body.get(field));
+        } else {
+            ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
+        }
     }
 
 }
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 2ff0f3c..09d3c82 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
@@ -37,6 +37,13 @@ spec:
         title: Field
         description: The name of the field to be added
         type: string
+      headerOutput:
+        title: Header Output
+        description: If enable the action will store the extracted field in an header named CamelKameletsExtractFieldName
+        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"
@@ -61,6 +68,9 @@ spec:
       - set-property:
           name: "field"
           constant: "{{field}}"
+      - set-property:
+          name: "headerOutput"
+          constant: "{{headerOutput}}"
       - bean: "org.apache.camel.kamelets.utils.transform.ExtractField"
       - choice:
           when: