This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch 8958 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 148a16f2867e3fd8580e3e206817edd6737c6dd8 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Feb 5 09:38:51 2018 +0100 CAMEL-8958: Claim Check EIP with push/pop. Work in progress. --- .../camel/impl/DefaultClaimCheckRepository.java | 8 ++-- .../apache/camel/model/ClaimCheckDefinition.java | 40 ++++++++++++++++---- .../processor/ClaimCheckAggregationStrategy.java | 44 ++++++++++++++++++---- .../camel/processor/ClaimCheckProcessor.java | 9 +++-- .../ClaimCheckEipGetAndRemoveSetTest.java | 6 +-- .../camel/processor/ClaimCheckEipGetSetTest.java | 6 +-- .../processor/ClaimCheckEipPushPopBodyTest.java | 6 +-- ...=> ClaimCheckEipPushPopHeadersPatternTest.java} | 32 ++++++++++++---- .../processor/ClaimCheckEipPushPopHeadersTest.java | 6 +-- .../camel/processor/ClaimCheckEipPushPopTest.java | 6 +-- 10 files changed, 120 insertions(+), 43 deletions(-) diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultClaimCheckRepository.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultClaimCheckRepository.java index adac797..91e1db2 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultClaimCheckRepository.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultClaimCheckRepository.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -26,7 +26,7 @@ import org.apache.camel.spi.ClaimCheckRepository; public class DefaultClaimCheckRepository implements ClaimCheckRepository { - private final Map<String, Exchange> map = new HashMap(); + private final Map<String, Exchange> map = new HashMap<>(); private final Deque<Exchange> stack = new ArrayDeque<>(); @Override diff --git a/camel-core/src/main/java/org/apache/camel/model/ClaimCheckDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ClaimCheckDefinition.java index 8db7fab..899c317 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ClaimCheckDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ClaimCheckDefinition.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -16,7 +16,10 @@ */ package org.apache.camel.model; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import org.apache.camel.CamelContextAware; @@ -24,9 +27,14 @@ import org.apache.camel.Processor; import org.apache.camel.processor.ClaimCheckProcessor; import org.apache.camel.processor.aggregate.AggregationStrategy; import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter; +import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RouteContext; +import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.ObjectHelper; +@Metadata(label = "eip,routing") +@XmlRootElement(name = "claimCheck") +@XmlAccessorType(XmlAccessType.FIELD) public class ClaimCheckDefinition extends NoOutputDefinition<ClaimCheckDefinition> { @XmlAttribute(required = true) @@ -35,9 +43,9 @@ public class ClaimCheckDefinition extends NoOutputDefinition<ClaimCheckDefinitio private String key; @XmlAttribute private String data; - @XmlAttribute(name = "strategyRef") + @XmlAttribute(name = "strategyRef") @Metadata(label = "advanced") private String aggregationStrategyRef; - @XmlAttribute(name = "strategyMethodName") + @XmlAttribute(name = "strategyMethodName") @Metadata(label = "advanced") private String aggregationStrategyMethodName; @XmlTransient private AggregationStrategy aggregationStrategy; @@ -101,7 +109,7 @@ public class ClaimCheckDefinition extends NoOutputDefinition<ClaimCheckDefinitio //------------------------------------------------------------------------- /** - * The claim check operation. + * The claim check operation to use. */ public ClaimCheckDefinition operation(ClaimCheckOperation operation) { setOperation(operation); @@ -118,18 +126,36 @@ public class ClaimCheckDefinition extends NoOutputDefinition<ClaimCheckDefinitio /** * What data to merge when claiming from the repository. - * // TODO: add more description here about the syntax + * + * The following syntax is supported: + * <ul> + * <li>body</li> - to aggregate the message body + * <li>headers</li> - to aggregate all the message headers + * <li>header:pattern</li> - to aggregate all the message headers that matches the pattern. + * The pattern syntax is documented by: {@link EndpointHelper#matchPattern(String, String)}. + * </ul> + * You can specify multiple rules separated by comma. For example to include the message body and all headers starting with foo + * <tt>body,header:foo*</tt>. + * If the data rule is specified as empty or as wildcard then everything is merged. */ public ClaimCheckDefinition data(String data) { setData(data); return this; } + /** + * To use a custom {@link AggregationStrategy} instead of the default implementation. + * Notice you cannot use both custom aggregation strategy and configure data at the same time. + */ public ClaimCheckDefinition aggregationStrategy(AggregationStrategy aggregationStrategy) { setAggregationStrategy(aggregationStrategy); return this; } + /** + * To use a custom {@link AggregationStrategy} instead of the default implementation. + * Notice you cannot use both custom aggregation strategy and configure data at the same time. + */ public ClaimCheckDefinition aggregationStrategyRef(String aggregationStrategyRef) { setAggregationStrategyRef(aggregationStrategyRef); return this; diff --git a/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java b/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java index c22cde9..80b9bc0 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java +++ b/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -16,14 +16,31 @@ */ package org.apache.camel.processor; +import java.util.Map; + import org.apache.camel.Exchange; import org.apache.camel.processor.aggregate.AggregationStrategy; +import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; +/** + * Default {@link AggregationStrategy} used by the {@link ClaimCheckProcessor} EIP. + * <p/> + * This strategy supports the following data rules syntax: + * <ul> + * <li>body</li> - to aggregate the message body + * <li>headers</li> - to aggregate all the message headers + * <li>header:pattern</li> - to aggregate all the message headers that matches the pattern. + * The pattern syntax is documented by: {@link EndpointHelper#matchPattern(String, String)}. + * </ul> + * You can specify multiple rules separated by comma. For example to include the message body and all headers starting with foo + * <tt>body,header:foo*</tt>. + * If the data rule is specified as empty or as wildcard then everything is merged. + */ public class ClaimCheckAggregationStrategy implements AggregationStrategy { - private final String data; - // TODO: pattern matching for headers, eg headers:foo*, headers, headers:*, header:foo,header:bar + private final String data; // describes what data to merge public ClaimCheckAggregationStrategy(String data) { this.data = data; @@ -35,8 +52,8 @@ public class ClaimCheckAggregationStrategy implements AggregationStrategy { return oldExchange; } - if (ObjectHelper.isEmpty(data)) { - // grab everything if data is empty + if (ObjectHelper.isEmpty(data) || "*".equals(data)) { + // grab everything if data is empty or wildcard return newExchange; } @@ -47,6 +64,19 @@ public class ClaimCheckAggregationStrategy implements AggregationStrategy { oldExchange.getMessage().setBody(newExchange.getMessage().getBody()); } else if ("headers".equals(part)) { oldExchange.getMessage().getHeaders().putAll(newExchange.getMessage().getHeaders()); + } else if (part.startsWith("header:")) { + // pattern matching for headers, eg header:foo, header:foo*, header:(foo|bar) + String after = StringHelper.after(part, "header:"); + Iterable i = ObjectHelper.createIterable(after, ","); + for (Object o : i) { + String pattern = o.toString(); + for (Map.Entry<String, Object> header : newExchange.getMessage().getHeaders().entrySet()) { + String key = header.getKey(); + if (EndpointHelper.matchPattern(key, pattern)) { + oldExchange.getMessage().getHeaders().put(key, header.getValue()); + } + } + } } } diff --git a/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckProcessor.java index a9cac9a..4ebfdf4 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/ClaimCheckProcessor.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -33,6 +33,9 @@ import org.apache.camel.util.ServiceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * ClaimCheck EIP implementation. + */ public class ClaimCheckProcessor extends ServiceSupport implements AsyncProcessor, IdAware, CamelContextAware { private static final Logger LOG = LoggerFactory.getLogger(ClaimCheckProcessor.class); diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetAndRemoveSetTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetAndRemoveSetTest.java index 4ba7a37..4c15369 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetAndRemoveSetTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetAndRemoveSetTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetSetTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetSetTest.java index 893040e..4714489 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetSetTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipGetSetTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopBodyTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopBodyTest.java index 59529bb..045a1ea 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopBodyTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopBodyTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersPatternTest.java similarity index 66% copy from camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java copy to camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersPatternTest.java index 3cf1e4f..6ab71a7 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersPatternTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -16,21 +16,38 @@ */ package org.apache.camel.processor; +import java.util.HashMap; +import java.util.Map; + import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.ClaimCheckOperation; -public class ClaimCheckEipPushPopHeadersTest extends ContextTestSupport { +public class ClaimCheckEipPushPopHeadersPatternTest extends ContextTestSupport { + + public void testPushPopHeadersPattern() throws Exception { + Map<String, Object> headers = new HashMap<>(); + headers.put("foo", 123); + headers.put("bar", "Moes"); + headers.put("car", "Toyota"); - public void testPushPopHeaders() throws Exception { getMockEndpoint("mock:a").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:a").expectedHeaderReceived("foo", 123); + getMockEndpoint("mock:a").expectedHeaderReceived("bar", "Moes"); + getMockEndpoint("mock:a").message(0).header("car").isEqualTo("Toyota"); + getMockEndpoint("mock:b").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:b").expectedHeaderReceived("foo", 456); + getMockEndpoint("mock:b").message(0).header("bar").isNull(); + getMockEndpoint("mock:b").message(0).header("car").isEqualTo("Toyota"); + getMockEndpoint("mock:c").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:c").expectedHeaderReceived("foo", 123); + // bar header should be back now + getMockEndpoint("mock:c").expectedHeaderReceived("bar", "Moes"); + getMockEndpoint("mock:c").message(0).header("car").isEqualTo("Toyota"); - template.sendBodyAndHeader("direct:start", "Hello World", "foo", 123); + template.sendBodyAndHeaders("direct:start", "Hello World", headers); assertMockEndpointsSatisfied(); } @@ -45,9 +62,10 @@ public class ClaimCheckEipPushPopHeadersTest extends ContextTestSupport { .claimCheck(ClaimCheckOperation.push) .transform().constant("Bye World") .setHeader("foo", constant(456)) + .removeHeader("bar") .to("mock:b") // only merge in the message headers - .claimCheck(ClaimCheckOperation.pop, null, "headers") + .claimCheck(ClaimCheckOperation.pop, null, "header:(foo|bar)") .to("mock:c"); } }; diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java index 3cf1e4f..354afce 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopHeadersTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. diff --git a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopTest.java b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopTest.java index 9e50e23..fc18b29 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/ClaimCheckEipPushPopTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. -- To stop receiving notification emails like this one, please contact [email protected].
