Author: jliu
Date: Wed May 16 02:47:21 2007
New Revision: 538504

URL: http://svn.apache.org/viewvc?view=rev&rev=538504
Log:
Support SOAPHandlers handleFault() throws RuntimeException/ProtocolException on 
server side outbound

Modified:
    
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java
    
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPHandlerFaultOutInterceptor.java
    
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationTest.java
    
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestSOAPHandler.java

Modified: 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java
URL: 
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java?view=diff&rev=538504&r1=538503&r2=538504
==============================================================================
--- 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java
 (original)
+++ 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java
 Wed May 16 02:47:21 2007
@@ -349,7 +349,7 @@
         } catch (RuntimeException e) {
             LOG.log(Level.WARNING, "HANDLER_RAISED_RUNTIME_EXCEPTION", e);
             continueProcessing = false;
-            closed = true;
+            throw e;
         }
         return continueProcessing;
     }
@@ -527,6 +527,7 @@
         int index = invokedHandlers.size() - 1;
         while (index >= 0) {
             Handler handler = invokedHandlers.get(index);
+            //System.out.println("===========invokeReversedClose " + 
invokeReversedClose.toString());
             if (handler instanceof LogicalHandler) {
                 handler.close(logicalMessageContext);
             } else {

Modified: 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPHandlerFaultOutInterceptor.java
URL: 
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPHandlerFaultOutInterceptor.java?view=diff&rev=538504&r1=538503&r2=538504
==============================================================================
--- 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPHandlerFaultOutInterceptor.java
 (original)
+++ 
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPHandlerFaultOutInterceptor.java
 Wed May 16 02:47:21 2007
@@ -24,13 +24,20 @@
 import java.util.Set;
 
 import javax.xml.namespace.QName;
+import javax.xml.soap.SOAPBody;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPFault;
 import javax.xml.soap.SOAPMessage;
 import javax.xml.ws.Binding;
 import javax.xml.ws.ProtocolException;
 import javax.xml.ws.handler.Handler;
 import javax.xml.ws.handler.MessageContext;
 import javax.xml.ws.handler.soap.SOAPHandler;
+import javax.xml.ws.soap.SOAPFaultException;
 
+import org.w3c.dom.Node;
+
+import org.apache.cxf.binding.soap.SoapFault;
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
 import org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor;
@@ -115,9 +122,47 @@
 
         Throwable cause = f.getCause();
         if (cause instanceof ProtocolException) {
+            try {
 
-            if 
(!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
-                // handleAbort(message, context);
+                if 
(!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
+                    // handleAbort(message, context);
+                }
+            } catch (RuntimeException exception) {
+                //Replace SOAPFault with the exception thrown from HandleFault
+                
+                try {
+                    SOAPMessage msgFromHandleFault = 
message.getContent(SOAPMessage.class);
+
+                    SOAPBody body = msgFromHandleFault.getSOAPBody();
+                    
+                    body.removeContents();
+
+                    SOAPFault soapFault = body.addFault();
+
+                    if (exception instanceof SOAPFaultException) {
+                        SOAPFaultException sf = (SOAPFaultException)exception;
+                        
soapFault.setFaultString(sf.getFault().getFaultString());
+                        soapFault.setFaultCode(sf.getFault().getFaultCode());
+                        soapFault.setFaultActor(sf.getFault().getFaultActor());
+                        Node nd = msgFromHandleFault.getSOAPPart().importNode(
+                                                                              
sf.getFault().getDetail()
+                                                                               
   .getFirstChild(), true);
+                        soapFault.addDetail().appendChild(nd);
+                    } else if (exception instanceof Fault) {
+                        SoapFault sf = SoapFault.createFault((Fault)exception, 
((SoapMessage)message)
+                            .getVersion());
+                        soapFault.setFaultString(sf.getReason());
+                        soapFault.setFaultCode(sf.getFaultCode());
+                        Node nd = 
msgFromHandleFault.getSOAPPart().importNode(sf.getOrCreateDetail(), true);
+                        soapFault.addDetail().appendChild(nd);
+                    } else {
+                        soapFault.setFaultString(exception.getMessage());
+                        soapFault.setFaultCode(new 
QName("http://cxf.apache.org/faultcode";, "HandleFault"));
+                    }
+                } catch (SOAPException e) {
+                    //do nothing
+                    e.printStackTrace();
+                } 
             }
         } else {
             // do nothing

Modified: 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationTest.java?view=diff&rev=538504&r1=538503&r2=538504
==============================================================================
--- 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationTest.java
 (original)
+++ 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationTest.java
 Wed May 16 02:47:21 2007
@@ -724,7 +724,7 @@
             PrintStream ps = new PrintStream(baos, true);
             e.printStackTrace(ps);
             assertTrue("Did not get expected exception message",  
baos.toString()
-                .indexOf("HandleMessage throws RuntimeException exception") > 
-1);
+                .indexOf("HandleMessage throws exception") > -1);
             assertTrue("Did not get expected 
javax.xml.ws.soap.SOAPFaultException", baos.toString()
                 .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
         }        
@@ -741,7 +741,7 @@
             PrintStream ps = new PrintStream(baos, true);
             e.printStackTrace(ps);
             assertTrue("Did not get expected exception message",  
baos.toString()
-                .indexOf("HandleMessage throws RuntimeException exception") > 
-1);
+                .indexOf("HandleMessage throws exception") > -1);
             assertTrue("Did not get expected 
javax.xml.ws.soap.SOAPFaultException", baos.toString()
                 .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
         }        
@@ -758,12 +758,41 @@
             PrintStream ps = new PrintStream(baos, true);
             e.printStackTrace(ps);
             assertTrue("Did not get expected exception message",  
baos.toString()
-                .indexOf("HandleMessage throws ProtocolException exception") > 
-1);
+                .indexOf("HandleMessage throws exception") > -1);
             assertTrue("Did not get expected 
javax.xml.ws.soap.SOAPFaultException", baos.toString()
                 .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
         }        
     }
