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: Add the possibility of setting the header name in case of headerOutput is true #690

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion kamelets/extract-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ spec:

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.
The optional parameter headerOutputName allows the user to specify a custom header name instead of the default 'CamelKameletsExtractFieldName'. This parameter must be used in conjunction with headerOutput.
If no headerOutputName parameter will be provided, the default 'CamelKameletsExtractFieldName' will be used.

The headerOutput/headerOutputName parameters are particulary useful in case you would like to reuse an extracted field as parameter for another header, for example.
required:
- field
properties:
Expand All @@ -53,6 +56,11 @@ spec:
default: false
x-descriptors:
- 'urn:alm:descriptor:com.tectonic.ui:checkbox'
headerOutputName:
title: Header Output Name
description: A custom name for the header containing the extracted field
default: "none"
type: string
type: object
dependencies:
- "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"
Expand Down Expand Up @@ -80,6 +88,9 @@ spec:
- set-property:
name: "headerOutput"
constant: "{{headerOutput}}"
- set-property:
name: "headerOutputName"
constant: "{{headerOutputName}}"
- 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,15 +27,19 @@

public class ExtractField {

public void process(@ExchangeProperty("field") String field, @ExchangeProperty("headerOutput") boolean headerOutput, Exchange ex) {
public void process(@ExchangeProperty("field") String field, @ExchangeProperty("headerOutput") boolean headerOutput, @ExchangeProperty("headerOutputName") String headerOutputName, 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>>(){});
if (!headerOutput) {
ex.getMessage().setBody(body.get(field));
} else {
ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
if ("none".equalsIgnoreCase(headerOutputName)) {
ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
} else {
ex.getMessage().setHeader(headerOutputName, body.get(field));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ spec:

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.
The optional parameter headerOutputName allows the user to specify a custom header name instead of the default 'CamelKameletsExtractFieldName'. This parameter must be used in conjunction with headerOutput.
If no headerOutputName parameter will be provided, the default 'CamelKameletsExtractFieldName' will be used.

The headerOutput/headerOutputName parameters are particulary useful in case you would like to reuse an extracted field as parameter for another header, for example.
required:
- field
properties:
Expand All @@ -53,6 +56,11 @@ spec:
default: false
x-descriptors:
- 'urn:alm:descriptor:com.tectonic.ui:checkbox'
headerOutputName:
title: Header Output Name
description: A custom name for the header containing the extracted field
default: "none"
type: string
type: object
dependencies:
- "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"
Expand Down Expand Up @@ -80,6 +88,9 @@ spec:
- set-property:
name: "headerOutput"
constant: "{{headerOutput}}"
- set-property:
name: "headerOutputName"
constant: "{{headerOutputName}}"
- bean: "org.apache.camel.kamelets.utils.transform.ExtractField"
- choice:
when:
Expand Down