You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/06/23 13:32:27 UTC
svn commit: r957165 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/util/
camel-core/src/test/java/org/apache/camel/util/ tests/camel-itest/
tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/
Author: davsclaus
Date: Wed Jun 23 11:32:26 2010
New Revision: 957165
URL: http://svn.apache.org/viewvc?rev=957165&view=rev
Log:
CAMEL-2843: Fixed using setHeader with Groovy to not loose message details during routing. In fact this applies for all script languages.
Added:
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java (with props)
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java (with props)
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
camel/trunk/tests/camel-itest/pom.xml
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=957165&r1=957164&r2=957165&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Wed Jun 23 11:32:26 2010
@@ -318,9 +318,12 @@ public final class ExchangeHelper {
map.put("headers", in.getHeaders());
map.put("body", in.getBody());
if (isOutCapable(exchange)) {
- Message out = exchange.getOut();
- map.put("out", out);
- map.put("response", out);
+ // if we are out capable then set out and response as well
+ // however only grab OUT if it exists, otherwise reuse IN
+ // this prevents side effects to alter the Exchange if we force creating an OUT message
+ Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
+ map.put("out", msg);
+ map.put("response", msg);
}
map.put("camelContext", exchange.getContext());
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java?rev=957165&r1=957164&r2=957165&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java Wed Jun 23 11:32:26 2010
@@ -148,6 +148,30 @@ public class ExchangeHelperTest extends
assertSame(exchange.getContext(), map.get("camelContext"));
}
+ public void testCreateVariableMapNoExistingOut() throws Exception {
+ exchange.setPattern(ExchangePattern.InOut);
+ exchange.getIn().setBody("bar");
+ exchange.getIn().setHeader("quote", "Camel rocks");
+ assertFalse(exchange.hasOut());
+
+ Map map = ExchangeHelper.createVariableMap(exchange);
+
+ // there should still be 8 in the map
+ assertEquals(8, map.size());
+ assertSame(exchange, map.get("exchange"));
+ assertSame(exchange.getIn(), map.get("in"));
+ assertSame(exchange.getIn(), map.get("request"));
+ assertSame(exchange.getIn(), map.get("out"));
+ assertSame(exchange.getIn(), map.get("response"));
+ assertSame(exchange.getIn().getHeaders(), map.get("headers"));
+ assertSame(exchange.getIn().getBody(), map.get("body"));
+ assertSame(exchange.getContext(), map.get("camelContext"));
+
+ // but the Exchange does still not have an OUT message to avoid
+ // causing side effects with the createVariableMap method
+ assertFalse(exchange.hasOut());
+ }
+
public void testGetContentType() throws Exception {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/xml");
assertEquals("text/xml", ExchangeHelper.getContentType(exchange));
Modified: camel/trunk/tests/camel-itest/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/pom.xml?rev=957165&r1=957164&r2=957165&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/pom.xml (original)
+++ camel/trunk/tests/camel-itest/pom.xml Wed Jun 23 11:32:26 2010
@@ -66,6 +66,11 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
+ <artifactId>camel-groovy</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<scope>test</scope>
</dependency>
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java?rev=957165&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java Wed Jun 23 11:32:26 2010
@@ -0,0 +1,57 @@
+/**
+ * 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.itest.jetty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class JettyConstantSetHeaderTest extends CamelTestSupport {
+
+ @Test
+ public void testJettyConstantSetHeader() throws Exception {
+ getMockEndpoint("mock:before").message(0).header("beer").isNull();
+
+ MockEndpoint result = getMockEndpoint("mock:result");
+ result.expectedBodiesReceived("Hello World");
+ result.message(0).header("beer").isEqualTo("Carlsberg");
+
+ String reply = template.requestBody("http://localhost:8223/beer", "Hello World", String.class);
+ assertEquals("Bye World", reply);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("jetty:http://localhost:8223/beer")
+ .convertBodyTo(String.class)
+ .to("mock:before")
+ .setHeader("beer", constant("Carlsberg"))
+ .to("mock:result")
+ .transform(constant("Bye World"));
+ }
+ };
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyConstantSetHeaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java?rev=957165&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java Wed Jun 23 11:32:26 2010
@@ -0,0 +1,59 @@
+/**
+ * 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.itest.jetty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.language.groovy.GroovyLanguage.groovy;
+
+/**
+ * @version $Revision$
+ */
+public class JettyGroovySetHeaderTest extends CamelTestSupport {
+
+ @Test
+ public void testJettyGroovySetHeader() throws Exception {
+ getMockEndpoint("mock:before").message(0).header("beer").isNull();
+
+ MockEndpoint result = getMockEndpoint("mock:result");
+ result.expectedBodiesReceived("Hello World");
+ result.message(0).header("beer").isEqualTo("Carlsberg");
+
+ String reply = template.requestBody("http://localhost:8222/beer", "Hello World", String.class);
+ assertEquals("Bye World", reply);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("jetty:http://localhost:8222/beer")
+ .convertBodyTo(String.class)
+ .to("mock:before")
+ .setHeader("beer", groovy("'Carlsberg'"))
+ .to("mock:result")
+ .transform(groovy("'Bye World'"));
+ }
+ };
+ }
+}
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyGroovySetHeaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java?rev=957165&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java Wed Jun 23 11:32:26 2010
@@ -0,0 +1,59 @@
+/**
+ * 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.itest.jetty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.language.simple.SimpleLanguage.simple;
+
+/**
+ * @version $Revision$
+ */
+public class JettySimpleSetHeaderTest extends CamelTestSupport {
+
+ @Test
+ public void testJettySimpleSetHeader() throws Exception {
+ getMockEndpoint("mock:before").message(0).header("beer").isNull();
+
+ MockEndpoint result = getMockEndpoint("mock:result");
+ result.expectedBodiesReceived("Hello World");
+ result.message(0).header("beer").isEqualTo("Carlsberg");
+
+ String reply = template.requestBody("http://localhost:8224/beer", "Hello World", String.class);
+ assertEquals("Bye World", reply);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("jetty:http://localhost:8224/beer")
+ .convertBodyTo(String.class)
+ .to("mock:before")
+ .setHeader("beer", simple("Carlsberg"))
+ .to("mock:result")
+ .transform(simple("Bye World"));
+ }
+ };
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettySimpleSetHeaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date