[
https://issues.apache.org/jira/browse/CAMEL-11532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16266783#comment-16266783
]
ASF GitHub Bot commented on CAMEL-11532:
----------------------------------------
lburgazzoli closed pull request #2114: CAMEL-11532 Java DSL enhancement to
accept supplier and function arguments
URL: https://github.com/apache/camel/pull/2114
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
index 9e3106a9a9f..16714d4e764 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
@@ -20,6 +20,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
+import java.util.function.Supplier;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Message;
@@ -150,6 +151,17 @@ public Object evaluate(Exchange exchange) {
});
}
+ /**
+ * A functional expression of an inbound message body
+ */
+ public T body(final Supplier<Object> supplier) {
+ return delegate.expression(new ExpressionAdapter() {
+ public Object evaluate(Exchange exchange) {
+ return supplier.get();
+ }
+ });
+ }
+
/**
* A functional expression of an inbound message body and headers
*/
diff --git
a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 0ac2474afaa..594f68dd911 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -28,6 +28,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
import java.util.function.Supplier;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -3051,6 +3052,42 @@ public Type setBody(Expression expression) {
return (Type) this;
}
+ /**
+ * <a href="http://camel.apache.org/message-translator.html">Message
Translator EIP:</a>
+ * Adds a processor which sets the body on the IN message
+ *
+ * @param supplier the supplier that provides a value to the IN message
body
+ * @return the builder
+ */
+ public <Result> Type setBody(Supplier<Result> supplier) {
+ SetBodyDefinition answer = new SetBodyDefinition(new
ExpressionAdapter() {
+ @Override
+ public Result evaluate(Exchange exchange) {
+ return supplier.get();
+ }
+ });
+ addOutput(answer);
+ return (Type) this;
+ }
+
+ /**
+ * <a href="http://camel.apache.org/message-translator.html">Message
Translator EIP:</a>
+ * Adds a processor which sets the body on the IN message
+ *
+ * @param function the function that provides a value to the IN message
body
+ * @return the builder
+ */
+ public <Result> Type setBody(Function<Exchange, Result> function) {
+ SetBodyDefinition answer = new SetBodyDefinition(new
ExpressionAdapter() {
+ @Override
+ public Result evaluate(Exchange exchange) {
+ return function.apply(exchange);
+ }
+ });
+ addOutput(answer);
+ return (Type) this;
+ }
+
/**
* <a href="http://camel.apache.org/message-translator.html">Message
Translator EIP:</a>
* Adds a processor which sets the body on the OUT message
diff --git
a/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java
b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java
new file mode 100644
index 00000000000..f104e952081
--- /dev/null
+++
b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.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.builder;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class ExpressionClauseSupplierTest extends ContextTestSupport {
+
+ private static final String BODY_SUPPLIER_MSG = "I am the body supplier!";
+
+ public void testBodySupplier() throws Exception {
+ MockEndpoint functionMock1 = getMockEndpoint("mock:output1");
+ functionMock1.expectedMessageCount(1);
+ functionMock1.expectedBodyReceived().constant(BODY_SUPPLIER_MSG);
+
+ template.sendBody("direct:supplier1", "are you there?");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:supplier1")
+ .transform().body(() -> BODY_SUPPLIER_MSG)
+ .to("mock:output1");
+ }
+ };
+ }
+
+}
diff --git
a/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java
b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java
new file mode 100644
index 00000000000..b0248083557
--- /dev/null
+++
b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.model;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class ProcessDefinitionSetBodyTest extends ContextTestSupport {
+
+ private static final String SUPPLIER_MESSAGE = "Hello from a Supplier!";
+ private static final String FUNCTION_MESSAGE = "Hello from a Function!";
+
+ public void testProcessDefinitionSetBody() throws InterruptedException {
+
+ MockEndpoint functionMock1 = getMockEndpoint("mock:supplierOutput");
+ functionMock1.expectedMessageCount(1);
+ functionMock1.expectedBodyReceived().constant(SUPPLIER_MESSAGE);
+ MockEndpoint functionMock2 = getMockEndpoint("mock:functionOutput");
+ functionMock2.expectedMessageCount(1);
+ functionMock2.expectedBodyReceived().constant(FUNCTION_MESSAGE);
+
+ template.sendBody("direct:start", "are you there?");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .setBody(() -> SUPPLIER_MESSAGE)
+ .to("mock:supplierOutput")
+ .setBody(exchange -> FUNCTION_MESSAGE)
+ .to("mock:functionOutput")
+ .to("mock:output");
+ }
+ };
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> java 8 dsl : allow to set the body using a supplier
> ----------------------------------------------------
>
> Key: CAMEL-11532
> URL: https://issues.apache.org/jira/browse/CAMEL-11532
> Project: Camel
> Issue Type: Improvement
> Components: camel-core
> Reporter: Luca Burgazzoli
> Assignee: Luca Burgazzoli
> Priority: Minor
> Fix For: 2.21.0
>
>
> Example:
> {code:java}
> from("timer")
> .setBody(User::new);
> from("timer")
> .trasform()
> .body(User::new);
> {code}
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)