Camel Components

JSLT

Since Camel 3.1

Only producer is supported

The JSLT component allows you to process JSON messages using an JSLT expression. This can be ideal when doing JSON to JSON transformation or querying data.

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

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

URI format

jslt:resourceUri[?options]

Where resourceUri is the classpath-local URI of the JSLT expression to apply; or the complete URL of the remote JSLT expression (e.g.: file://folder/myfile.json).

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 JSLT component supports the following options which are listed below.

Name Description Default Type

allowTemplateFromHeader (producer)

Whether the producer reads the CamelJsltString and CamelJsltResourceUri headers. If false, the producer ignores both headers. A JSLT expression from a header can do all that JSLT allows, such as calling the configured functions and importing modules from the classpath, and the CamelJsltResourceUri header can name any resource, such as a file or an http URL. Enable it only when the route sets these headers, not a message sender.

false

boolean

contentCache (producer)

Sets whether to use resource content cache or not.

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

functions (advanced)

Custom JSLT functions, implementations of the JSLT Function interface, that the JSLT expressions of all endpoints of this component can call.

Collection

objectFilter (advanced)

The JSLT JsonFilter that decides which key/value pairs the object constructors of the JSLT expression keep in the output. If not set, the DefaultJsonFilter of JSLT is used, which leaves out the pairs whose value is null, an empty object or an empty array.

JsonFilter

Endpoint Options

The JSLT endpoint is configured using URI syntax:

jslt: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

allowContextMapAll (producer)

Sets whether the context map should allow access to all details. By default only the message body and headers can be accessed. This option can be enabled for full access to the current Exchange and CamelContext. Doing so impose a potential security risk as this opens access to the full power of CamelContext API.

false

boolean

allowTemplateFromHeader (producer)

Whether the producer reads the CamelJsltString and CamelJsltResourceUri headers. If false, the producer ignores both headers. A JSLT expression from a header can do all that JSLT allows, such as calling the configured functions and importing modules from the classpath, and the CamelJsltResourceUri header can name any resource, such as a file or an http URL. Enable it only when the route sets these headers, not a message sender.

false

boolean

contentCache (producer)

Sets whether to use resource content cache or not.

true

boolean

mapBigDecimalAsFloats (producer)

Whether to read the floating-point numbers of the input JSON as BigDecimal instead of Double (the Jackson DeserializationFeature USE_BIG_DECIMAL_FOR_FLOATS), so that the numbers the JSLT expression copies unchanged keep their exact decimal value. The arithmetic operators of JSLT still compute with double. If an objectMapper is configured, the feature is enabled on that ObjectMapper.

false

boolean

objectMapper (producer)

The Jackson ObjectMapper that parses the message body into the input JSON of the JSLT expression. It is not used for the headers, variables or the result. If not set, a new ObjectMapper with the default settings is used for each message.

ObjectMapper

prettyPrint (common)

If true, JSON in output message is pretty printed.

false

boolean

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 JSLT component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelJsltString (producer)

Constant: HEADER_JSLT_STRING

The text of a JSLT expression that the producer applies in place of the one of resourceUri. Only used when allowTemplateFromHeader is true. The producer compiles the expression for each message and does not cache it.

String

CamelJsltResourceUri (producer)

Constant: HEADER_JSLT_RESOURCE_URI

The URI of a JSLT expression that the producer applies in place of the one of resourceUri, in the same form as resourceUri. Only used when allowTemplateFromHeader is true. The producer removes the header and hands the exchange to the jslt endpoint of that URI, with the same options as this endpoint, which applies the text of a CamelJsltString header instead if one is set.

String

Message Body

The message body at each side of the JSLT 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

text, bytes, stream or file, as json

JSON as text, bytes, a stream, a Reader or a java.io.File, such as the file of the file consumer. A Map, a List, a JsonNode or no body fails: marshal parsed JSON first.

Producer output

text (java.lang.String), as json

The result as JSON text, pretty printed with prettyPrint=true.

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

Passing values to JSLT

Camel can supply exchange information as variables when applying a JSLT expression on the body. The available variables from the Exchange are:

name value

headers

The headers of the In message as a json object

variables

The variables

exchange.properties

The Exchange properties as a json object. exchange is the name of the variable and properties is the path to the exchange properties. Available if allowContextMapAll option is true.

All the values that cannot be converted to json with Jackson are denied and will not be available in the jslt expression.

For example, the header named type and the exchange property instance can be accessed like

{
  "type": $headers.type,
  "instance": $exchange.properties.instance
}

Examples

For example, you could use something like:

  • Java

  • XML

  • YAML

from("activemq:My.Queue")
    .to("jslt:com/acme/MyResponse.json");
<route>
  <from uri="activemq:My.Queue"/>
  <to uri="jslt:com/acme/MyResponse.json"/>
</route>
- route:
    from:
      uri: activemq:My.Queue
      steps:
        - to:
            uri: jslt:com/acme/MyResponse.json

And a file-based resource:

  • Java

  • XML

  • YAML

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

You can also specify which JSLT expression the component should use dynamically via a header, so, for example:

  • Java

  • XML

  • YAML

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

Or send whole jslt expression via header: (suitable for querying)

  • Java

  • XML

  • YAML

from("direct:in")
    .setHeader("CamelJsltString").constant(".published")
    .to("jslt:dummy?allowTemplateFromHeader=true");
<route>
  <from uri="direct:in"/>
  <setHeader name="CamelJsltString">
    <constant>.published</constant>
  </setHeader>
  <to uri="jslt:dummy?allowTemplateFromHeader=true"/>
</route>
- route:
    from:
      uri: direct:in
      steps:
        - setHeader:
            name: CamelJsltString
            expression:
              constant:
                expression: ".published"
        - to:
            uri: jslt:dummy
            parameters:
              allowTemplateFromHeader: true

Passing exchange properties to the jslt expression can be done like this

  • Java

  • XML

  • YAML

from("direct:in")
    .to("jslt:com/acme/MyResponse.json?allowContextMapAll=true");
<route>
  <from uri="direct:in"/>
  <to uri="jslt:com/acme/MyResponse.json?allowContextMapAll=true"/>
</route>
- route:
    from:
      uri: direct:in
      steps:
        - to:
            uri: jslt:com/acme/MyResponse.json
            parameters:
              allowContextMapAll: true