From a0dfcfb37a35c6b6798b81e1c93030c0469a80cb Mon Sep 17 00:00:00 2001 From: Andrea Cosentino Date: Fri, 7 Jan 2022 14:34:01 +0100 Subject: [PATCH 1/2] 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 +++++++++++++++++-- .../utils/transform/ExtractField.java | 9 ++++++-- .../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 2ff0f3c11..33d4e23f2 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 a0a396e48..36d2416df 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 @@ 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 body = mapper.convertValue(jsonNodeBody, new TypeReference>(){}); - 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 2ff0f3c11..09d3c8240 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: From cb37a54fa8c6ef6c51e40e1daa33f419374c54e3 Mon Sep 17 00:00:00 2001 From: Andrea Cosentino Date: Fri, 7 Jan 2022 14:35:19 +0100 Subject: [PATCH 2/2] 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 09d3c8240..33d4e23f2 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 @@ -29,13 +29,22 @@ 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