Repository: camel Updated Branches: refs/heads/camel-2.15.x bfcd55687 -> ae1f2015e refs/heads/master 3fbf4e851 -> 0fa00d30d
FUSETOOLS-1352 - Found issue with camel-dozer loading script files for expression mappings. Added functionality and new tests to camel component Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ad77413b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ad77413b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ad77413b Branch: refs/heads/master Commit: ad77413bfa7e3a4b6a52c201416ec8c44e7959e6 Parents: 3fbf4e8 Author: Brian Fitzpatrick <bfitz...@redhat.com> Authored: Tue Jun 23 11:32:27 2015 -0600 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jun 24 07:58:50 2015 +0200 ---------------------------------------------------------------------- .../camel/component/dozer/ExpressionMapper.java | 83 +++++++++++++++++++- .../dozer/ExpressionMappingClasspathTest.java | 72 +++++++++++++++++ .../dozer/ExpressionMappingFileTest.java | 72 +++++++++++++++++ .../component/dozer/ExpressionMappingTest.java | 1 + .../ExpressionMappingClasspathTest-context.xml | 29 +++++++ .../dozer/ExpressionMappingFileTest-context.xml | 29 +++++++ .../component/dozer/expressionClasspath.txt | 1 + .../camel/component/dozer/expressionFile.txt | 1 + .../dozer/expressionMappingClasspath.xml | 43 ++++++++++ .../component/dozer/expressionMappingFile.xml | 43 ++++++++++ 10 files changed, 373 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/ExpressionMapper.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/ExpressionMapper.java b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/ExpressionMapper.java index afee452..007cc97 100644 --- a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/ExpressionMapper.java +++ b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/ExpressionMapper.java @@ -16,9 +16,14 @@ */ package org.apache.camel.component.dozer; +import java.io.IOException; +import java.io.InputStream; + import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.spi.Language; +import org.apache.camel.util.ResourceHelper; +import org.apache.camel.util.IOHelper; /** * Provides support for mapping a Camel expression to a target field in a @@ -44,7 +49,22 @@ public class ExpressionMapper extends BaseConverter { // Resolve the language being used for this expression and evaluate Exchange exchange = currentExchange.get(); Language expLang = exchange.getContext().resolveLanguage(getLanguagePart()); - Expression exp = expLang.createExpression(getExpressionPart()); + String scheme = getSchemePart(); + Expression exp = null; + if (scheme != null && + (scheme.equalsIgnoreCase("classpath") || + scheme.equalsIgnoreCase("file") || + scheme.equalsIgnoreCase("http"))) { + String path = getPathPart(); + try { + exp = expLang.createExpression(resolveScript(scheme + ":" + path)); + } catch (IOException e) { + throw new IllegalStateException( + "Expression script specified but not found", e); + } + } else { + exp = expLang.createExpression(getExpressionPart()); + } return exp.evaluate(exchange, destinationClass); } finally { done(); @@ -52,6 +72,37 @@ public class ExpressionMapper extends BaseConverter { } /** + * Resolves the script. + * + * @param script script or uri for a script to load + * @return the script + * @throws IOException is thrown if error loading the script + */ + protected String resolveScript(String script) throws IOException { + String answer; + if (ResourceHelper.hasScheme(script)) { + InputStream is = loadResource(script); + answer = currentExchange.get().getContext().getTypeConverter().convertTo(String.class, is); + IOHelper.close(is); + } else { + answer = script; + } + + return answer; + } + + /** + * Loads the given resource. + * + * @param uri uri of the resource. + * @return the loaded resource + * @throws IOException is thrown if resource is not found or cannot be loaded + */ + protected InputStream loadResource(String uri) throws IOException { + return ResourceHelper.resolveMandatoryResourceAsInputStream(currentExchange.get().getContext().getClassResolver(), uri); + } + + /** * Used as the source field for Dozer mappings. */ public String getExpression() { @@ -81,4 +132,34 @@ public class ExpressionMapper extends BaseConverter { public void setCurrentExchange(Exchange exchange) { currentExchange.set(exchange); } + + /** + * The scheme used for this mapping's resource file (classpath, file, http). + */ + public String getSchemePart() { + return getParameterPart(":", 1); + } + + /** + * The path used for this mapping's resource file. + */ + public String getPathPart() { + return getParameterPart(":", 2); + } + + /* + * Parse the URI to get at one of the parameters. + * @param separator + * @param idx + * @return + */ + private String getParameterPart(String separator, int idx) { + String part = null; + String[] parts = getParameter().split(separator); + if (parts.length > idx) { + part = parts[idx]; + } + return part; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingClasspathTest.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingClasspathTest.java b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingClasspathTest.java new file mode 100644 index 0000000..37ddf59 --- /dev/null +++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingClasspathTest.java @@ -0,0 +1,72 @@ +/** + * 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.component.dozer; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.dozer.example.abc.ABCOrder; +import org.apache.camel.component.dozer.example.xyz.XYZOrder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class ExpressionMappingClasspathTest { + + @EndpointInject(uri = "mock:result") + private MockEndpoint resultEndpoint; + + @Produce(uri = "direct:start") + private ProducerTemplate startEndpoint; + + @Autowired + private CamelContext camelContext; + + @After + public void tearDown() { + resultEndpoint.reset(); + } + + @Test + public void testExpressionMappingScript() throws Exception { + resultEndpoint.expectedMessageCount(1); + Map<String, Object> headers = new HashMap<String, Object>(); + final String customerNumber = "CAFE-345"; + final String orderNumber = "ABC-001"; + headers.put("customerNumber", customerNumber); + headers.put("orderNumber", orderNumber); + ABCOrder abcOrder = new ABCOrder(); + // Header value should be mapped to custId in target model + startEndpoint.sendBodyAndHeaders(abcOrder, headers); + // check results + resultEndpoint.assertIsSatisfied(); + XYZOrder result = resultEndpoint.getExchanges().get(0).getIn().getBody(XYZOrder.class); + Assert.assertEquals(customerNumber, result.getCustId()); + Assert.assertEquals(orderNumber, result.getOrderId()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingFileTest.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingFileTest.java b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingFileTest.java new file mode 100644 index 0000000..6cec546 --- /dev/null +++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingFileTest.java @@ -0,0 +1,72 @@ +/** + * 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.component.dozer; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.dozer.example.abc.ABCOrder; +import org.apache.camel.component.dozer.example.xyz.XYZOrder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class ExpressionMappingFileTest { + + @EndpointInject(uri = "mock:result") + private MockEndpoint resultEndpoint; + + @Produce(uri = "direct:start") + private ProducerTemplate startEndpoint; + + @Autowired + private CamelContext camelContext; + + @After + public void tearDown() { + resultEndpoint.reset(); + } + + @Test + public void testExpressionMappingScriptFile() throws Exception { + resultEndpoint.expectedMessageCount(1); + Map<String, Object> headers = new HashMap<String, Object>(); + final String customerNumber = "CAFE-678"; + final String orderNumber = "ABC-002"; + headers.put("customerNumber", customerNumber); + headers.put("orderNumber", orderNumber); + ABCOrder abcOrder = new ABCOrder(); + // Header value should be mapped to custId in target model + startEndpoint.sendBodyAndHeaders(abcOrder, headers); + // check results + resultEndpoint.assertIsSatisfied(); + XYZOrder result = resultEndpoint.getExchanges().get(0).getIn().getBody(XYZOrder.class); + Assert.assertEquals(customerNumber, result.getCustId()); + Assert.assertEquals(orderNumber, result.getOrderId()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingTest.java b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingTest.java index 423f2a9..669487a 100644 --- a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingTest.java +++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/ExpressionMappingTest.java @@ -69,4 +69,5 @@ public class ExpressionMappingTest { Assert.assertEquals(customerNumber, result.getCustId()); Assert.assertEquals(orderNumber, result.getOrderId()); } + } http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingClasspathTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingClasspathTest-context.xml b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingClasspathTest-context.xml new file mode 100644 index 0000000..c5b7e9d --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingClasspathTest-context.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <!-- Camel route --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <endpoint uri="dozer:java2java?mappingFile=org/apache/camel/component/dozer/expressionMappingClasspath.xml&targetModel=org.apache.camel.component.dozer.example.xyz.XYZOrder&sourceModel=org.apache.camel.component.dozer.example.abc.ABCOrder" id="java2java"/> + <route> + <from uri="direct:start"/> + <to ref="java2java"/> + <to uri="mock:result"/> + </route> +</camelContext> +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingFileTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingFileTest-context.xml b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingFileTest-context.xml new file mode 100644 index 0000000..0acad77 --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/ExpressionMappingFileTest-context.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <!-- Camel route --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <endpoint uri="dozer:java2java?mappingFile=org/apache/camel/component/dozer/expressionMappingFile.xml&targetModel=org.apache.camel.component.dozer.example.xyz.XYZOrder&sourceModel=org.apache.camel.component.dozer.example.abc.ABCOrder" id="java2java"/> + <route> + <from uri="direct:start"/> + <to ref="java2java"/> + <to uri="mock:result"/> + </route> +</camelContext> +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionClasspath.txt ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionClasspath.txt b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionClasspath.txt new file mode 100644 index 0000000..418906f --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionClasspath.txt @@ -0,0 +1 @@ +orderNumber \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionFile.txt ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionFile.txt b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionFile.txt new file mode 100644 index 0000000..418906f --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionFile.txt @@ -0,0 +1 @@ +orderNumber \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingClasspath.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingClasspath.xml b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingClasspath.xml new file mode 100644 index 0000000..3b36fa3 --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingClasspath.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- + 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. +--> +<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> + <mapping> + <class-a>org.apache.camel.component.dozer.example.abc.ABCOrder</class-a> + <class-b>org.apache.camel.component.dozer.example.xyz.XYZOrder</class-b> + <field> + <a>header.status</a> + <b>priority</b> + </field> + <field> + <a>header.orderNum</a> + <b>orderId</b> + </field> + </mapping> + <mapping> + <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a> + <class-b>org.apache.camel.component.dozer.example.xyz.XYZOrder</class-b> + <field custom-converter-id="_expressionMapping" custom-converter-param="simple:\${header.customerNumber}"> + <a>expression</a> + <b>custId</b> + </field> + <field custom-converter-id="_expressionMapping" custom-converter-param="header:classpath:/org/apache/camel/component/dozer/expressionClasspath.txt"> + <a>expression</a> + <b>orderId</b> + </field> + </mapping> +</mappings> http://git-wip-us.apache.org/repos/asf/camel/blob/ad77413b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingFile.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingFile.xml b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingFile.xml new file mode 100644 index 0000000..56cf32b --- /dev/null +++ b/components/camel-dozer/src/test/resources/org/apache/camel/component/dozer/expressionMappingFile.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- + 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. +--> +<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> + <mapping> + <class-a>org.apache.camel.component.dozer.example.abc.ABCOrder</class-a> + <class-b>org.apache.camel.component.dozer.example.xyz.XYZOrder</class-b> + <field> + <a>header.status</a> + <b>priority</b> + </field> + <field> + <a>header.orderNum</a> + <b>orderId</b> + </field> + </mapping> + <mapping> + <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a> + <class-b>org.apache.camel.component.dozer.example.xyz.XYZOrder</class-b> + <field custom-converter-id="_expressionMapping" custom-converter-param="simple:\${header.customerNumber}"> + <a>expression</a> + <b>custId</b> + </field> + <field custom-converter-id="_expressionMapping" custom-converter-param="header:file:src/test/resources/org/apache/camel/component/dozer/expressionFile.txt"> + <a>expression</a> + <b>orderId</b> + </field> + </mapping> +</mappings>