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 6dd1260 CAMEL-16777: camel-core - Add constant language in Java DSL where you can specify trim option. 6dd1260 is described below commit 6dd12607f61026c03fedcbb175da1ec23a56ea9e Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Jul 2 16:50:47 2021 +0200 CAMEL-16777: camel-core - Add constant language in Java DSL where you can specify trim option. --- .../java/org/apache/camel/builder/Builder.java | 15 +++++++ .../org/apache/camel/builder/BuilderSupport.java | 7 ++++ .../org/apache/camel/builder/ExpressionClause.java | 8 ++++ .../camel/builder/ExpressionClauseSupport.java | 14 +++++++ .../apache/camel/language/ConstantTrimTest.java | 46 ++++++++++++++++++++++ 5 files changed, 90 insertions(+) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/Builder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/Builder.java index 447a061..e5f98a3 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/Builder.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/Builder.java @@ -146,6 +146,21 @@ public final class Builder { /** * Returns a constant expression */ + public static ValueBuilder constant(Object value, boolean trim) { + Expression exp; + if (value instanceof String) { + ConstantExpression ce = new ConstantExpression((String) value); + ce.setTrim(trim ? "true" : "false"); + exp = ce; + } else { + exp = ExpressionBuilder.constantExpression(value); + } + return new ValueBuilder(exp); + } + + /** + * Returns a constant expression + */ public static ValueBuilder language(String language, String expression) { Expression exp = new LanguageExpression(language, expression); return new ValueBuilder(exp); diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java index 81d9453..2b2bcfc 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java @@ -104,6 +104,13 @@ public abstract class BuilderSupport { /** * Returns a constant expression value builder */ + public ValueBuilder constant(Object value, boolean trim) { + return Builder.constant(value, trim); + } + + /** + * Returns a constant expression value builder + */ public ValueBuilder constant(Object... value) { return Builder.constant(value); } diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClause.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClause.java index f0f1f75..f35fc61 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClause.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClause.java @@ -63,6 +63,14 @@ public class ExpressionClause<T> implements Expression, Predicate { } /** + * Specify the constant expression value. <b>Important:</b> this is a fixed constant value that is only set once + * during starting up the route, do not use this if you want dynamic values during routing. + */ + public T constant(Object value, boolean trim) { + return delegate.constant(value, trim); + } + + /** * An expression of the exchange */ public T exchange() { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java index 3eadd5b..36b57c1 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java @@ -108,6 +108,20 @@ public class ExpressionClauseSupport<T> implements ExpressionFactoryAware, Predi } /** + * Specify the constant expression value. <b>Important:</b> this is a fixed constant value that is only set once + * during starting up the route, do not use this if you want dynamic values during routing. + */ + public T constant(Object value, boolean trim) { + if (value instanceof String) { + ConstantExpression ce = new ConstantExpression((String) value); + ce.setTrim(trim ? "true" : "false"); + return expression(ce); + } else { + return expression(ExpressionBuilder.constantExpression(value)); + } + } + + /** * An expression of the exchange */ public T exchange() { diff --git a/core/camel-core/src/test/java/org/apache/camel/language/ConstantTrimTest.java b/core/camel-core/src/test/java/org/apache/camel/language/ConstantTrimTest.java new file mode 100644 index 0000000..df51f64 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/language/ConstantTrimTest.java @@ -0,0 +1,46 @@ +/* + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +public class ConstantTrimTest extends ContextTestSupport { + + @Test + public void testTrim() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived(" Hello World "); + + template.sendBody("direct:start", "Start"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .setBody().constant(" Hello World ", false) + .to("mock:result"); + + } + }; + } +}