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:
Accepting a list of field names to mask.
Masking those fields in the incoming JSON object body.
Supporting multiple common Java types (e.g., numeric types, strings, dates).
Throwing exceptions if a value cannot be masked due to unsupported type or invalid replacement format.
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
Maps Java wrapper classes to functions that convert replacement strings into the respective types.
Example:
Integer.class -> Integer::parseInt
private static final Map<Class<?>, Object> BASIC_MAPPING
Maps Java classes to default mask values representing a "nullified" or masked form.
Examples:
Integer.class -> 0String.class -> ""(empty string)Date.class -> new Date(0)(epoch start)ListandMaphandled specially in masking method.
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:
fields(String): Comma-separated list of JSON field names to mask.replacement(String): Optional replacement value as a string. Ifnull, default null-equivalent masking is applied.ex(Exchange): The Camel Exchange containing the message to process.
Returns:
JsonNode: The updated JSON body with specified fields masked.
Throws:
InvalidPayloadExceptionif the body cannot be converted to JSON.
Behavior:
Parses the JSON payload from the Exchange message body into a generic Map.
Splits the
fieldsstring into a list.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.
Returns a new
JsonNoderepresenting 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:
fieldName(String): The name of the JSON field.splittedFields(List): List of fields to mask.
Returns:
trueiffieldNameis insplittedFields, otherwisefalse.
private Object masked(Object value, String replacement)
Determines the masked value for a given field value.
Parameters:
value(Object): The original field value.replacement(String): Optional replacement string.
Returns:
Masked value of the same type as
value, ornullif the original value wasnull.
Behavior:
If the original value is
null, returnsnull.If
replacementisnull, callsmaskWithNullValue()to return a default mask.Otherwise, calls
maskWithCustomReplacement()to parse the replacement.
private static Object maskWithCustomReplacement(Object value, String replacement)
Returns a masked value by converting the replacement string into the original value's type.
Parameters:
value(Object): Original field value.replacement(String): Replacement string value.
Returns:
Converted replacement value of the same type as
value.
Throws:
IllegalArgumentExceptionif type is unsupported or conversion fails.
private static Object maskWithNullValue(Object value)
Returns the default "null" or masked value for the given object’s type.
Parameters:
value(Object): The original field value.
Returns:
Default masked value for the type, e.g.:
0for numeric types""for stringsEmpty list or map for collections
new Date(0)for dates
Throws:
IllegalArgumentExceptionif type is unsupported.
Important Implementation Details
Uses Jackson's
ObjectMapperto convert between JSON trees (JsonNode) and Java maps.Uses Java 8 functional interfaces (
Function<String, ?>) to convert replacement strings into typed values.Supports primitive wrapper types,
BigInteger,BigDecimal,Date,String, and collections (List,Map).For collection types (List/Map), returns empty immutable collections as masked values.
Throws meaningful exceptions when masking is impossible, preventing silent failures.
The masking operation does not mutate the original JSON object directly; it creates a new map and converts it back to a
JsonNode.
Interaction with Other Components
Camel Exchange: Reads the message body as a JSON object from the Camel exchange.
Kafka Component: Typically used in Kafka message routes to mask sensitive fields before message production or forwarding.
Other JSON Field Manipulation Processors: Complements processors like
InsertField,DropField, andReplaceFieldin the JSON Field Manipulation suite.Configuration via Exchange Properties: The list of fields to mask and replacement values are passed as exchange properties (
fields,replacement), enabling flexible runtime configuration.
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.