ReplaceField.java
Overview
The `ReplaceField` class is a JSON field manipulation utility designed for use within Apache Camel Kafka integration routes. Its primary function is to selectively filter, disable, enable, and rename fields within a JSON message payload represented as a Jackson `JsonNode`. It allows fine-grained control over which fields are included or excluded in the output JSON and supports renaming fields through configurable mappings.
This class is typically used as a processor in Camel routes to transform the JSON structure of Kafka messages dynamically based on exchange properties. It helps in adapting messages to downstream systems by shaping the JSON content without mutating the original payload directly.
Class: ReplaceField
Package
org.apache.camel.component.kafka.transform
Purpose
To process a JSON payload by:
Enabling only specified fields (if configured).
Disabling certain fields (removing them).
Renaming fields based on a provided mapping.
Returns a new JSON node reflecting these transformations.
Main Method
JsonNode process(String enabled, String disabled, String renames, Exchange ex) throws InvalidPayloadException
Processes the input JSON body from the Camel `Exchange` by applying field filters and renames.
Parameters
enabled(String): A comma-separated list of field names to enable (include). If"all"or empty, all fields are considered enabled by default.disabled(String): A comma-separated list of field names to disable (exclude). If"none"or empty, no fields are disabled.renames(String): A comma-separated list of rename mappings formatted as"oldName:newName".ex(Exchange): The Camel exchange containing the message and its body.
Returns
JsonNode: A new JSON node representing the transformed JSON object with applied filters and renames.
Throws
InvalidPayloadException: If the message body cannot be converted to a JSON node or if conversion fails.
Usage Example
ReplaceField replaceField = new ReplaceField();
Exchange exchange = ...; // assume initialized Camel Exchange with JSON body
String enabled = "name,email";
String disabled = "password";
String renames = "email:userEmail,name:fullName";
JsonNode transformedJson = replaceField.process(enabled, disabled, renames, exchange);
// The transformedJson will contain only 'name' and 'email' fields (if present),
// will exclude 'password', and rename 'email' -> 'userEmail', 'name' -> 'fullName'
Helper Methods
boolean filterNames(String fieldName, List<String> enabledFields, List<String> disabledFields)
Determines if a field should be included based on enabled and disabled lists.
Returns
trueif:The field is not in the disabled list.
The enabled list is empty (meaning all fields enabled), or the field is in the enabled list.
**Parameters:**
fieldName(String): The field name to check.enabledFields(List<String>): Fields explicitly enabled.disabledFields(List<String>): Fields explicitly disabled.
**Returns:**
boolean:trueif the field passes the filter; otherwise,false.
static Map<String, String> parseNames(List<String> mappings)
Parses a list of rename mappings into a key-value map.
Each mapping is expected in the form
"oldName:newName".
**Parameters:**
mappings(List<String>): List of rename mappings.
**Returns:**
Map<String, String>: Map of original field names to new field names.
**Example:**
List<String> mappings = Arrays.asList("email:userEmail", "name:fullName");
Map<String, String> renamingMap = ReplaceField.parseNames(mappings);
// renamingMap = { "email" -> "userEmail", "name" -> "fullName" }
String renameOptional(String fieldName, Map<String, String> renames)
Returns the new field name if a rename mapping exists; otherwise returns the original field name.
**Parameters:**
fieldName(String): Original field name.renames(Map<String, String>): Map of rename mappings.
**Returns:**
String: Renamed field or original field name.
Implementation Details and Algorithm
Input Extraction: The process method extracts the JSON body from the Camel
Exchangemessage as a JacksonJsonNode.Parsing Enabled/Disabled Fields:
The
enabledanddisabledstrings are parsed into lists.If
enabledis"all"or empty, all fields are considered enabled.If
disabledis"none"or empty, no fields are disabled.
Parsing Rename Mappings:
The
renamesstring is split into a list.Parsed using
parseNamesto create a map from original names to new names.
Field Filtering and Renaming:
Converts the JSON node body into a
Map<Object,Object>for easier iteration.Iterates over each field:
Filters fields using
filterNames.Renames fields using
renameOptional.Adds the transformed field to a new map.
Output Generation:
If any renames or filters are applied, converts the updated map back into a
JsonNode.Otherwise, returns the original JSON node.
Interaction with Other Components
Camel Exchange: Reads from and writes to the Camel exchange's message body.
Jackson ObjectMapper: Used for JSON serialization and deserialization.
Apache Camel Kafka Component: Used within Camel Kafka route processors to transform messages before producing to Kafka.
The class is designed to be used as part of Kafka message transformation processors in Apache Camel routes, often in conjunction with other JSON manipulation classes such as `InsertField`, `DropField`, and `MaskField`.
Class Diagram
classDiagram
class ReplaceField {
+JsonNode process(String enabled, String disabled, String renames, Exchange ex) throws InvalidPayloadException
+boolean filterNames(String fieldName, List~String~ enabledFields, List~String~ disabledFields)
+static Map~String,String~ parseNames(List~String~ mappings)
+String renameOptional(String fieldName, Map~String,String~ renames)
}
Summary
The
ReplaceFieldclass is a utility processor that applies selective field filtering and renaming to JSON payloads within Apache Camel Kafka routes.It provides configurable field enable/disable lists and rename mappings.
It processes JSON in an immutable fashion by creating new JSON nodes with the applied transformations.
This class facilitates flexible message shaping to meet integration requirements such as compliance, routing, or data format adaptation.
It is commonly used alongside other JSON transformation utilities within the Message Transformation Utilities module.
If you need further guidance on integrating `ReplaceField` into your Camel routes or extending its functionality, please let me know!