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

chore: Avoid NPE on extract/insert field Kamelets #953

Merged
merged 1 commit into from
Jun 28, 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
1 change: 1 addition & 0 deletions .github/workflows/yaks-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ jobs:
run: |
echo "Running tests"
yaks run test/aws-ddb-sink $YAKS_RUN_OPTIONS
yaks run test/extract-field-action $YAKS_RUN_OPTIONS
yaks run test/insert-field-action $YAKS_RUN_OPTIONS
yaks run test/mail-sink $YAKS_RUN_OPTIONS
yaks run test/timer-source $YAKS_RUN_OPTIONS
Expand Down
37 changes: 20 additions & 17 deletions kamelets/extract-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ spec:
description: |-
Extract a field from the message body.

The extract field action expects an application/json content type.
The extract field action expects an application/json content type.

The field parameter allows you 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 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.
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 optional parameter strictHeaderCheck allows to user to enable a strict header name check. If enabled the action will check if the header output name (custom or default) has been used already in the exchange. If so, the extracted field will be stored in the message body, if not, the extracted field will be stored in the selected header (custom or default).
Expand Down Expand Up @@ -77,6 +77,20 @@ spec:
- "camel:core"
- "camel:jackson"
template:
beans:
- name: extractField
type: "#class:org.apache.camel.kamelets.utils.transform.ExtractField"
property:
- key: field
value: '{{field}}'
- key: headerOutput
value: '{{headerOutput}}'
- key: headerOutput
value: '{{headerOutput}}'
- key: headerOutputName
value: '{{headerOutputName}}'
- key: strictHeaderCheck
value: '{{strictHeaderCheck}}'
from:
uri: kamelet:source
steps:
Expand All @@ -88,28 +102,17 @@ spec:
name: deserialized
constant: "true"
- unmarshal:
json:
json:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
- set-property:
name: "field"
constant: "{{field}}"
- set-property:
name: "headerOutput"
constant: "{{headerOutput}}"
- set-property:
name: "headerOutputName"
constant: "{{headerOutputName}}"
- set-property:
name: "strictHeaderCheck"
constant: "{{strictHeaderCheck}}"
- bean: "org.apache.camel.kamelets.utils.transform.ExtractField"
- process:
ref: "{{extractField}}"
- choice:
when:
- simple: "${exchangeProperty[deserialized]} == 'true'"
steps:
- marshal:
json:
json:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
- set-header:
Expand Down
23 changes: 13 additions & 10 deletions kamelets/insert-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
description: |-
Adds a custom field with a simple language parsed value to the message in transit.

The extract field action expected an application/json content type.
The extract field action expected an application/json content type.

If for example you have an array like '{ "foo":"John", "bar":30 }' and your action has been configured with field as 'element' and value as 'hello', you'll get '{ "foo":"John", "bar":30, "element":"hello" }'
required:
Expand All @@ -54,6 +54,14 @@ spec:
- "camel:jackson"
- "camel:kamelet"
template:
beans:
- name: insertField
type: "#class:org.apache.camel.kamelets.utils.transform.InsertField"
property:
- key: field
value: '{{field}}'
- key: value
value: '{{value}}'
from:
uri: kamelet:source
steps:
Expand All @@ -65,22 +73,17 @@ spec:
name: deserialized
constant: "true"
- unmarshal:
json:
json:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
- set-property:
name: "field"
constant: "{{field}}"
- set-property:
name: "value"
simple: "{{value}}"
- bean: "org.apache.camel.kamelets.utils.transform.InsertField"
- process:
ref: "{{insertField}}"
- choice:
when:
- simple: "${exchangeProperty[deserialized]} == 'true'"
steps:
- marshal:
json:
json:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
- set-header:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,85 @@
*/
package org.apache.camel.kamelets.utils.transform;

import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangeProperty;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Processor;

