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

Extract Field Action: Make it possible to set if the extracted field should be in the body or in a particular header #681

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions kamelets/extract-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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.


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.

Choose a reason for hiding this comment

The 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"
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Choose a reason for hiding this comment

The 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 String sanitized(String headerOutput) does its best to ensure a valid headerName that does not collide with existing headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@oscerd oscerd Jan 7, 2022

Choose a reason for hiding this comment

The 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.

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down