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/11 15:34:25 UTC

svn commit: r635944 - in /activemq/camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/feature/ main/java/org/apache/camel/component/cxf/interceptors/ test/java/org/apache/camel/component/cxf/

Author: ningjiang
Date: Tue Mar 11 07:34:21 2008
New Revision: 635944

URL: http://svn.apache.org/viewvc?rev=635944&view=rev
Log:
CAMEL-363 Added the support for throwing the customized exception from Camel Processor

Added:
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java   (with props)
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java   (with props)
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java   (with props)
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/MessageDataFormatFeature.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FaultOutInterceptor.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/MessageDataFormatFeature.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/MessageDataFormatFeature.java?rev=635944&r1=635943&r2=635944&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/MessageDataFormatFeature.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/MessageDataFormatFeature.java Tue Mar 11 07:34:21 2008
@@ -19,6 +19,7 @@
 
 import java.util.logging.Logger;
 
+import org.apache.camel.component.cxf.interceptors.FaultOutInterceptor;
 import org.apache.camel.component.cxf.interceptors.RawMessageContentRedirectInterceptor;
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.logging.LogUtils;
@@ -50,7 +51,7 @@
         client.getEndpoint().getBinding().getOutInterceptors().clear();
         client.getEndpoint().getOutInterceptors().add(new RawMessageContentRedirectInterceptor());
 
-
+        client.getEndpoint().getBinding().getOutFaultInterceptors().add(new FaultOutInterceptor());
     }
 
     @Override
@@ -66,6 +67,8 @@
         removeInterceptorWhichIsOutThePhases(server.getEndpoint().getOutInterceptors(), REMAINING_OUT_PHASES);
         // Do not use the binding interceptor any more
         server.getEndpoint().getBinding().getOutInterceptors().clear();
+
+        server.getEndpoint().getBinding().getOutFaultInterceptors().add(new FaultOutInterceptor());
 
 
         resetServiceInvokerInterceptor(server);

Added: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java?rev=635944&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java (added)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java Tue Mar 11 07:34:21 2008
@@ -0,0 +1,45 @@
+/**
+ * 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.interceptors;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.cxf.service.model.BindingFaultInfo;
+import org.apache.cxf.service.model.BindingInfo;
+import org.apache.cxf.service.model.BindingOperationInfo;
+import org.apache.cxf.service.model.OperationInfo;
+import org.apache.cxf.service.model.ServiceInfo;
+
+/**
+ * This class is used to provide the BindingOperationInfo for
+ * the FaultOutInterceptor which serves for the RawMessage DataFormat
+ *
+ */
+public class FakeBindingOperationInfo extends BindingOperationInfo {
+
+    public FakeBindingOperationInfo() {
+        super();
+    }
+
+    public boolean isUnwrapped() {
+        return false;
+    }
+
+
+
+}

Propchange: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FakeBindingOperationInfo.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FaultOutInterceptor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FaultOutInterceptor.java?rev=635944&r1=635943&r2=635944&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FaultOutInterceptor.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/FaultOutInterceptor.java Tue Mar 11 07:34:21 2008
@@ -24,43 +24,62 @@
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.xml.XMLFault;
 import org.apache.cxf.common.logging.LogUtils;
-//import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.XMLMessage;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
+import org.apache.cxf.service.model.BindingOperationInfo;
 
 public class FaultOutInterceptor extends AbstractPhaseInterceptor<Message> {
     private static final Logger LOG = LogUtils.getL7dLogger(FaultOutInterceptor.class);
-    
+
     public FaultOutInterceptor() {
-        super(Phase.PREPARE_SEND);        
+        super(Phase.PREPARE_SEND);
     }
 
     @SuppressWarnings("unchecked")
     public void handleMessage(Message message) throws Fault {
-        Exception ex = message.getContent(Exception.class); 
+        // To walk around the FaultOutInterceptor NPE issue of CXF 2.0.4
+        checkBindingOperationInfor(message);
+
+        Exception ex = message.getContent(Exception.class);
 
         if (ex != null) {
             if (!(ex instanceof Fault)) {
                 ex = new Fault(ex);
             }
-            
+
             if (message instanceof XMLMessage) {
                 if (LOG.isLoggable(Level.INFO)) {
                     LOG.info("FaultOutInterceptor Creating XMLFault");
-                }                
+                }
                 ex = XMLFault.createFault((Fault)ex);
             } else if (message instanceof SoapMessage) {
                 if (LOG.isLoggable(Level.INFO)) {
                     LOG.info("FaultOutInterceptor Creating SoapFault");
-                }                
-                
+                }
+
                 SoapMessage sm = (SoapMessage)message;
                 ex = SoapFault.createFault((Fault)ex, sm.getVersion());
-            } 
+            }
             message.setContent(Exception.class, ex);
         }
