Author: ningjiang
Date: Fri Aug 20 03:46:55 2010
New Revision: 987375
URL: http://svn.apache.org/viewvc?rev=987375&view=rev
Log:
CAMEL-3033 @WebFault interceptor to map exceptions to faults
Added:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
(with props)
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java?rev=987375&r1=987374&r2=987375&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
(original)
+++
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
Fri Aug 20 03:46:55 2010
@@ -16,8 +16,12 @@
*/
package org.apache.camel.component.cxf;
+import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
+import javax.xml.ws.WebFault;
+
+import org.w3c.dom.Element;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultConsumer;
@@ -123,21 +127,48 @@ public class CxfConsumer extends Default
}
private void checkFailure(org.apache.camel.Exchange camelExchange)
throws Fault {
+ final Throwable t;
if (camelExchange.isFailed()) {
- // either Fault or Exception
- Throwable t = (camelExchange.hasOut() &&
camelExchange.getOut().isFault())
- ? camelExchange.getOut().getBody(Throwable.class) :
camelExchange.getException();
- // There is no exception and the Fault message is set to
the out message
- if (t != null) {
- throw (t instanceof Fault) ? (Fault)t : new Fault(t);
+ t = (camelExchange.hasOut() &&
camelExchange.getOut().isFault()) ? camelExchange.getOut()
+ .getBody(Throwable.class) :
camelExchange.getException();
+ if (t instanceof Fault) {
+ throw (Fault)t;
+ } else if (t != null) {
+ // This is not a CXF Fault. Build the CXF Fault
manuallly.
+ Fault fault = new Fault(t);
+ if (fault.getMessage() == null) {
+ // The Fault has no Message. This is the case if t
had
+ // no message, for
+ // example was a NullPointerException.
+ fault.setMessage(t.getClass().getSimpleName());
+ }
+ WebFault faultAnnotation =
t.getClass().getAnnotation(WebFault.class);
+ Object faultInfo = null;
+ try {
+ Method method =
t.getClass().getMethod("getFaultInfo", new Class[0]);
+ faultInfo = method.invoke(t, new Object[0]);
+ } catch (Exception e) {
+ // do nothing here
+ }
+ if (faultAnnotation != null && faultInfo == null) {
+ // t has a JAX-WS WebFault annotation, which
describes
+ // in detail the Web Service Fault that should be
thrown. Add the
+ // detail.
+ Element detail = fault.getOrCreateDetail();
+ Element faultDetails = detail.getOwnerDocument()
+
.createElementNS(faultAnnotation.targetNamespace(), faultAnnotation.name());
+ detail.appendChild(faultDetails);
+ }
+
+ throw fault;
}
+
}
-
}
-
+
});
- server = svrBean.create();
- }
+ server = svrBean.create();
+ }
@Override
protected void doStart() throws Exception {
Added:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java?rev=987375&view=auto
==============================================================================
---
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
(added)
+++
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
Fri Aug 20 03:46:55 2010
@@ -0,0 +1,77 @@
+/**
+ * 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.cxf;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.CamelTestSupport;
+import org.apache.cxf.frontend.ClientFactoryBean;
+import org.apache.cxf.greeter_control.Greeter;
+import org.apache.cxf.greeter_control.PingMeFault;
+import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
+import org.junit.Test;
+
+/**
+ * Test for throwing an exception with a JAX-WS WebFault annotation from Camel
CXF consumer
+ */
+public class JaxWsWebFaultAnnotationToFaultTest extends CamelTestSupport {
+
+ protected static final String ROUTER_ADDRESS =
"http://localhost:9002/router";
+ protected static final String SERVICE_CLASS =
"serviceClass=org.apache.cxf.greeter_control.Greeter";
+ protected static final String SERVICE_URI = "cxf://" + ROUTER_ADDRESS +
"?" + SERVICE_CLASS;
+
+ protected static final String MESSAGE = "this is our test message for the
exception";
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from(SERVICE_URI).process(new Processor() {
+
+ public void process(Exchange exchng) throws Exception {
+ throw new PingMeFault(MESSAGE);
+ }
+ });
+ }
+ };
+ }
+
+ @Test
+ public void testInvokingServiceFromCXFClient() throws Exception {
+ JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
+ ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
+ clientBean.setAddress(ROUTER_ADDRESS);
+ clientBean.setServiceClass(Greeter.class);
+
+ Greeter client = (Greeter) proxyFactory.create();
+
+ try {
+ client.pingMe();
+ fail("Expect to get an exception here");
+ } catch (PingMeFault expected) {
+ assertEquals(MESSAGE, expected.getMessage());
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("The CXF client did not manage to map the client exception "
+ + t.getClass().getName() + " to a " +
PingMeFault.class.getName()
+ + ": " + t.getMessage());
+ }
+
+ }
+
+}
\ No newline at end of file
Propchange:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/JaxWsWebFaultAnnotationToFaultTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date