Author: davsclaus Date: Wed Oct 5 09:42:33 2011 New Revision: 1179125 URL: http://svn.apache.org/viewvc?rev=1179125&view=rev Log: CAMEL-4509: Marshal/unmarshal should clear OUT message in case of exception thrown.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelSetHeaderTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelUnmarshalSetHeaderTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java camel/trunk/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java?rev=1179125&r1=1179124&r2=1179125&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java Wed Oct 5 09:42:33 2011 @@ -55,9 +55,15 @@ public class MarshalProcessor extends Se Message out = exchange.getOut(); out.copyFrom(in); - dataFormat.marshal(exchange, body, buffer); - byte[] data = buffer.toByteArray(); - out.setBody(data); + try { + dataFormat.marshal(exchange, body, buffer); + byte[] data = buffer.toByteArray(); + out.setBody(data); + } catch (Exception e) { + // remove OUT message, as an exception occurred + exchange.setOut(null); + throw e; + } } @Override Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java?rev=1179125&r1=1179124&r2=1179125&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java Wed Oct 5 09:42:33 2011 @@ -27,6 +27,7 @@ import org.apache.camel.Traceable; import org.apache.camel.spi.DataFormat; import org.apache.camel.support.ServiceSupport; import org.apache.camel.util.ExchangeHelper; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ServiceHelper; @@ -56,10 +57,12 @@ public class UnmarshalProcessor extends Object result = dataFormat.unmarshal(exchange, stream); out.setBody(result); + } catch (Exception e) { + // remove OUT message, as an exception occurred + exchange.setOut(null); + throw e; } finally { - if (stream != null) { - stream.close(); - } + IOHelper.close(stream, "input stream"); } } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelSetHeaderTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelSetHeaderTest.java?rev=1179125&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelSetHeaderTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelSetHeaderTest.java Wed Oct 5 09:42:33 2011 @@ -0,0 +1,55 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +/** + * + */ +public class DeadLetterChannelSetHeaderTest extends ContextTestSupport { + + public void testDLCSetHeader() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:error"); + mock.expectedBodiesReceived("Hello World"); + mock.expectedHeaderReceived("foo", "123"); + mock.expectedHeaderReceived("bar", "456"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .errorHandler(deadLetterChannel("direct:error")) + .throwException(new IllegalArgumentException("Damn")); + + from("direct:error") + .setHeader("foo", constant("123")) + .setHeader("bar", constant("456")) + .to("mock:error"); + } + }; + } +} Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelUnmarshalSetHeaderTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelUnmarshalSetHeaderTest.java?rev=1179125&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelUnmarshalSetHeaderTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelUnmarshalSetHeaderTest.java Wed Oct 5 09:42:33 2011 @@ -0,0 +1,75 @@ +/** + * 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; + +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spi.DataFormat; + +/** + * + */ +public class DeadLetterChannelUnmarshalSetHeaderTest extends ContextTestSupport { + + public void testDLCSetHeader() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:error"); + mock.expectedBodiesReceived("Hello World"); + mock.expectedHeaderReceived("foo", "123"); + mock.expectedHeaderReceived("bar", "456"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + MyDataFormat df = new MyDataFormat(); + + from("direct:start") + .errorHandler(deadLetterChannel("direct:error")) + .unmarshal(df); + + from("direct:error") + .setHeader("foo", constant("123")) + .setHeader("bar", constant("456")) + .to("mock:error"); + } + }; + } + + private class MyDataFormat implements DataFormat { + + @Override + public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { + // noop + } + + @Override + public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { + throw new IllegalArgumentException("Damn"); + } + } +}