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

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
Returns
Throws
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.

**Parameters:**

**Returns:**


static Map<String, String> parseNames(List<String> mappings)

Parses a list of rename mappings into a key-value map.

**Parameters:**

**Returns:**

**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:**

**Returns:**


Implementation Details and Algorithm

  1. Input Extraction: The process method extracts the JSON body from the Camel Exchange message as a Jackson JsonNode.

  2. Parsing Enabled/Disabled Fields:

    • The enabled and disabled strings are parsed into lists.

    • If enabled is "all" or empty, all fields are considered enabled.

    • If disabled is "none" or empty, no fields are disabled.

  3. Parsing Rename Mappings:

    • The renames string is split into a list.

    • Parsed using parseNames to create a map from original names to new names.

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

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

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


If you need further guidance on integrating `ReplaceField` into your Camel routes or extending its functionality, please let me know!