-
+    
+    /*-------------------------------------------------------
+    * This is the expected order
+    *-------------------------------------------------------
+    * soapHandler3.handleMessage().doInbound()
+    * soapHandler4.handleMessage().doInbound()
+    * soapHandler4 Throwing an inbound ProtocolException
+    * soapHandler3.handleFault()
+    * soapHandler3 Throwing an outbound RuntimeException
+    * soapHandler4.close()
+    * soapHandler3.close()
+    */
+    @Test
+    public void 
testSOAPHandlerHandleFaultThrowsRuntimeExceptionServerOutbound() throws 
PingException {
+        try {
+            handlerTest.pingWithArgs("soapHandler3 inbound throw 
ProtocolException "
+                                     + 
"soapHandler4HandleFaultThrowsRunException");
+            fail("did not get expected WebServiceException");
+        } catch (WebServiceException e) {
+            //e.printStackTrace();
+/*            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            PrintStream ps = new PrintStream(baos, true);
+            e.printStackTrace(ps);
+            assertTrue("Did not get expected exception message",  
baos.toString()
+                .indexOf("soapHandler4 HandleFault throws RuntimeException") > 
-1);
+            assertTrue("Did not get expected 
javax.xml.ws.soap.SOAPFaultException", baos.toString()
+                .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
+        }        
+    }
+    
     @Test
     public void 
testSOAPHandlerHandleMessageThrowsProtocolExceptionServerOutbound() throws 
PingException {
         try {

Modified: 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestSOAPHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestSOAPHandler.java?view=diff&rev=538504&r1=538503&r2=538504
==============================================================================
--- 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestSOAPHandler.java
 (original)
+++ 
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestSOAPHandler.java
 Wed May 16 02:47:21 2007
@@ -119,6 +119,20 @@
     public final boolean handleFault(T ctx) {
         methodCalled("handleFault"); 
         printHandlerInfo("handleFault", isOutbound(ctx));
+
+        if (!"soapHandler4".equals(getHandlerId())) {
+            return true;
+        } 
+        
+        try {
+            SOAPMessage msg = ctx.getMessage();
+            if 
("soapHandler4HandleFaultThrowsRunException".equals(msg.getSOAPBody().getFault()
+                .getFaultString())) {
+                throw new RuntimeException("soapHandler4 HandleFault throws 
RuntimeException");
+            }
+        } catch (SOAPException e) {
+            // do nothing
+        }
         return true;
     }
 
@@ -191,20 +205,24 @@
                 }
             } else if ("throw".equals(command)) {
                 String exceptionType = null;
+                String exceptionText = "HandleMessage throws exception";
                 if (strtok.hasMoreTokens()) {
                     exceptionType = strtok.nextToken();
                 }
+                if (strtok.hasMoreTokens()) {
+                    exceptionText = strtok.nextToken();
+                }
                 if (exceptionType != null && !outbound && 
"inbound".equals(direction)) {
                     if ("RuntimeException".equals(exceptionType)) {
-                        throw new RuntimeException("HandleMessage throws 
RuntimeException exception");
+                        throw new RuntimeException(exceptionText);
                     } else if ("ProtocolException".equals(exceptionType)) {
-                        throw new ProtocolException("HandleMessage throws 
ProtocolException exception");
+                        throw new ProtocolException(exceptionText);
                     }
                 } else if (exceptionType != null && outbound && 
"outbound".equals(direction)) {
                     if ("RuntimeException".equals(exceptionType)) {
-                        throw new RuntimeException("HandleMessage throws 
RuntimeException exception");
+                        throw new RuntimeException(exceptionText);
                     } else if ("ProtocolException".equals(exceptionType)) {
-                        throw new ProtocolException("HandleMessage throws 
ProtocolException exception");
+                        throw new ProtocolException(exceptionText);
                     }
                 }
              


Reply via email to