-
Notifications
You must be signed in to change notification settings - Fork 73
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
Extract Field Action: Make it possible to set if the extracted field should be in the body or in a particular header #681
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The field parameter allows you to ... |
||
|
||
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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per my comments in the issue, maybe this is better in line 38? ex.getMessage.setHeader(sanitized(headerOutput), body.get(field)); Where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Header output is a boolean. The header name is static and fixed. So, no. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this is a bean that should be simple. Introducing more logic to check stuff like headers existence is too much, there is for example the has header action or you can write your own kamelet action doing all the logic you want. But I'd probably leave the beans simplest as possible. |
||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extract field action expects an application/json content type.