ExtractField.java


Overview

`ExtractField.java` defines the `ExtractField` class, a Camel `Processor` implementation designed to extract a specific field from a JSON message body within an Apache Camel Exchange during Kafka message processing. The extracted value can either replace the message body or be stored in a message header, based on configurable options.

This processor is typically used in Kafka integration pipelines to isolate and promote a particular JSON field from the message payload, facilitating downstream routing, filtering, or processing logic that depends on that field.


Class: ExtractField

public class ExtractField implements Processor

Purpose

Key Features


Fields

Field Name

Type

Description

`String field`

String

The name of the JSON field to extract from the message body.

`String headerOutputName`

String

Optional header name to place the extracted field value into. Defaults to a constant header if unset.

`boolean headerOutput`

boolean

Flag to output extracted field value in a header instead of the message body.

`boolean strictHeaderCheck`

boolean

When `true`, only extract to header if the header does not already exist (avoids overwrite).

`boolean trimField`

boolean

When `true`, sets a Camel exchange property `"trimField"` to `"true"`, else `"false"`.


Constants

Constant Name

Type

Value

Description

`EXTRACTED_FIELD_HEADER`

String

`"CamelKameletsExtractFieldName"`

Default header name used for extracted fields if none specified.


Constructors

ExtractField()

ExtractField(String field)

**Example:**

ExtractField extractor = new ExtractField("userId");

Methods

void process(Exchange ex) throws InvalidPayloadException

**Parameters:**

Parameter

Type

Description

`ex`

Exchange

The Camel exchange object for the current message.

**Throws:**

**Usage example:**

ExtractField extractor = new ExtractField("orderId");
extractor.setHeaderOutput(true);
extractor.setHeaderOutputName("ExtractedOrderId");
extractor.process(exchange);

void setField(String field)


void setHeaderOutput(boolean headerOutput)


void setHeaderOutputName(String headerOutputName)


void setStrictHeaderCheck(boolean strictHeaderCheck)


void setTrimField(boolean trimField)


Private Helper Methods

private void extractToHeader(Exchange ex, Map<Object, Object> body)


private boolean checkHeaderExistence(Exchange exchange)


Implementation Details and Algorithm

  1. Parsing JSON Body:

    • The processor attempts to retrieve the message body as a Jackson JsonNode.

    • If the body is not a valid JSON node, throws InvalidPayloadException.

  2. Conversion to Map:

    • Converts the JsonNode into a generic Map<Object, Object> using Jackson's ObjectMapper and TypeReference.

    • This facilitates easy field extraction with dynamic key lookup.

  3. Extraction Logic:

    • If headerOutput is false, or strictHeaderCheck is true but the target header already exists, it replaces the message body with the extracted value.

    • Otherwise, it extracts the field value into the specified header.

  4. Setting Properties:

    • Sets the exchange property "trimField" to "true" or "false" depending on the trimField flag.

    • This allows downstream processors or routes to react accordingly.


Usage Example

// Create processor to extract "customerId" from JSON body
ExtractField extractor = new ExtractField("customerId");

// Configure to output extracted value into a header named "Customer-ID"
extractor.setHeaderOutput(true);
extractor.setHeaderOutputName("Customer-ID");

// Enable strict check to avoid overwriting existing headers
extractor.setStrictHeaderCheck(true);

// Enable trim field property setting
extractor.setTrimField(true);

// In a Camel route:
// .process(extractor)

This will extract the value of the `"customerId"` field from the JSON body. If the `"Customer-ID"` header does not already exist, it will be set with this value. Additionally, the property `"trimField"` will be set to `"true"` on the exchange.


Interaction with Other System Components


Mermaid Class Diagram

classDiagram
    class ExtractField {
        -String field
        -String headerOutputName
        -boolean headerOutput
        -boolean strictHeaderCheck
        -boolean trimField
        +void process(Exchange ex) throws InvalidPayloadException
        +void setField(String field)
        +void setHeaderOutput(boolean headerOutput)
        +void setHeaderOutputName(String headerOutputName)
        +void setStrictHeaderCheck(boolean strictHeaderCheck)
        +void setTrimField(boolean trimField)
        -void extractToHeader(Exchange ex, Map<Object,Object> body)
        -boolean checkHeaderExistence(Exchange exchange)
    }

Summary

The `ExtractField` processor facilitates efficient and configurable extraction of specific JSON fields from Kafka messages within Apache Camel routes. By supporting both body replacement and header insertion modes, along with strict header existence checks and trimming flags, it enables flexible integration patterns and enhances message enrichment and routing capabilities in Kafka-based data pipelines.

This class is a key component in the suite of JSON field manipulation processors that allow fine-grained control over Kafka message contents and metadata, ensuring streamlined and maintainable transformations in complex integration scenarios.