import java.util.Map;
public class ExtractField implements Processor {

String field;
String headerOutputName;
boolean headerOutput;
boolean strictHeaderCheck;

static final String EXTRACTED_FIELD_HEADER = "CamelKameletsExtractFieldName";

/**
* Default constructor
*/
public ExtractField() {
}

public class ExtractField {
/**
* Constructor using field member.
* @param field the field name to extract.
*/
public ExtractField(String field) {
this.field = field;
}

public void process(@ExchangeProperty("field") String field, @ExchangeProperty("headerOutput") boolean headerOutput, @ExchangeProperty("headerOutputName") String headerOutputName, @ExchangeProperty("strictHeaderCheck") boolean strictHeaderCheck, Exchange ex) {
final String EXTRACTED_FIELD_HEADER = "CamelKameletsExtractFieldName";
@Override
public void process(Exchange ex) throws InvalidPayloadException {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNodeBody = ex.getMessage().getBody(JsonNode.class);

if (jsonNodeBody == null) {
throw new InvalidPayloadException(ex, JsonNode.class);

}

Map<Object, Object> body = mapper.convertValue(jsonNodeBody, new TypeReference<Map<Object, Object>>(){});
if (!headerOutput) {
if (!headerOutput || (strictHeaderCheck && checkHeaderExistence(ex))) {
ex.getMessage().setBody(body.get(field));
} else {
if (!strictHeaderCheck) {
if ("none".equalsIgnoreCase(headerOutputName)) {
ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
} else {
ex.getMessage().setHeader(headerOutputName, body.get(field));
}
} else {
if (checkHeaderExistence(EXTRACTED_FIELD_HEADER, ex) || checkHeaderExistence(headerOutputName, ex)) {
ex.getMessage().setBody(body.get(field));
} else {
if ("none".equalsIgnoreCase(headerOutputName)) {
ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
} else {
ex.getMessage().setHeader(headerOutputName, body.get(field));
}
}
}
extractToHeader(ex, body);
}
}

private final boolean checkHeaderExistence(String headerName, Exchange exchange) {
if (exchange.getMessage().getHeaders().containsKey(headerName)) {
return true;
private void extractToHeader(Exchange ex, Map<Object, Object> body) {
if (headerOutputName == null || headerOutputName.isEmpty() || "none".equalsIgnoreCase(headerOutputName)) {
ex.getMessage().setHeader(EXTRACTED_FIELD_HEADER, body.get(field));
} else {
return false;
ex.getMessage().setHeader(headerOutputName, body.get(field));
}
}

private boolean checkHeaderExistence(Exchange exchange) {
if (headerOutputName == null || headerOutputName.isEmpty() || "none".equalsIgnoreCase(headerOutputName)) {
return exchange.getMessage().getHeaders().containsKey(EXTRACTED_FIELD_HEADER);
} else {
return exchange.getMessage().getHeaders().containsKey(headerOutputName);
}
}

public void setField(String field) {
this.field = field;
}

public void setHeaderOutput(boolean headerOutput) {
this.headerOutput = headerOutput;
}

public void setHeaderOutputName(String headerOutputName) {
this.headerOutputName = headerOutputName;
}

public void setStrictHeaderCheck(boolean strictHeaderCheck) {
this.strictHeaderCheck = strictHeaderCheck;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,37 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangeProperty;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Processor;

public class InsertField {
public class InsertField implements Processor {

public JsonNode process(@ExchangeProperty("field") String field, @ExchangeProperty("value") String value, Exchange ex) throws InvalidPayloadException {
String field;
String value;

/**
* Default constructor.
*/
public InsertField() {
}

/**
* Constructor using fields.
* @param field the field name to insert.
* @param value the value of the new field.
*/
public InsertField(String field, String value) {
this.field = field;
this.value = value;
}

public void process(Exchange ex) throws InvalidPayloadException {
JsonNode body = ex.getMessage().getBody(JsonNode.class);

if (body == null) {
throw new InvalidPayloadException(ex, JsonNode.class);
}

switch (body.getNodeType()) {
case ARRAY:
((ArrayNode) body).add(value);
Expand All @@ -38,7 +62,15 @@ public JsonNode process(@ExchangeProperty("field") String field, @ExchangeProper
((ObjectNode) body).put(field, value);
break;
}
return body;

ex.getMessage().setBody(body);
}

public void setField(String field) {
this.field = field;
}

public void setValue(String value) {
this.value = value;
}
}
Loading