Camel Components

JOLT

Since Camel 2.16

Only producer is supported

The Jolt component allows you to process a JSON messages using a JOLT specification. This can be ideal when doing JSON to JSON transformation.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jolt</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

jolt:specName[?options]

Where specName is the classpath-local URI of the specification to invoke; or the complete URL of the remote specification (e.g.: file://folder/myfile.vm).

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The JOLT component supports the following options which are listed below.

Name Description Default Type

allowTemplateFromHeader (producer)

Whether the endpoints read the CamelJoltResourceUri and CamelJoltContext headers. It is the default of the allowTemplateFromHeader option of each endpoint. Enable it only when the headers come from a trusted source.

false

boolean

contentCache (producer)

Whether the endpoints keep the content of their spec resource in memory after the first read. It is the default of the contentCache option of each endpoint. Each endpoint builds its JOLT transform from the spec once, at the first exchange, and keeps it, so false does not make it read a changed spec again.

true

boolean

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

transform (advanced)

A JOLT Transform that all endpoints of the component use in place of the transform that they build from their spec. When set, the endpoints do not read their spec resource, and transformDsl and the CamelJoltResourceUri header have no effect. To use the CamelJoltContext header, it must also be a ContextualTransform, such as a Chainr.

Transform

Endpoint Options

The JOLT endpoint is configured using URI syntax:

jolt:resourceUri

With the following path and query parameters:

Path Parameters

Name Description Default Type

resourceUri (producer)

Required Path to the resource. You can prefix with: classpath, file, http, ref, or bean. classpath, file and http loads the resource using these protocols (classpath is default). ref will lookup the resource in the registry. bean will call a method on a bean to be used as the resource. For bean you can specify the method name after dot, eg bean:myBean.myMethod.

String

Query Parameters

Name Description Default Type

allowTemplateFromHeader (producer)

Whether the producer reads the CamelJoltResourceUri and CamelJoltContext headers. If false, the producer ignores both headers. Enable it only when the headers come from a trusted source.

false

boolean

contentCache (producer)

Sets whether to use resource content cache or not.

true

boolean

inputType (producer)

Whether the producer parses the body as JSON or uses it as it is. With Hydrated, a body that is JSON text (its first non-whitespace character is \{ or [), bytes, a stream or a file is parsed as JSON, and any other body, such as a Map or a List, is used as it is. With JsonString, the body is always parsed as JSON. With both types, a Jackson JsonNode body is converted to a Map or a List.

Enum values:

  • Hydrated

  • JsonString

Hydrated

JoltInputOutputType

outputType (producer)

Whether the producer sets the body to the result of the transform as it is or to its JSON text. With Hydrated, the body is the result as it is, which is a Map for a JSON object and a List for a JSON array. With JsonString, the body is the result written as a JSON String.

Enum values:

  • Hydrated

  • JsonString

Hydrated

JoltInputOutputType

transformDsl (producer)

The JOLT transform that the producer builds from the spec of resourceUri. Chainr: the spec is a JSON array of operations, each with an operation name (such as shift, default, remove, sort, cardinality or modify-overwrite-beta) and its spec. Shiftr, Defaultr and Removr: the spec of that one operation. Sortr: sorts the keys of the JSON objects and reads no spec. Not used when the transform option of the component is set.

Enum values:

  • Chainr

  • Shiftr

  • Defaultr

  • Removr

  • Sortr

Chainr

JoltTransformType

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Headers

The JOLT component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelJoltResourceUri (producer)

Constant: JOLT_RESOURCE_URI

The URI of the spec resource to use instead of the resourceUri of the endpoint, in the same form, such as classpath:, file: or http:. The producer reads it only when allowTemplateFromHeader is true, and removes it from the message. It then uses the endpoint with the same options for that URI.

String

CamelJoltContext (producer)

Constant: JOLT_CONTEXT

A Map of values that the modify-overwrite-beta, modify-default-beta and modify-define-beta operations of the spec can refer to with a caret followed by the key. The producer reads it only when allowTemplateFromHeader is true. The transform must take a context: of the transformDsl types only Chainr does, and the others fail the exchange.

Map

Message Body

The message body at each side of the JOLT component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

map, list, text, bytes, stream or file, as json

Parsed JSON (a Map or a List), or JSON as text, bytes, a stream, a file or a Jackson JsonNode, which is parsed. Text that does not start with { or [ reaches the spec as it is. With inputType=JsonString the body is always parsed as JSON.

  • With inputType=JsonString: text, bytes, stream or file

Producer output

map or list, as json

The JSON the spec makes: a Map for an object, a List for an array; JSON text with outputType=JsonString.

  • With outputType=JsonString: text (java.lang.String)

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Usage

Input and output types

With the default inputType, Hydrated, the body can be:

  • JSON text (a String or other text whose first non-whitespace character is { or [), which is parsed as JSON;

  • bytes, an InputStream (including a stream cache) or a file (java.io.File, java.nio.file.Path or the file of the File component), which is parsed as JSON. The text is decoded in the charset of the exchange (Exchange.CHARSET_NAME) when one is set; otherwise the JSON parser detects the encoding;

  • a Jackson JsonNode (Jackson 3 or Jackson 2), such as the body of the application-x-struct data type or of the JSON data format with unmarshalType set to JsonNode, which is converted to a Map or a List from its JSON text;

  • a Map or a List (hydrated JSON), which is used as it is.

Text that does not start with { or [ is passed to the specification as it is. Text that starts with { or [ but is not valid JSON fails with the parse error.

With inputType=JsonString, the body is always parsed as JSON, and a Jackson JsonNode is converted the same way.

The output is hydrated JSON (a Map or a List) by default; set outputType=JsonString to get a JSON String.

Examples

For example, you could use something like

The messages of the queue are JSON text, so the examples set inputType and outputType to JsonString: the body is always parsed as JSON, and the result is JSON text. With the default, Hydrated, the result is parsed JSON (a Map or a List).

  • Java

  • XML

  • YAML

from("activemq:My.Queue")
    .to("jolt:com/acme/MyResponse.json?inputType=JsonString&outputType=JsonString");
<route>
  <from uri="activemq:My.Queue"/>
  <to uri="jolt:com/acme/MyResponse.json?inputType=JsonString&amp;outputType=JsonString"/>
</route>
- route:
    from:
      uri: activemq:My.Queue
      steps:
        - to:
            uri: jolt:com/acme/MyResponse.json
            parameters:
              inputType: JsonString
              outputType: JsonString

And a file-based resource:

  • Java

  • XML

  • YAML

from("activemq:My.Queue")
    .to("jolt:file://myfolder/MyResponse.json?contentCache=true&inputType=JsonString&outputType=JsonString")
    .to("activemq:Another.Queue");
<route>
  <from uri="activemq:My.Queue"/>
  <to uri="jolt:file://myfolder/MyResponse.json?contentCache=true&amp;inputType=JsonString&amp;outputType=JsonString"/>
  <to uri="activemq:Another.Queue"/>
</route>
- route:
    from:
      uri: activemq:My.Queue
      steps:
        - to:
            uri: jolt:file://myfolder/MyResponse.json
            parameters:
              contentCache: true
              inputType: JsonString
              outputType: JsonString
        - to:
            uri: activemq:Another.Queue

You can also specify what specification the component should use dynamically via a header, so, for example:

  • Java

  • XML

  • YAML

from("direct:in")
    .setHeader("CamelJoltResourceUri").constant("path/to/my/spec.json")
    .to("jolt:dummy?allowTemplateFromHeader=true");
<route>
  <from uri="direct:in"/>
  <setHeader name="CamelJoltResourceUri">
    <constant>path/to/my/spec.json</constant>
  </setHeader>
  <to uri="jolt:dummy?allowTemplateFromHeader=true"/>
</route>
- route:
    from:
      uri: direct:in
      steps:
        - setHeader:
            name: CamelJoltResourceUri
            expression:
              constant:
                expression: path/to/my/spec.json
        - to:
            uri: jolt:dummy
            parameters:
              allowTemplateFromHeader: true