MaskField.java


Overview

The `MaskField` class is a utility processor designed to **mask sensitive fields** within JSON message payloads in Apache Camel routes, specifically targeting Kafka message transformations. It replaces values of specified JSON fields with either **default "null-equivalent" values** or **custom replacement values**, depending on configuration.

This class operates by:

This processor is typically used in data privacy or compliance scenarios where sensitive information must be sanitized or obscured before sending messages downstream.


Class Summary

public class MaskField

Member Type

Name

Description

**Static Fields**

`MAPPING_FUNC`

Map of Java classes to functions parsing `String` replacement values into the corresponding type.

`BASIC_MAPPING`

Map of Java classes to default mask values (e.g., 0 for numbers, empty string for String).

**Methods**

`process()`

Masks specified fields in the JSON body of the Camel Exchange based on given parameters.

`filterNames()`

Helper to check if a field is in the list of fields to mask.

`masked()`

Determines how to mask a single value (null masking or custom replacement).

`maskWithCustomReplacement()`

Converts a replacement string to the value's type using `MAPPING_FUNC`.

`maskWithNullValue()`

Returns the default null-equivalent value for a given type.


Detailed API

Static Fields

private static final Map<Class<?>, Function<String, ?>> MAPPING_FUNC
private static final Map<Class<?>, Object> BASIC_MAPPING

public JsonNode process(String fields, String replacement, Exchange ex) throws InvalidPayloadException

Processes the masking operation on the JSON body contained in the Camel `Exchange`.

Parameters:

Returns:

Throws:

Behavior:

  1. Parses the JSON payload from the Exchange message body into a generic Map.

  2. Splits the fields string into a list.

  3. Iterates over all fields in the JSON object:

    • If the field is in the mask list, replaces its value with a masked version.

    • Otherwise, leaves it unchanged.

  4. Returns a new JsonNode representing the masked JSON object.

Usage Example:

// Assume 'exchange' is a Camel Exchange with JSON body:
// {"username":"user1", "password":"secret", "age":30}

MaskField masker = new MaskField();
String fieldsToMask = "password";
String replacementValue = null; // Use default masking

JsonNode maskedJson = masker.process(fieldsToMask, replacementValue, exchange);

// Resulting JSON:
// {"username":"user1", "password":"", "age":30}

boolean filterNames(String fieldName, List<String> splittedFields)

Helper method to check if a field should be masked.

Parameters:

Returns:


private Object masked(Object value, String replacement)

Determines the masked value for a given field value.

Parameters:

Returns:

Behavior:


private static Object maskWithCustomReplacement(Object value, String replacement)

Returns a masked value by converting the replacement string into the original value's type.

Parameters:

Returns:

Throws:


private static Object maskWithNullValue(Object value)

Returns the default "null" or masked value for the given object’s type.

Parameters:

Returns:

Throws:


Important Implementation Details


Interaction with Other Components


Class Diagram

classDiagram
    class MaskField {
        -static Map<Class<?>, Function<String, ?>> MAPPING_FUNC
        -static Map<Class<?>, Object> BASIC_MAPPING
        +JsonNode process(String fields, String replacement, Exchange ex) throws InvalidPayloadException
        +boolean filterNames(String fieldName, List<String> splittedFields)
        -Object masked(Object value, String replacement)
        -static Object maskWithCustomReplacement(Object value, String replacement)
        -static Object maskWithNullValue(Object value)
    }

Summary

`MaskField` is a focused processor class that enables secure and configurable masking of JSON fields within Apache Camel Kafka routes. By mapping Java types to default masked values or parsing custom replacements, it provides robust type-safe masking behavior. Its integration with Camel's Exchange model and reliance on Jackson for JSON processing makes it a flexible and reusable component in Kafka message transformation pipelines, especially for data privacy and compliance needs.