このコンテンツは選択した言語では利用できません。
Chapter 9. Message Transformation
Abstract
The message transformation patterns describe how to modify the contents of messages for various purposes.
9.1. Content Enricher リンクのコピーリンクがクリップボードにコピーされました!
Overview リンクのコピーリンクがクリップボードにコピーされました!
The content enricher pattern describes a scenario where the message destination requires more data than is present in the original message. In this case, you would use a message translator, an arbitrary processor in the routing logic, or a content enricher method to pull in the extra data from an external resource.
Figure 9.1. Content Enricher Pattern
Alternatives for enriching content リンクのコピーリンクがクリップボードにコピーされました!
Apache Camel supports several ways to enrich content:
- Message translator with arbitrary processor in the routing logic
-
The
enrich()
method obtains additional data from the resource by sending a copy of the current exchange to a producer endpoint and then using the data in the resulting reply. The exchange created by the enricher is always an InOut exchange. -
The
pollEnrich()
method obtains additional data by polling a consumer endpoint for data. Effectively, the consumer endpoint from the main route and the consumer endpoint inpollEnrich()
operation are coupled. That is, an incoming message on the initial consumer in the route triggers thepollEnrich()
method on the consumer to be polled.
The enrich()
and pollEnrich()
methods support dynamic endpoint URIs. You can compute URIs by specifying an expression that enables you to obtain values from the current exchange. For example, you can poll a file with a name that is computed from the data exchange. This behavior was introduced in Camel 2.16. This change breaks the XML DSL and enables you to migrate easily. The Java DSL stays backwards compatible.
Using message translators and processors to enrich content リンクのコピーリンクがクリップボードにコピーされました!
Camel provides fluent builders for creating routing and mediation rules using a type-safe IDE-friendly way that provides smart completion and is refactoring safe. When you are testing distributed systems it is a very common requirement to have to stub out certain external systems so that you can test other parts of the system until a specific system is available or written. One way to do this is to use some kind of template system to generate responses to requests by generating a dynamic message that has a mostly-static body. Another way to use templates is to consume a message from one destination, transform it with something like Velocity or XQuery, and then send it to another destination. The following example shows this for an InOnly
(one way) message:
from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm"). to("activemq:Another.Queue");
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm").
to("activemq:Another.Queue");
Suppose you want to use InOut (request-reply) messaging to process requests on the My.Queue
queue on ActiveMQ. You want a template-generated response that goes to a JMSReplyTo
destination. The following example shows how to do this:
from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm");
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm");
The following simple example shows how to use DSL to transform the message body:
from("direct:start").setBody(body().append(" World!")).to("mock:result");
from("direct:start").setBody(body().append(" World!")).to("mock:result");
The following example uses explicit Java code to add a processor:
The next example uses bean integration to enable the use of any bean to act as the transformer:
from("activemq:My.Queue"). beanRef("myBeanName", "myMethodName"). to("activemq:Another.Queue");
from("activemq:My.Queue").
beanRef("myBeanName", "myMethodName").
to("activemq:Another.Queue");
The following example shows a Spring XML implementation:
<route> <from uri="activemq:Input"/> <bean ref="myBeanName" method="doTransform"/> <to uri="activemq:Output"/> </route>/>
<route>
<from uri="activemq:Input"/>
<bean ref="myBeanName" method="doTransform"/>
<to uri="activemq:Output"/>
</route>/>
Using the enrich() method to enrich content リンクのコピーリンクがクリップボードにコピーされました!
The content enricher (enrich
) retrieves additional data from a resource endpoint in order to enrich an incoming message (contained in the orginal exchange). An aggregation strategy combines the original exchange and the resource exchange. The first parameter of the AggregationStrategy.aggregate(Exchange, Exchange)
method corresponds to the the original exchange, and the second parameter corresponds to the resource exchange. The results from the resource endpoint are stored in the resource exchange’s Out message. Here is a sample template for implementing your own aggregation strategy class:
Using this template, the original exchange can have any exchange pattern. The resource exchange created by the enricher is always an InOut exchange.
Spring XML enrich example リンクのコピーリンクがクリップボードにコピーされました!
The preceding example can also be implemented in Spring XML:
Default aggregation strategy when enriching content リンクのコピーリンクがクリップボードにコピーされました!
The aggregation strategy is optional. If you do not provide it, Apache Camel will use the body obtained from the resource by default. For example:
from("direct:start") .enrich("direct:resource") .to("direct:result");
from("direct:start")
.enrich("direct:resource")
.to("direct:result");
In the preceding route, the message sent to the direct:result
endpoint contains the output from the direct:resource
, because this example does not use any custom aggregation.
In XML DSL, just omit the strategyRef
attribute, as follows:
<route> <from uri="direct:start"/> <enrich uri="direct:resource"/> <to uri="direct:result"/> </route>
<route>
<from uri="direct:start"/>
<enrich uri="direct:resource"/>
<to uri="direct:result"/>
</route>
Options supported by the enrich() method リンクのコピーリンクがクリップボードにコピーされました!
The enrich
DSL command supports the following options:
Name | Default Value | Description |
| None | Starting with Camel 2.16, this option is required. Specify an expression for configuring the URI of the external service to enrich from. You can use the Simple expression language, the Constant expression language, or any other language that can dynamically compute the URI from values in the current exchange. |
|
These options have been removed. Specify the | |
|
Refers to the endpoint for the external service to enrich from. You must use either | |
|
Refers to an AggregationStrategy to be used to merge the reply from the external service into a single outgoing message. By default, Camel uses the reply from the external service as the outgoing message. You can use a POJO as the | |
|
When using POJOs as the | |
| false |
The default behavior is that the aggregate method is not used if there is no data to enrich. If this option is true then null values are used as the |
| false |
The default behavior is that the aggregate method is not used if there was an exception thrown while trying to retrieve the data to enrich from the resource. Setting this option to |
| false | Starting with Camel 2.16, the default behavior is that the enrich operation does not share the unit of work between the parent exchange and the resource exchange. This means that the resource exchange has its own individual unit of work. For more information, see the documentation for the Splitter pattern. |
|
|
Starting with Camel 2.16, specify this option to configure the cache size for the |
| false | Starting with Camel 2.16, this option indicates whether or not to ignore an endpoint URI that cannot be resolved. The default behavior is that Camel throws an exception that identifies the invalid endpoint URI. |
Specifying an aggregation strategy when using the enrich() method リンクのコピーリンクがクリップボードにコピーされました!
The enrich()
method retrieves additional data from a resource endpoint to enrich an incoming message, which is contained in the original exchange. You can use an aggregation strategy to combine the original exchange and the resource exchange. The first parameter of the AggregationStrategy.aggregate(Exchange, Exchange)
method corresponds to the original exchange. The second parameter corresponds to the resource exchange. The results from the resource endpoint are stored in the resource exchange’s Out
message. For example:
The following code is a template for implementing an aggregation strategy. In an implementation that uses this template, the original exchange can be any message exchange pattern. The resource exchange created by the enricher is always an InOut message exchange pattern.
The following example shows the use of the Spring XML DSL to implement an aggregation strategy:
Using dynamic URIs with enrich() リンクのコピーリンクがクリップボードにコピーされました!
Starting with Camel 2.16, the enrich()
and pollEnrich()
methods support the use of dynamic URIs that are computed based on information from the current exchange. For example, to enrich from an HTTP endpoint where the header with the orderId
key is used as part of the content path of the HTTP URL, you can do something like this:
from("direct:start") .enrich().simple("http:myserver/${header.orderId}/order") .to("direct:result");
from("direct:start")
.enrich().simple("http:myserver/${header.orderId}/order")
.to("direct:result");
Following is the same example in XML DSL:
Using the pollEnrich() method to enrich content リンクのコピーリンクがクリップボードにコピーされました!
The pollEnrich
command treats the resource endpoint as a consumer. Instead of sending an exchange to the resource endpoint, it polls the endpoint. By default, the poll returns immediately, if there is no exchange available from the resource endpoint. For example, the following route reads a file whose name is extracted from the header of an incoming JMS message:
from("activemq:queue:order") .pollEnrich("file://order/data/additional?fileName=orderId") .to("bean:processOrder");
from("activemq:queue:order")
.pollEnrich("file://order/data/additional?fileName=orderId")
.to("bean:processOrder");
You can limit the time to wait for the file to be ready. The following example shows a maximum wait of 20 seconds:
from("activemq:queue:order") .pollEnrich("file://order/data/additional?fileName=orderId", 20000) // timeout is in milliseconds .to("bean:processOrder");
from("activemq:queue:order")
.pollEnrich("file://order/data/additional?fileName=orderId", 20000) // timeout is in milliseconds
.to("bean:processOrder");
You can also specify an aggregation strategy for pollEnrich()
, for example:
.pollEnrich("file://order/data/additional?fileName=orderId", 20000, aggregationStrategy)
.pollEnrich("file://order/data/additional?fileName=orderId", 20000, aggregationStrategy)
The pollEnrich()
method supports consumers that are configured with consumer.bridgeErrorHandler=true
. This lets any exceptions from the poll propagate to the route error handler, which could, for example, retry the poll.
Support for consumer.bridgeErrorHandler=true
is new in Camel 2.18. This behavior is not supported in Camel 2.17.
The resource exchange passed to the aggregation strategy’s aggregate()
method might be null
if the poll times out before an exchange is received.
Polling methods used by pollEnrich() リンクのコピーリンクがクリップボードにコピーされました!
The pollEnrich()
method polls the consumer endpoint by calling one of the following polling methods:
-
receiveNoWait()
(This is the default.) -
receive()
-
receive(long timeout)
The pollEnrich()
command’s timeout argument (specified in milliseconds) determines which method to call, as follows:
-
When the timeout is
0
or not specified,pollEnrich()
callsreceiveNoWait
. -
When the timeout is negative,
pollEnrich()
callsreceive
. -
Otherwise,
pollEnrich()
callsreceive(timeout)
.
If there is no data then the newExchange
in the aggregation strategy is null.
Examples of using the pollEnrich() method リンクのコピーリンクがクリップボードにコピーされました!
The following example shows enrichment of the message by loading the content from the inbox/data.txt
file:
from("direct:start") .pollEnrich("file:inbox?fileName=data.txt") .to("direct:result");
from("direct:start")
.pollEnrich("file:inbox?fileName=data.txt")
.to("direct:result");
Following is the same example in XML DSL:
If the specified file does not exist then the message is empty. You can specify a timeout to wait (potentially forever) until a file exists or to wait up to a particular length of time. In the following example, the command waits no more than 5 seconds:
Using dynamic URIs with pollEnrich() リンクのコピーリンクがクリップボードにコピーされました!
Starting with Camel 2.16, the enrich()
and pollEnrich()
methods support the use of dynamic URIs that are computed based on information from the current exchange. For example, to poll enrich from an endpoint that uses a header to indicate a SEDA queue name, you can do something like this:
from("direct:start") .pollEnrich().simple("seda:${header.name}") .to("direct:result");
from("direct:start")
.pollEnrich().simple("seda:${header.name}")
.to("direct:result");
Following is the same example in XML DSL:
Options supported by the pollEnrich() method リンクのコピーリンクがクリップボードにコピーされました!
The pollEnrich
DSL command supports the following options:
Name | Default Value | Description |
| None | Starting with Camel 2.16, this option is required. Specify an expression for configuring the URI of the external service to enrich from. You can use the Simple expression language, the Constant expression language, or any other language that can dynamically compute the URI from values in the current exchange. |
|
These options have been removed. Specify the | |
|
Refers to the endpoint for the external service to enrich from. You must use either | |
|
Refers to an AggregationStrategy to be used to merge the reply from the external service into a single outgoing message. By default, Camel uses the reply from the external service as the outgoing message. You can use a POJO as the | |
|
When using POJOs as the | |
| false |
The default behavior is that the aggregate method is not used if there is no data to enrich. If this option is true then null values are used as the |
|
|
The maximum length of time, in milliseconds, to wait for a response when polling from the external service. The default behavior is that the |
| false |
The default behavior is that the aggregate method is not used if there was an exception thrown while trying to retrieve the data to enrich from the resource. Setting this option to |
|
|
Specify this option to configure the cache size for the |
| false | Indicates whether or not to ignore an endpoint URI that cannot be resolved. The default behavior is that Camel throws an exception that identifies the invalid endpoint URI. |
9.2. Content Filter リンクのコピーリンクがクリップボードにコピーされました!
Overview リンクのコピーリンクがクリップボードにコピーされました!
The content filter pattern describes a scenario where you need to filter out extraneous content from a message before delivering it to its intended recipient. For example, you might employ a content filter to strip out confidential information from a message.
Figure 9.2. Content Filter Pattern
A common way to filter messages is to use an expression in the DSL, written in one of the supported scripting languages (for example, XSLT, XQuery or JoSQL).
Implementing a content filter リンクのコピーリンクがクリップボードにコピーされました!
A content filter is essentially an application of a message processing technique for a particular purpose. To implement a content filter, you can employ any of the following message processing techniques:
- Message translator — see Section 5.6, “Message Translator”.
- Processors — see Chapter 34, Implementing a Processor.
- Bean integration.
XML configuration example リンクのコピーリンクがクリップボードにコピーされました!
The following example shows how to configure the same route in XML:
Using an XPath filter リンクのコピーリンクがクリップボードにコピーされました!
You can also use XPath to filter out part of the message you are interested in:
<route> <from uri="activemq:Input"/> <setBody><xpath resultType="org.w3c.dom.Document">//foo:bar</xpath></setBody> <to uri="activemq:Output"/> </route>
<route>
<from uri="activemq:Input"/>
<setBody><xpath resultType="org.w3c.dom.Document">//foo:bar</xpath></setBody>
<to uri="activemq:Output"/>
</route>
9.3. Normalizer リンクのコピーリンクがクリップボードにコピーされました!
Overview リンクのコピーリンクがクリップボードにコピーされました!
The normalizer pattern is used to process messages that are semantically equivalent, but arrive in different formats. The normalizer transforms the incoming messages into a common format.
In Apache Camel, you can implement the normalizer pattern by combining a Section 8.1, “Content-Based Router”, which detects the incoming message’s format, with a collection of different Section 5.6, “Message Translator”, which transform the different incoming formats into a common format.
Figure 9.3. Normalizer Pattern
Java DSL example リンクのコピーリンクがクリップボードにコピーされました!
This example shows a Message Normalizer that converts two types of XML messages into a common format. Messages in this common format are then filtered.
Using the Fluent Builders
In this case we’re using a Java bean as the normalizer. The class looks like this
XML configuration example リンクのコピーリンクがクリップボードにコピーされました!
The same example in the XML DSL
9.4. Claim Check リンクのコピーリンクがクリップボードにコピーされました!
Claim Check リンクのコピーリンクがクリップボードにコピーされました!
The claim check pattern, shown in Figure 9.4, “Claim Check Pattern”, allows you to replace message content with a claim check (a unique key), which can be used to retrieve the message content at a later time. The message content is stored temporarily in a persistent store like a database or file system. This pattern is very useful when message content is very large (thus it would be expensive to send around) and not all components require all information.
It can also be useful in situations where you cannot trust the information with an outside party; in this case, you can use the Claim Check to hide the sensitive portions of data.
Figure 9.4. Claim Check Pattern
Java DSL example リンクのコピーリンクがクリップボードにコピーされました!
The following example shows how to replace a message body with a claim check and restore the body at a later step.
from("direct:start").to("bean:checkLuggage", "mock:testCheckpoint", "bean:dataEnricher", "mock:result");
from("direct:start").to("bean:checkLuggage", "mock:testCheckpoint", "bean:dataEnricher", "mock:result");
The next step in the pipeline is the mock:testCheckpoint
endpoint, which checks that the message body has been removed, the claim check added, and so on.
XML DSL example リンクのコピーリンクがクリップボードにコピーされました!
The preceding example can also be written in XML, as follows:
checkLuggage bean リンクのコピーリンクがクリップボードにコピーされました!
The message is first sent to the checkLuggage
bean which is implemented as follows:
This bean stores the message body into the data store, using the custId
as the claim check. In this example, we are using a HashMap
to store the message body; in a real application you would use a database or the file system. The claim check is added as a message header for later use and, finally, we remove the body from the message and pass it down the pipeline.
testCheckpoint endpoint リンクのコピーリンクがクリップボードにコピーされました!
The example route is just a Section 5.4, “Pipes and Filters”. In a real application, you would substitute some other steps for the mock:testCheckpoint
endpoint.
dataEnricher bean リンクのコピーリンクがクリップボードにコピーされました!
To add the message body back into the message, we use the dataEnricher
bean, which is implemented as follows:
This bean queries the data store, using the claim check as the key, and then adds the recovered data back into the message body. The bean then deletes the message data from the data store and removes the claimCheck
header from the message.
9.5. Sort リンクのコピーリンクがクリップボードにコピーされました!
Sort リンクのコピーリンクがクリップボードにコピーされました!
The sort pattern is used to sort the contents of a message body, assuming that the message body contains a list of items that can be sorted.
By default, the contents of the message are sorted using a default comparator that handles numeric values or strings. You can provide your own comparator and you can specify an expression that returns the list to be sorted (the expression must be convertible to java.util.List
).
Java DSL example リンクのコピーリンクがクリップボードにコピーされました!
The following example generates the list of items to sort by tokenizing on the line break character:
from("file://inbox").sort(body().tokenize("\n")).to("bean:MyServiceBean.processLine");
from("file://inbox").sort(body().tokenize("\n")).to("bean:MyServiceBean.processLine");
You can pass in your own comparator as the second argument to sort()
:
from("file://inbox").sort(body().tokenize("\n"), new MyReverseComparator()).to("bean:MyServiceBean.processLine");
from("file://inbox").sort(body().tokenize("\n"), new MyReverseComparator()).to("bean:MyServiceBean.processLine");
XML configuration example リンクのコピーリンクがクリップボードにコピーされました!
You can configure the same routes in Spring XML.
The following example generates the list of items to sort by tokenizing on the line break character:
And to use a custom comparator, you can reference it as a Spring bean:
Besides <simple>
, you can supply an expression using any language you like, so long as it returns a list.
Options リンクのコピーリンクがクリップボードにコピーされました!
The sort
DSL command supports the following options:
Name | Default Value | Description |
|
Refers to a custom |
9.6. Transformer リンクのコピーリンクがクリップボードにコピーされました!
Transformer performs declarative transformation of the message according to the declared Input Type
and/or Output Type
on a route definition. The default camel message implements DataTypeAware
, which holds the message type represented by DataType
.
9.6.1. How the Transformer works? リンクのコピーリンクがクリップボードにコピーされました!
The route definition declares the Input Type
and/or Output Type
. If the Input Type
and/or Output Type
are different from the message type at runtime, the camel internal processor looks for a Transformer. The Transformer transforms the current message type to the expected message type. Once the message is transformed successfully or if the message is already in expected type, then the message data type is updated.
9.6.1.1. Data type format リンクのコピーリンクがクリップボードにコピーされました!
The format for the data type is scheme:name
, where scheme is the type of data model such as java
, xml
or json
and name is the data type name.
If you only specify scheme then it matches all the data types with that scheme.
9.6.1.2. Supported Transformers リンクのコピーリンクがクリップボードにコピーされました!
Transformer | Description |
---|---|
Data Format Transformer | Transforms by using Data Format |
Endpoint Transformer | Transforms by using Endpoint |
Custom Transformer | Transforms by using custom transformer class. |
9.6.1.3. Common Options リンクのコピーリンクがクリップボードにコピーされました!
All transformers have the following common options to specify the supported data type by the transformer.
Either scheme
or both fromType
and toType
must be specified.
9.6.1.4. DataFormat Transformer Options リンクのコピーリンクがクリップボードにコピーされました!
Name | Description |
---|---|
type | Data Format type |
ref | Reference to the Data Format ID |
An example to specify bindy
DataFormat type:
Java DSL:
XML DSL:
<dataFormatTransformer fromType="java:com.example.Order" toType="csv:CSVOrder"> <bindy id="csvdf" type="Csv" classType="com.example.Order"/> </dataFormatTransformer>
<dataFormatTransformer fromType="java:com.example.Order" toType="csv:CSVOrder">
<bindy id="csvdf" type="Csv" classType="com.example.Order"/>
</dataFormatTransformer>
9.6.2. Endpoint Transformer Options リンクのコピーリンクがクリップボードにコピーされました!
Name | Description |
---|---|
ref | Reference to the Endpoint ID |
uri | Endpoint URI |
An example to specify endpoint URI in Java DSL:
transformer() .fromType("xml") .toType("json") .withUri("dozer:myDozer?mappingFile=myMapping.xml...");
transformer()
.fromType("xml")
.toType("json")
.withUri("dozer:myDozer?mappingFile=myMapping.xml...");
An example to specify endpoint ref in XML DSL:
<transformers> <endpointTransformer ref="myDozerEndpoint" fromType="xml" toType="json"/> </transformers>
<transformers>
<endpointTransformer ref="myDozerEndpoint" fromType="xml" toType="json"/>
</transformers>
9.6.3. Custom Transformer Options リンクのコピーリンクがクリップボードにコピーされました!
Transformer must be a subclass of org.apache.camel.spi.Transformer
Name | Description |
---|---|
ref | Reference to the custom Transformer bean ID |
className | Fully qualified class name of the custom Transformer class |
An example to specify custom Transformer class:
Java DSL:
transformer() .fromType("xml") .toType("json") .withJava(com.example.MyCustomTransformer.class);
transformer()
.fromType("xml")
.toType("json")
.withJava(com.example.MyCustomTransformer.class);
XML DSL:
<transformers> <customTransformer className="com.example.MyCustomTransformer" fromType="xml" toType="json"/> </transformers>
<transformers>
<customTransformer className="com.example.MyCustomTransformer" fromType="xml" toType="json"/>
</transformers>
9.6.4. Transformer Example リンクのコピーリンクがクリップボードにコピーされました!
This example is in two parts, the first part declares the Endpoint Transformer which transforms the message. The second part shows how the transformer is applied to a route.
9.6.4.1. Part I リンクのコピーリンクがクリップボードにコピーされました!
Declares the Endpoint Transformer which uses xslt component to transform from xml:ABCOrder
to xml:XYZOrder
.
Java DSL:
transformer() .fromType("xml:ABCOrder") .toType("xml:XYZOrder") .withUri("xslt:transform.xsl");
transformer()
.fromType("xml:ABCOrder")
.toType("xml:XYZOrder")
.withUri("xslt:transform.xsl");
XML DSL:
9.6.4.2. Part II リンクのコピーリンクがクリップボードにコピーされました!
The above transformer is applied to the following route definition when direct:abc
endpoint sends the message to direct:xyz
:
Java DSL:
XML DSL:
9.7. Validator リンクのコピーリンクがクリップボードにコピーされました!
Validator performs declarative validation of the message according to the declared Input Type
and/or Output Type
on a route definition which declares the expected message type.
The validation is performed only if the validate
attribute on the type declaration is true.
If the validate
attribute is true on an Input Type
and/or Output Type
declaration, camel internal processor looks for a corresponding Validator from the registry.
9.7.1. Data type format リンクのコピーリンクがクリップボードにコピーされました!
The format for the data type is scheme:name
, where scheme is the type of data model such as java
, xml
, or json
and name is the data type name.
9.7.2. Supported Validators リンクのコピーリンクがクリップボードにコピーされました!
Validator | Description |
---|---|
Predicate Validator | Validate by using Expression or Predicate |
Endpoint Validator | Validate by forwarding to the Endpoint to be used with the validation component such as Validation Component or Bean Validation Component. |
Custom Validator |
Validate using custom validator class. Validator must be a subclass of |
9.7.3. Common Option リンクのコピーリンクがクリップボードにコピーされました!
All validators must include the type option that specifies the Data type to validate.
9.7.4. Predicate Validator Option リンクのコピーリンクがクリップボードにコピーされました!
Name | Description |
---|---|
expression | Expression or Predicate to use for validation. |
An example that specifies a validation predicate:
Java DSL:
validator() .type("csv:CSVOrder") .withExpression(bodyAs(String.class).contains("{name:XOrder}"));
validator()
.type("csv:CSVOrder")
.withExpression(bodyAs(String.class).contains("{name:XOrder}"));
XML DSL:
<predicateValidator Type="csv:CSVOrder"> <simple>${body} contains 'name:XOrder'</simple> </predicateValidator>
<predicateValidator Type="csv:CSVOrder">
<simple>${body} contains 'name:XOrder'</simple>
</predicateValidator>
9.7.5. Endpoint Validator Options リンクのコピーリンクがクリップボードにコピーされました!
Name | Description |
---|---|
ref | Reference to the Endpoint ID. |
uri | Endpoint URI. |
An example that specifies endpoint URI in Java DSL:
validator() .type("xml") .withUri("validator:xsd/schema.xsd");
validator()
.type("xml")
.withUri("validator:xsd/schema.xsd");
An example that specifies endpoint ref in XML DSL:
<validators> <endpointValidator uri="validator:xsd/schema.xsd" type="xml"/> </validators>
<validators>
<endpointValidator uri="validator:xsd/schema.xsd" type="xml"/>
</validators>
The Endpoint Validator forwards the message to the specified endpoint. In above example, camel forwards the message to the validator:
endpoint, which is a Validation Component. You can also use a different validation component, such as Bean Validation Component.
9.7.6. Custom Validator Options リンクのコピーリンクがクリップボードにコピーされました!
The Validator must be a subclass of org.apache.camel.spi.Validator
Name | Description |
---|---|
ref | Reference to the custom Validator bean ID. |
className | Fully qualified class name of the custom Validator class. |
An example that specifies custom Validator class:
Java DSL:
validator() .type("json") .withJava(com.example.MyCustomValidator.class);
validator()
.type("json")
.withJava(com.example.MyCustomValidator.class);
XML DSL:
<validators> <customValidator className="com.example.MyCustomValidator" type="json"/> </validators>
<validators>
<customValidator className="com.example.MyCustomValidator" type="json"/>
</validators>
9.7.7. Validator Examples リンクのコピーリンクがクリップボードにコピーされました!
This example is in two parts, the first part declares the Endpoint Validator which validates the message. The second part shows how the validator is applied to a route.
9.7.7.1. Part I リンクのコピーリンクがクリップボードにコピーされました!
Declares the Endpoint Validator which uses validator component to validate from xml:ABCOrder
.
Java DSL:
validator() .type("xml:ABCOrder") .withUri("validator:xsd/schema.xsd");
validator()
.type("xml:ABCOrder")
.withUri("validator:xsd/schema.xsd");
XML DSL:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <validators> <endpointValidator uri="validator:xsd/schema.xsd" type="xml:ABCOrder"/> </validators> </camelContext>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<validators>
<endpointValidator uri="validator:xsd/schema.xsd" type="xml:ABCOrder"/>
</validators>
</camelContext>
9.7.7.2. Part II リンクのコピーリンクがクリップボードにコピーされました!
The above validator is applied to the following route definition when direct:abc
endpoint receives the message.
The inputTypeWithValidate
is used instead of inputType
in Java DSL, and the validate
attribute on the inputType declaration is set to true
in XML DSL:
Java DSL:
from("direct:abc") .inputTypeWithValidate("xml:ABCOrder") .log("${body}");
from("direct:abc")
.inputTypeWithValidate("xml:ABCOrder")
.log("${body}");
XML DSL:
9.8. Validate リンクのコピーリンクがクリップボードにコピーされました!
Overview リンクのコピーリンクがクリップボードにコピーされました!
The validate pattern provides a convenient syntax to check whether the content of a message is valid. The validate DSL command takes a predicate expression as its sole argument: if the predicate evaluates to true
, the route continues processing normally; if the predicate evaluates to false
, a PredicateValidationException
is thrown.
Java DSL example リンクのコピーリンクがクリップボードにコピーされました!
The following route validates the body of the current message using a regular expression:
from("jms:queue:incoming") .validate(body(String.class).regex("^\\w{10}\\,\\d{2}\\,\\w{24}$")) .to("bean:MyServiceBean.processLine");
from("jms:queue:incoming")
.validate(body(String.class).regex("^\\w{10}\\,\\d{2}\\,\\w{24}$"))
.to("bean:MyServiceBean.processLine");
You can also validate a message header — for example:
from("jms:queue:incoming") .validate(header("bar").isGreaterThan(100)) .to("bean:MyServiceBean.processLine");
from("jms:queue:incoming")
.validate(header("bar").isGreaterThan(100))
.to("bean:MyServiceBean.processLine");
And you can use validate with the simple expression language:
from("jms:queue:incoming") .validate(simple("${in.header.bar} == 100")) .to("bean:MyServiceBean.processLine");
from("jms:queue:incoming")
.validate(simple("${in.header.bar} == 100"))
.to("bean:MyServiceBean.processLine");
XML DSL example リンクのコピーリンクがクリップボードにコピーされました!
To use validate in the XML DSL, the recommended approach is to use the simple expression language:
You can also validate a message header — for example: