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,*.yamlfiles, 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 |
|---|---|---|---|
Whether to allow to use resource template from header or not (default false). Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential security vulnerability if the header is coming from a malicious user, so use this with care. | false | boolean | |
Sets whether to use resource content cache or not. | true | boolean | |
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 | |
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 | |
Explicitly sets the Transform to use. If not set a Transform specified by the transformDsl will be created. | 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 |
|---|---|---|---|
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 |
|---|---|---|---|
Whether to allow to use resource template from header or not (default false). Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential security vulnerability if the header is coming from a malicious user, so use this with care. | false | boolean | |
Sets whether to use resource content cache or not. | true | boolean | |
Specifies if the input is hydrated JSON or a JSON String. 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. Enum values:
| Hydrated | JoltInputOutputType | |
Specifies if the output should be hydrated JSON or a JSON String. Enum values:
| Hydrated | JoltInputOutputType | |
Specifies the Transform DSL of the endpoint resource. If none is specified Chainr will be used. Enum values:
| Chainr | JoltTransformType | |
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: | The resource URI. | String | |
| Constant: | The context. | 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 |
| Parsed JSON (a Map or a List), or JSON as text, bytes, a stream or a file, 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.
|
Producer output |
| The JSON the spec makes: a Map for an object, a List for an array; JSON text with outputType=JsonString.
|
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 are described in the Camel Catalog.
Usage
Input and output types
With the default inputType, Hydrated, the body can be:
-
JSON text (a
Stringor 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.Pathor 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
Mapor aList(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.
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&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&inputType=JsonString&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