+    }
+
+    /*
+     * This method is used to walk around the NPE issue of CXF 2.0.4
+     * org.apache.cxf.interceptor.FaultOutInterceptor.
+     * This issue was fixed in CXF 2.0.5 and CXF 2.1, when we upgrade CXF to that version
+     * we could remove this method from the interceptor
+     */
+    private void checkBindingOperationInfor(Message message) {
+        BindingOperationInfo bop = message.getExchange().get(BindingOperationInfo.class);
+        if (bop == null) {
+            bop = new FakeBindingOperationInfo();
+            message.getExchange().put(BindingOperationInfo.class, bop);
+        }
+
+
     }
 }

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java?rev=635944&r1=635943&r2=635944&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java Tue Mar 11 07:34:21 2008
@@ -27,15 +27,20 @@
 
 public class RawMessageContentRedirectInterceptor extends AbstractPhaseInterceptor<Message> {
     public RawMessageContentRedirectInterceptor() {
-        super(Phase.WRITE);        
+        super(Phase.WRITE);
     }
-    
+
     public void handleMessage(Message message) throws Fault {
+        // check the fault from the message
+        Exception ex = message.getContent(Exception.class);
+        if (ex != null) {
+            throw new Fault(ex);
+        }
 
         InputStream is = message.getContent(InputStream.class);
         OutputStream os = message.getContent(OutputStream.class);
-        
-        try {            
+
+        try {
             IOUtils.copy(is, os);
             is.close();
             os.flush();

Added: 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=635944&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java (added)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java Tue Mar 11 07:34:21 2008
@@ -0,0 +1,98 @@
+/**
+ * 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.CamelContext;
+import org.apache.camel.CamelException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.endpoint.ServerImpl;
+import org.apache.cxf.frontend.ClientFactoryBean;
+import org.apache.cxf.frontend.ClientProxyFactoryBean;
+import org.apache.cxf.frontend.ServerFactoryBean;
+
+public class CxfCustmerizedExceptionTest extends ContextTestSupport {
+    protected static final String ROUTER_ADDRESS = "http://localhost:9002/router";
+    protected static final String SERVICE_CLASS = "serviceClass=org.apache.camel.component.cxf.HelloService";
+    protected static String ROUTER_ENDPOINT_URI = "cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS;
+
+    private static final String EXCEPTION_MESSAGE = "This is an exception test message";
+
+    private Bus bus;
+
+
+    @Override
+    protected void setUp() throws Exception {
+        BusFactory.setDefaultBus(null);
+        bus = BusFactory.getDefaultBus();
+        super.setUp();
+
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        //TODO need to shutdown the server
+        super.tearDown();
+        //bus.shutdown(true);
+        BusFactory.setDefaultBus(null);
+    }
+
+    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();
+                        message.setBody(new CamelException(EXCEPTION_MESSAGE));
+                    }
+
+                });
+            }
+        };
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        return new DefaultCamelContext();
+    }
+
+
+    public void testInvokingServiceFromCXFClient() throws Exception {
+        ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean();
+        ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
+        clientBean.setAddress(ROUTER_ADDRESS);
+        clientBean.setServiceClass(HelloService.class);
+        clientBean.setBus(bus);
+
+        HelloService client = (HelloService) proxyFactory.create();
+
+        try {
+            client.echo("hello world");
+            fail("Except to get an exception here");
+        } catch (Exception e) {
+            assertEquals("Except to get right exception message", e.getMessage(), EXCEPTION_MESSAGE);
+        }
+
+    }
+
+
+}

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfCustmerizedExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java?rev=635944&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java (added)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java Tue Mar 11 07:34:21 2008
@@ -0,0 +1,34 @@
+/**
+ * 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.cxf.BusFactory;
+
+/**
+ * Test for throwing the CustomizedException from Camel CXF consumer with MESSAGE DataFormat
+ */
+public class CxfMessageCustomizedExceptionTest extends CxfCustmerizedExceptionTest {
+
+    @Override
+    protected void setUp() throws Exception {
+        ROUTER_ENDPOINT_URI = "cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS + "&DataFormat=MESSAGE";
+        super.setUp();
+
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfMessageCustomizedExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java?rev=635944&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java (added)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java Tue Mar 11 07:34:21 2008
@@ -0,0 +1,31 @@
+/**
+ * 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;
+
+/**
+ * Test for throwing the CustomizedException from Camel CXF consumer with PAYLOAD DataFormat
+ */
+public class CxfPayLoadCustomizedExceptionTest extends CxfCustmerizedExceptionTest {
+    @Override
+    protected void setUp() throws Exception {
+        ROUTER_ENDPOINT_URI = "cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS + "&DataFormat=MESSAGE";
+        super.setUp();
+
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadCustomizedExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date