This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 325d81c CAMEL-13731: Implemented StringAggregationStrategy
325d81c is described below
commit 325d81c7dff898742c077cd90a9ccbbac2f11df9
Author: Jan Bednář <[email protected]>
AuthorDate: Sat Jul 6 15:02:30 2019 +0200
CAMEL-13731: Implemented StringAggregationStrategy
---
.../aggregate/StringAggregationStrategy.java | 102 +++++++++++++++++++++
.../camel/builder/AggregationStrategies.java | 18 ++++
.../aggregator/StringAggregationStrategyTest.java | 84 +++++++++++++++++
3 files changed, 204 insertions(+)
diff --git
a/core/camel-base/src/main/java/org/apache/camel/processor/aggregate/StringAggregationStrategy.java
b/core/camel-base/src/main/java/org/apache/camel/processor/aggregate/StringAggregationStrategy.java
new file mode 100644
index 0000000..e98474f
--- /dev/null
+++
b/core/camel-base/src/main/java/org/apache/camel/processor/aggregate/StringAggregationStrategy.java
@@ -0,0 +1,102 @@
+/*
+ * 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.processor.aggregate;
+
+import org.apache.camel.AggregationStrategy;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.support.builder.ExpressionBuilder;
+
+/**
+ * Aggregate result of pick expression into a single combined Exchange holding
all the
+ * aggregated bodies in a {@link String} as the message body.
+ *
+ * This aggregation strategy can used in combination with {@link
org.apache.camel.processor.Splitter} to batch messages
+ * @since 3.0.0
+ */
+public class StringAggregationStrategy implements AggregationStrategy {
+
+ private String delimiter = "";
+ private Expression pickExpression = ExpressionBuilder.bodyExpression();
+
+ /**
+ * Set delimiter used for joining aggregated String
+ * @param delimiter The delimiter to join with. Default empty String
+ * @return
+ */
+ public StringAggregationStrategy delimiter(String delimiter) {
+ this.delimiter = delimiter;
+ return this;
+ }
+
+ /**
+ * Set an expression to extract the element to be aggregated from the
incoming {@link Exchange}.
+ * <p/>
+ * By default, it picks the full IN message body of the incoming exchange.
+ * @param expression The picking expression.
+ * @return This instance.
+ */
+ public StringAggregationStrategy pick(Expression expression) {
+ this.pickExpression = expression;
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+ StringBuffer result; // Aggregate in StringBuffer instead of
StringBuilder, to make it thread safe
+ StringBuilder value = new StringBuilder();
+ if (oldExchange == null) {
+ result = getStringBuffer(newExchange); // do not prepend delimiter
for first invocation
+ } else {
+ result = getStringBuffer(oldExchange);
+ value.append(delimiter);
+ }
+
+ if (newExchange != null) {
+ String pick = pickExpression.evaluate(newExchange, String.class);
+ if (pick != null) {
+ value.append(pick);
+ result.append(value);
+ }
+ }
+
+ return oldExchange != null ? oldExchange : newExchange;
+ }
+
+ @Override
+ public void onCompletion(Exchange exchange) {
+ if (exchange != null) {
+ StringBuffer stringBuffer = (StringBuffer)
exchange.removeProperty(Exchange.GROUPED_EXCHANGE);
+ if (stringBuffer != null) {
+ exchange.getIn().setBody(stringBuffer.toString());
+ }
+ }
+ }
+
+ private static StringBuffer getStringBuffer(Exchange exchange) {
+ StringBuffer stringBuffer =
exchange.getProperty(Exchange.GROUPED_EXCHANGE, StringBuffer.class);
+ if (stringBuffer == null) {
+ stringBuffer = new StringBuffer();
+ exchange.setProperty(Exchange.GROUPED_EXCHANGE, stringBuffer);
+ }
+ return stringBuffer;
+ }
+
+}
diff --git
a/core/camel-core/src/main/java/org/apache/camel/builder/AggregationStrategies.java
b/core/camel-core/src/main/java/org/apache/camel/builder/AggregationStrategies.java
index 1909b3c..01c569e 100644
---
a/core/camel-core/src/main/java/org/apache/camel/builder/AggregationStrategies.java
+++
b/core/camel-core/src/main/java/org/apache/camel/builder/AggregationStrategies.java
@@ -20,6 +20,7 @@ import org.apache.camel.AggregationStrategy;
import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
import org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy;
import org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy;
+import org.apache.camel.processor.aggregate.StringAggregationStrategy;
import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
import org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy;
@@ -142,4 +143,21 @@ public final class AggregationStrategies {
return adapter;
}
+ /**
+ * Creates a {@link StringAggregationStrategy}.
+ * @since 3.0.0
+ */
+ public static StringAggregationStrategy string() {
+ return new StringAggregationStrategy();
+ }
+
+ /**
+ * Creates a {@link StringAggregationStrategy} with delimiter.
+ * @param delimiter The delimiter to join with.
+ * @since 3.0.0
+ */
+ public static StringAggregationStrategy string(String delimiter) {
+ return string().delimiter(delimiter);
+ }
+
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/StringAggregationStrategyTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/StringAggregationStrategyTest.java
new file mode 100644
index 0000000..825b938
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/StringAggregationStrategyTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.processor.aggregator;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.AggregationStrategies;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class StringAggregationStrategyTest extends ContextTestSupport {
+
+ @Test
+ public void testAggregateString() throws Exception {
+ // for testing to string type conversion inside aggregator
+ Object objectBody = new Object();
+ Object objectHeader = new Object();
+
+
getMockEndpoint("mock:aggregatedBody").expectedBodiesReceived("bodyAbodyB" +
objectBody.toString());
+
getMockEndpoint("mock:aggregatedBodyComma").expectedBodiesReceived("bodyA,
bodyB, " + objectBody.toString());
+
getMockEndpoint("mock:aggregatedBodyLines").expectedBodiesReceived("bodyA\nbodyB\n"
+ objectBody.toString());
+
+
getMockEndpoint("mock:aggregatedHeader").expectedBodiesReceived("headerAheaderB"
+ objectHeader.toString());
+
getMockEndpoint("mock:aggregatedHeaderComma").expectedBodiesReceived("headerA,
headerB, " + objectHeader.toString());
+
getMockEndpoint("mock:aggregatedHeaderLines").expectedBodiesReceived("headerA\nheaderB\n"
+ objectHeader.toString());
+
+ template.sendBodyAndHeader("direct:start", "bodyA", "header",
"headerA");
+ template.sendBodyAndHeader("direct:start", "bodyB", "header",
"headerB");
+ template.sendBodyAndHeader("direct:start", objectBody, "header",
objectHeader);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to(
+ "direct:aggregateBody", "direct:aggregateBodyComma",
"direct:aggregateBodyLines",
+ "direct:aggregateHeader",
"direct:aggregateHeaderComma", "direct:aggregateHeaderLines"
+ );
+
+ from("direct:aggregateBody")
+ .aggregate(constant(true),
AggregationStrategies.string()).completionSize(3)
+ .to("mock:aggregatedBody");
+
+ from("direct:aggregateBodyComma")
+ .aggregate(constant(true),
AggregationStrategies.string().delimiter(", ")).completionSize(3)
+ .to("mock:aggregatedBodyComma");
+
+ from("direct:aggregateBodyLines")
+ .aggregate(constant(true),
AggregationStrategies.string("\n")).completionSize(3)
+ .to("mock:aggregatedBodyLines");
+
+ from("direct:aggregateHeader")
+ .aggregate(constant(true),
AggregationStrategies.string().pick(header("header"))).completionSize(3)
+ .to("mock:aggregatedHeader");
+
+ from("direct:aggregateHeaderComma")
+ .aggregate(constant(true), AggregationStrategies.string(",
").pick(header("header"))).completionSize(3)
+ .to("mock:aggregatedHeaderComma");
+
+ from("direct:aggregateHeaderLines")
+ .aggregate(constant(true),
AggregationStrategies.string().pick(header("header")).delimiter("\n")).completionSize(3)
+ .to("mock:aggregatedHeaderLines");
+ }
+ };
+ }
+}