This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new ea2a039 CAMEL-16861: Polished mvel language ea2a039 is described below commit ea2a039061de5e444cc4878b6c63ef72f96f6668 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Sep 14 09:42:29 2021 +0200 CAMEL-16861: Polished mvel language --- .../camel-mvel/src/main/docs/mvel-component.adoc | 41 +++----- .../camel-mvel/src/main/docs/mvel-language.adoc | 112 ++++++++++----------- .../org/apache/camel/language/mvel/RootObject.java | 20 ++-- .../apache/camel/language/mvel/MvelFilterTest.java | 48 +++++++++ .../camel-mvel/src/test/resources/jndi.properties | 18 ---- 5 files changed, 133 insertions(+), 106 deletions(-) diff --git a/components/camel-mvel/src/main/docs/mvel-component.adoc b/components/camel-mvel/src/main/docs/mvel-component.adoc index 0deb60b..9efa752 100644 --- a/components/camel-mvel/src/main/docs/mvel-component.adoc +++ b/components/camel-mvel/src/main/docs/mvel-component.adoc @@ -14,7 +14,7 @@ include::{cq-version}@camel-quarkus:ROOT:partial$reference/components/mvel.adoc[ The MVEL component allows you to process a message using an http://mvel.documentnode.com/[MVEL] template. This can be ideal when using -Templating to generate responses for requests. +templating to generate responses for requests. Maven users will need to add the following dependency to their `pom.xml` for this component: @@ -132,7 +132,7 @@ with the following path and query parameters: == Message Headers -The mvel component sets a couple headers on the message. +The MVEL component sets a couple headers on the message. [width="100%",cols="10%,90%",options="header",] |======================================================================= @@ -144,29 +144,20 @@ The mvel component sets a couple headers on the message. == MVEL Context Camel will provide exchange information in the MVEL context (just a -`Map`). The `Exchange` is transfered as: +`Map`). The `Exchange` is transferred as: -[width="100%",cols="10%,90%",options="header",] +[width="100%",cols="10%m,90%",options="header",] |======================================================================= |key |value - -|`exchange` |The `Exchange` itself. - -|`exchange.properties` |The `Exchange` properties. - -|`headers` |The headers of the In message. - -|`camelContext` |The Camel Context intance. - -|`request` |The In message. - -|`in` |The In message. - -|`body` |The In message body. - -|`out` |The Out message (only for InOut message exchange pattern). - -|`response` |The Out message (only for InOut message exchange pattern). +|exchange |The `Exchange` itself +|exchange.properties |The `Exchange` properties +|headers |The headers of the message +|camelContext |The `CamelContext` +|request |The message +|in |The message +|body |The message body +|out | The Out message (only for InOut message exchange pattern). +|response |The Out message (only for InOut message exchange pattern). |======================================================================= == Hot reloading @@ -180,7 +171,7 @@ never changes. == Dynamic templates Camel provides two headers by which you can define a different resource -location for a template or the template content itself. If any of these +location for a template, or the template content itself. If any of these headers is set then Camel uses this over the endpoint configured resource. This allows you to provide a dynamic template at runtime. @@ -194,9 +185,9 @@ configured. |CamelMvelTemplate |String |The template to use instead of the endpoint configured. |======================================================================= -== Samples +== Example -For example you could use something like +For example, you could use something like [source,java] -------------------------------------- diff --git a/components/camel-mvel/src/main/docs/mvel-language.adoc b/components/camel-mvel/src/main/docs/mvel-language.adoc index be5e60e..d9742bf 100644 --- a/components/camel-mvel/src/main/docs/mvel-language.adoc +++ b/components/camel-mvel/src/main/docs/mvel-language.adoc @@ -9,27 +9,32 @@ include::{cq-version}@camel-quarkus:ROOT:partial$reference/languages/mvel.adoc[o *Since Camel {since}* -Camel allows Mvel to be used as an Expression or -Predicate the DSL or -Xml Configuration. +Camel supports http://mvel.documentnode.com/[MVEL] to do message transformations +using MVEL templates. -You could use Mvel to create an Predicate in a -Message Filter or as an -Expression for a -Recipient List +MVEL is powerful for templates, but can also be used for +xref:manual::expression.adoc[Expression] or xref:manual::predicate.adoc[Predicate] -You can use Mvel dot notation to invoke operations. If you for instance -have a body that contains a POJO that has a `getFamiliyName` method then +For example, you can use MVEL in a xref:manual::predicate.adoc[Predicate] +with the xref:{eip-vc}:eips:choice-eip.adoc[Content Based Router] EIP. + +You can use MVEL dot notation to invoke operations. If you for instance +have a body that contains a POJO that has a `getFamilyName` method then you can construct the syntax as follows: +[source,text] +---- +message.body.familyName +---- + +Or use similar syntax as in Java: + [source,java] ----------------------------------------- -"request.body.familyName" - // or -"getRequest().getBody().getFamilyName()" ----------------------------------------- +---- +getMessage().getBody().getFamilyName() +---- -== Mvel Options +== MVEL Options // language options: START The MVEL language supports 1 options, which are listed below. @@ -49,81 +54,74 @@ The MVEL language supports 1 options, which are listed below. [width="100%",cols="10%,10%,80%",options="header",] |======================================================================= |Variable |Type |Description - |*this* |Exchange |the Exchange is the root object - -|exchange |Exchange |the Exchange object - -|exception |Throwable |the Exchange exception (if any) - +|context |CamelContext |the CamelContext +|exchange |Exchange |the Exchange |exchangeId |String |the exchange id - -|fault |Message |the Fault message (if any) - -|request |Message |the exchange.in message - -|response |Message |the exchange.out message (if any) - +|exception |Throwable |the Exchange exception (if any) +|request |Message |the message +|message |Message |the message +|headers |Map |the message headers +|header(name) |Object |the message header by the given name +|header(name, type) |Type |the message header by the given name as the given type |properties |Map |the exchange properties - -|property(name) |Object |the property by the given name - -|property(name, type) |Type |the property by the given name as the given type +|property(name) |Object |the exchange property by the given name +|property(name, type) |Type |the exchange property by the given name as the given type |======================================================================= -== Samples +== Example -For example you could use Mvel inside a xref:{eip-vc}:eips:filter-eip.adoc[Message -Filter] in XML +For example, you could use MVEL inside a xref:{eip-vc}:eips:filter-eip.adoc[Message +Filter] [source,java] ---------------------------------------------- +---- +from("seda:foo") + .filter().mvel("headers.foo == 'bar'") + .to("seda:bar"); +---- + +And in XML: + +[source,xml] +---- <route> <from uri="seda:foo"/> <filter> - <mvel>request.headers.foo == 'bar'</mvel> + <mvel>headers.foo == 'bar'</mvel> <to uri="seda:bar"/> </filter> </route> ---------------------------------------------- - -And the sample using Java DSL: - -[source,java] ---------------------------------------------------------------------------------- - from("seda:foo").filter().mvel("request.headers.foo == 'bar'").to("seda:bar"); ---------------------------------------------------------------------------------- +---- == Loading script from external resource -*Since Camel 2.11* - You can externalize the script and have Camel load it from a resource -such as `"classpath:"`, `"file:"`, or `"http:"`. + - This is done using the following syntax: `"resource:scheme:location"`, -eg to refer to a file on the classpath you can do: +such as `"classpath:"`, `"file:"`, or `"http:"`. +This is done using the following syntax: `"resource:scheme:location"`, +e.g. to refer to a file on the classpath you can do: [source,java] -------------------------------------------------------------- +---- .setHeader("myHeader").mvel("resource:classpath:script.mvel") -------------------------------------------------------------- +---- == Dependencies -To use Mvel in your camel routes you need to add the a dependency on -*camel-mvel* which implements the Mvel language. +To use MVEL in your camel routes you need to add the dependency on +*camel-mvel* which implements the MVEL language. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). -[source,java] -------------------------------------- +[source,xml] +---- <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-mvel</artifactId> <version>x.x.x</version> </dependency> -------------------------------------- +---- include::{page-component-version}@camel-spring-boot::page$mvel-starter.adoc[] diff --git a/components/camel-mvel/src/main/java/org/apache/camel/language/mvel/RootObject.java b/components/camel-mvel/src/main/java/org/apache/camel/language/mvel/RootObject.java index 0f0b5db..bf2fc79 100644 --- a/components/camel-mvel/src/main/java/org/apache/camel/language/mvel/RootObject.java +++ b/components/camel-mvel/src/main/java/org/apache/camel/language/mvel/RootObject.java @@ -16,7 +16,6 @@ */ package org.apache.camel.language.mvel; -import java.util.Collections; import java.util.Map; import org.apache.camel.CamelContext; @@ -46,10 +45,15 @@ public class RootObject { return exchange.getExchangeId(); } + public Message getMessage() { + return exchange.getMessage(); + } + public Message getRequest() { return exchange.getIn(); } + @Deprecated public Message getResponse() { return exchange.getOut(); } @@ -67,10 +71,14 @@ public class RootObject { } public Map<String, Object> getHeaders() { - Message message = exchange.getMessage(); - if (message != null) { - return message.getHeaders(); - } - return Collections.emptyMap(); + return exchange.getMessage().getHeaders(); + } + + public Object getHeader(String name) { + return exchange.getMessage().getHeader(name); + } + + public <T> T getHeader(String name, Class<T> type) { + return exchange.getMessage().getHeader(name, type); } } diff --git a/components/camel-mvel/src/test/java/org/apache/camel/language/mvel/MvelFilterTest.java b/components/camel-mvel/src/test/java/org/apache/camel/language/mvel/MvelFilterTest.java new file mode 100644 index 0000000..49e1c8e --- /dev/null +++ b/components/camel-mvel/src/test/java/org/apache/camel/language/mvel/MvelFilterTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.language.mvel; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +public class MvelFilterTest extends CamelTestSupport { + + @Test + public void testFilter() throws Exception { + getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World"); + + template.sendBodyAndHeader("direct:foo", "Bye World", "foo", "dude"); + template.sendBodyAndHeader("direct:foo", "Hello World", "foo", "bar"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo") + .filter().mvel("headers.foo == 'bar'") + .to("mock:bar"); + + } + }; + } +} diff --git a/components/camel-mvel/src/test/resources/jndi.properties b/components/camel-mvel/src/test/resources/jndi.properties deleted file mode 100644 index 7675797..0000000 --- a/components/camel-mvel/src/test/resources/jndi.properties +++ /dev/null @@ -1,18 +0,0 @@ -## --------------------------------------------------------------------------- -## Licensed to the Apache Software Foundation (ASF) under one or more -## contributor license agreements. See the NOTICE file distributed with -## this work for additional information regarding copyright ownership. -## The ASF licenses this file to You under the Apache License, Version 2.0 -## (the "License"); you may not use this file except in compliance with -## the License. You may obtain a copy of the License at -## -## http://www.apache.org/licenses/LICENSE-2.0 -## -## Unless required by applicable law or agreed to in writing, software -## distributed under the License is distributed on an "AS IS" BASIS, -## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -## See the License for the specific language governing permissions and -## limitations under the License. -## --------------------------------------------------------------------------- - -java.naming.factory.initial = org.apache.camel.support.jndi.CamelInitialContextFactory