You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/03/24 07:23:17 UTC
svn commit: r640326 - in /activemq/camel/trunk:
camel-core/src/main/java/org/apache/camel/model/
camel-core/src/main/java/org/apache/camel/processor/
camel-core/src/main/resources/org/apache/camel/model/
camel-core/src/test/java/org/apache/camel/proces...
Author: ningjiang
Date: Sun Mar 23 23:23:16 2008
New Revision: 640326
URL: http://svn.apache.org/viewvc?rev=640326&view=rev
Log:
CAMEL-266 make it easy to throw a SOAP fault in a Camel route
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java (with props)
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java (with props)
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java (with props)
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml (with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FaultRouteTest.java
activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java?rev=640326&r1=640325&r2=640326&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java Sun Mar 23 23:23:16 2008
@@ -26,6 +26,7 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
+import org.apache.camel.CamelException;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
@@ -701,6 +702,18 @@
ThrottlerType answer = new ThrottlerType(maximumRequestCount);
addOutput(answer);
return answer;
+ }
+
+
+ public Type throwFault(Throwable fault) {
+ ThrowFaultType answer = new ThrowFaultType();
+ answer.setFault(fault);
+ addOutput(answer);
+ return (Type) this;
+ }
+
+ public Type throwFault(String message) {
+ return throwFault(new CamelException(message));
}
public Type interceptor(String ref) {
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java?rev=640326&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java Sun Mar 23 23:23:16 2008
@@ -0,0 +1,87 @@
+/**
+ *
+ * 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.model;
+
+import java.util.Collections;
+import java.util.List;
+
+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.CamelException;
+import org.apache.camel.Processor;
+import org.apache.camel.impl.RouteContext;
+import org.apache.camel.processor.ThrowFaultProcessor;
+
+@XmlRootElement(name = "throwFault")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class ThrowFaultType extends ProcessorType<ThrowFaultType> {
+ @XmlTransient
+ private Throwable fault;
+ @XmlTransient
+ private Processor processor;
+ @XmlAttribute (required = true)
+ private String faultRef;
+
+ public ThrowFaultType() {
+
+ }
+
+ public void setFault(Throwable fault) {
+ this.fault = fault;
+ }
+
+ public Throwable getFault() {
+ return fault;
+ }
+
+ public void setFaultRef(String ref) {
+ this.faultRef = ref;
+ }
+
+ public String getFaultRef() {
+ return faultRef;
+ }
+
+ @Override
+ public Processor createProcessor(RouteContext routeContext) {
+
+ if (processor == null) {
+ if (fault == null) {
+ fault = routeContext.lookup(faultRef, Throwable.class);
+ if (fault == null) {
+ // can't find the fault instance, create a new one
+ fault = new CamelException(faultRef);
+ }
+ }
+ processor = new ThrowFaultProcessor(fault);
+ }
+ return processor;
+ }
+
+ @Override
+ public List<ProcessorType<?>> getOutputs() {
+ return Collections.EMPTY_LIST;
+ }
+
+
+
+}
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultType.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java?rev=640326&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java Sun Mar 23 23:23:16 2008
@@ -0,0 +1,42 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+
+/**
+ * The processor which implements the ThrowFault DSL
+ */
+public class ThrowFaultProcessor implements Processor {
+ private Throwable fault;
+
+ public ThrowFaultProcessor(Throwable fault) {
+ this.fault = fault;
+ }
+
+ /** Set the fault message in the exchange
+ * @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
+ */
+ public void process(Exchange exchange) throws Exception {
+ Message message = exchange.getFault();
+ message.setBody(fault);
+ }
+
+}
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index?rev=640326&r1=640325&r2=640326&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index (original)
+++ activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index Sun Mar 23 23:23:16 2008
@@ -44,6 +44,7 @@
SplitterType
ThrottlerType
ThreadType
+ThrowFaultType
ToType
TryType
UnmarshalType
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FaultRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FaultRouteTest.java?rev=640326&r1=640325&r2=640326&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FaultRouteTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FaultRouteTest.java Sun Mar 23 23:23:16 2008
@@ -17,6 +17,7 @@
*/
package org.apache.camel.processor;
+import org.apache.camel.CamelException;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
@@ -66,7 +67,7 @@
MockEndpoint.assertIsSatisfied(a, b, c);
- // TODO wrap up as an expecation on the mock endpoint
+ // TODO wrap up as an exception on the mock endpoint
List<Exchange> list = a.getReceivedExchanges();
Exchange exchange = list.get(0);
Message fault = exchange.getFault();
@@ -74,6 +75,42 @@
assertEquals("Fault body", "fault", fault.getBody());
}
+
+ public void testWithThrowFaultMessage() throws Exception {
+
+ throwFaultTest("direct:string");
+
+ }
+
+ public void testWithThrowFaultException() throws Exception {
+
+ throwFaultTest("direct:exception");
+
+ }
+
+ private void throwFaultTest(String startPoint) throws InterruptedException {
+ a.expectedMessageCount(1);
+ b.expectedMessageCount(0);
+ c.expectedMessageCount(0);
+
+ template.sendBody(startPoint, "in");
+
+ MockEndpoint.assertIsSatisfied(a, b, c);
+
+ List<Exchange> list = a.getReceivedExchanges();
+ Exchange exchange = list.get(0);
+ Message fault = exchange.getFault();
+ assertNotNull("Should have a fault on A", fault);
+ if (startPoint.equals("direct:exception")) {
+ assertTrue("It should be the IllegalStateException", fault.getBody() instanceof IllegalStateException);
+ assertEquals("Fault message", "It makes no sense of business logic", ((IllegalStateException)(fault.getBody())).getMessage());
+ } else { // test for the throwFault with String
+ assertTrue("It should be the CamelException", fault.getBody() instanceof CamelException);
+ assertEquals("Fault message", "ExceptionMessage", ((CamelException)(fault.getBody())).getMessage());
+ }
+
+ }
+
@Override
protected void setUp() throws Exception {
super.setUp();
@@ -89,6 +126,16 @@
public void configure() {
from("direct:start")
.to("mock:a")
+ .to("mock:b");
+
+ from("direct:string")
+ .to("mock:a")
+ .throwFault("ExceptionMessage")
+ .to("mock:b");
+
+ from("direct:exception")
+ .to("mock:a")
+ .throwFault(new IllegalStateException("It makes no sense of business logic"))
.to("mock:b");
}
};
Modified: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java?rev=640326&r1=640325&r2=640326&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java Sun Mar 23 23:23:16 2008
@@ -44,9 +44,19 @@
private static final String EXCEPTION_MESSAGE = "This is an exception test message";
private static final String DETAIL_TEXT = "This is a detail text node";
+ private static SoapFault SOAP_FAULT;
private Bus bus;
+ static {
+ SOAP_FAULT = new SoapFault(EXCEPTION_MESSAGE, SoapFault.FAULT_CODE_CLIENT);
+ Element detail = SOAP_FAULT.getOrCreateDetail();
+ Document doc = detail.getOwnerDocument();
+ Text tn = doc.createTextNode(DETAIL_TEXT);
+ detail.appendChild(tn);
+ }
+
+
@Override
protected void setUp() throws Exception {
BusFactory.setDefaultBus(null);
@@ -66,17 +76,7 @@
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
- from(ROUTER_ENDPOINT_URI).process(new Processor() {
- public void process(Exchange exchange) throws Exception {
- Message message = exchange.getFault();
- SoapFault fault = new SoapFault(EXCEPTION_MESSAGE, SoapFault.FAULT_CODE_CLIENT);
- Element detail = fault.getOrCreateDetail();
- Document doc = detail.getOwnerDocument();
- Text tn = doc.createTextNode(DETAIL_TEXT);
- detail.appendChild(tn);
- message.setBody(fault);
- }
- });
+ from(ROUTER_ENDPOINT_URI).throwFault(SOAP_FAULT);
}
};
}
Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java?rev=640326&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java Sun Mar 23 23:23:16 2008
@@ -0,0 +1,32 @@
+/**
+ * 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.spring.processor;
+
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+import org.apache.camel.CamelContext;
+import org.apache.camel.processor.FaultRouteTest;
+
+/**
+ * The spring context test for the FaultRoute
+ */
+public class SpringFaultRouteTest extends FaultRouteTest {
+ protected CamelContext createCamelContext() throws Exception {
+ return createSpringCamelContext(this, "org/apache/camel/spring/processor/faultRoute.xml");
+ }
+
+}
Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringFaultRouteTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml?rev=640326&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml Sun Mar 23 23:23:16 2008
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+ http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+ ">
+
+ <!-- START SNIPPET: example -->
+ <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+ <route>
+ <from uri="direct:start"/>
+ <to uri="mock:a"/>
+ <to uri="mock:b"/>
+ </route>
+
+ <route>
+ <from uri="direct:exception"/>
+ <to uri="mock:a"/>
+ <throwFault faultRef="myFault"/>
+ <to uri="mock:b"/>
+ </route>
+
+ <route>
+ <from uri="direct:string"/>
+ <to uri="mock:a"/>
+ <throwFault faultRef="ExceptionMessage"/>
+ <to uri="mock:b"/>
+ </route>
+ </camelContext>
+ <!-- END SNIPPET: example -->
+
+ <bean id="myFault" class="java.lang.IllegalStateException" >
+ <constructor-arg index="0" value="It makes no sense of business logic"/>
+ </bean>
+</beans>
Propchange: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/faultRoute.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml