Author: jliu
Date: Mon May 21 22:00:32 2007
New Revision: 540423
URL: http://svn.apache.org/viewvc?view=rev&rev=540423
Log:
Support logical handler handleFault() throws Exception on server side outbound,
in this case, we go through fault out interceptor again to dispatch the
exception thrown from handleFault().
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/phase/PhaseInterceptorChain.java
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalHandlerFaultOutInterceptor.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/TestHandler.java
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/phase/PhaseInterceptorChain.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/phase/PhaseInterceptorChain.java?view=diff&rev=540423&r1=540422&r2=540423
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/phase/PhaseInterceptorChain.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/phase/PhaseInterceptorChain.java
Mon May 21 22:00:32 2007
@@ -144,7 +144,6 @@
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Invoking handleMessage on interceptor " +
currentInterceptor);
}
-
currentInterceptor.handleMessage(message);
} catch (RuntimeException ex) {
@@ -220,6 +219,8 @@
public synchronized void reset() {
if (state == State.COMPLETE) {
state = State.EXECUTING;
+ iterator.reset();
+ } else {
iterator.reset();
}
}
Modified:
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalHandlerFaultOutInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalHandlerFaultOutInterceptor.java?view=diff&rev=540423&r1=540422&r2=540423
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalHandlerFaultOutInterceptor.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalHandlerFaultOutInterceptor.java
Mon May 21 22:00:32 2007
@@ -28,10 +28,14 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.Binding;
+import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.InterceptorChain;
import org.apache.cxf.jaxws.handler.AbstractJAXWSHandlerInterceptor;
import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.FaultMode;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.staxutils.StaxUtils;
@@ -40,6 +44,8 @@
public class LogicalHandlerFaultOutInterceptor<T extends Message>
extends AbstractJAXWSHandlerInterceptor<T> {
+
+ public static final String ORIGINAL_WRITER = "original_writer";
public LogicalHandlerFaultOutInterceptor(Binding binding) {
super(binding);
@@ -59,12 +65,9 @@
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
-
-
- message.getInterceptorChain().add(new
LogicalHandlerFaultOutEndingInterceptor<T>(
- getBinding(),
- origWriter,
- writer));
+ message.put(ORIGINAL_WRITER, origWriter);
+
+ message.getInterceptorChain().add(new
LogicalHandlerFaultOutEndingInterceptor<T>(getBinding()));
} catch (ParserConfigurationException e) {
throw new Fault(e);
}
@@ -74,20 +77,17 @@
private class LogicalHandlerFaultOutEndingInterceptor<X extends Message>
extends AbstractJAXWSHandlerInterceptor<X> {
- XMLStreamWriter origWriter;
- W3CDOMStreamWriter domWriter;
-
- public LogicalHandlerFaultOutEndingInterceptor(Binding binding,
- XMLStreamWriter o,
- W3CDOMStreamWriter n) {
+ public LogicalHandlerFaultOutEndingInterceptor(Binding binding) {
super(binding);
- origWriter = o;
- domWriter = n;
-
+
setPhase(Phase.POST_MARSHAL);
}
- public void handleMessage(X message) throws Fault {
+ public void handleMessage(X message) throws Fault {
+ W3CDOMStreamWriter domWriter =
(W3CDOMStreamWriter)message.getContent(XMLStreamWriter.class);
+ XMLStreamWriter origWriter = (XMLStreamWriter)message
+ .get(LogicalHandlerFaultOutInterceptor.ORIGINAL_WRITER);
+
HandlerChainInvoker invoker = getInvoker(message);
LogicalMessageContextImpl lctx = new
LogicalMessageContextImpl(message);
invoker.setLogicalMessageContext(lctx);
@@ -101,15 +101,44 @@
message.removeContent(SOAPMessage.class);
} else if (domWriter.getDocument().getDocumentElement() != null) {
Source source = new DOMSource(domWriter.getDocument());
- XMLUtils.writeTo(domWriter.getDocument(), System.out);
message.setContent(Source.class, source);
message.setContent(XMLStreamReader.class,
StaxUtils.createXMLStreamReader(domWriter.getDocument()));
}
- if (!invoker.invokeLogicalHandlersHandleFault(requestor, lctx)) {
- //do nothing
- }
+ try {
+ if (!invoker.invokeLogicalHandlersHandleFault(requestor,
lctx)) {
+ // handleAbort(message, context);
+ }
+ } catch (RuntimeException exception) {
+ Exchange exchange = message.getExchange();
+
+ Exception ex = new Fault(exception);
+
+ FaultMode mode = (FaultMode)message.get(FaultMode.class);
+
+ Message faultMessage = exchange.getOutMessage();
+ if (null == faultMessage) {
+ faultMessage =
exchange.get(Endpoint.class).getBinding().createMessage();
+ }
+ faultMessage.setContent(Exception.class, ex);
+ if (null != mode) {
+ faultMessage.put(FaultMode.class, mode);
+ }
+ assert exchange.get(Exception.class) == ex;
+ exchange.setOutMessage(null);
+ exchange.setOutFaultMessage(faultMessage);
+
+ InterceptorChain ic = message.getInterceptorChain();
+ ic.reset();
+
+ onCompletion(message);
+
+ faultMessage.setInterceptorChain(ic);
+ ic.doIntercept(faultMessage);
+
+ return;
+ }
if (origMessage != null) {
message.setContent(SOAPMessage.class, origMessage);
@@ -127,9 +156,7 @@
} catch (XMLStreamException e) {
throw new Fault(e);
}
- }
-
+ }
}
-
}
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=540423&r1=540422&r2=540423
==============================================================================
---
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
Mon May 21 22:00:32 2007
@@ -742,8 +742,8 @@
handlerTest.pingWithArgs("soapHandler3 outbound throw
RuntimeException");
fail("did not get expected exception");
} catch (RuntimeException e) {
-/* //e.printStackTrace();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ //e.printStackTrace();
+/* ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
e.printStackTrace(ps);
assertTrue("Did not get expected exception message",
baos.toString()
@@ -788,8 +788,8 @@
+
"soapHandler4HandleFaultThrowsRunException");
fail("did not get expected WebServiceException");
} catch (WebServiceException e) {
-/* //e.printStackTrace();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ //e.printStackTrace();
+/* ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
e.printStackTrace(ps);
assertTrue("Did not get expected exception message",
baos.toString()
@@ -857,8 +857,8 @@
.indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
}
-/*
- assertEquals("handle message was not invoked", 1,
handler1.getHandleMessageInvoked());
+
+/* assertEquals("handle message was not invoked", 1,
handler1.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 1,
handler2.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 1,
soapHandler1.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 1,
soapHandler2.getHandleMessageInvoked());
@@ -878,7 +878,7 @@
assertTrue(soapHandler1.getInvokeOrderOfClose()
< handler2.getInvokeOrderOfClose());
assertTrue(handler2.getInvokeOrderOfClose()
- < handler1.getInvokeOrderOfClose()); */
+ < handler1.getInvokeOrderOfClose()); */
}
/*-------------------------------------------------------
@@ -912,7 +912,59 @@
.indexOf("RemoteException with nested RuntimeException") >
-1);*/
}
}
-
+
+ /*-------------------------------------------------------
+ * This is the expected order
+ *-------------------------------------------------------
+ * soapHandler3.handleMessage().doInbound()
+ * soapHandler4.handleMessage().doInbound()
+ * handler2.handleMessage().doInbound()
+ * handler1.handleMessage().doInbound()
+ * handler1 Throwing an inbound ProtocolException
+ * handler2.handleFault()
+ * handler2 Throwing an outbound RuntimeException
+ * handler1.close()
+ * handler1.close()
+ * soapHandler4.close()
+ * soapHandler3.close()
+ */
+ @Test
+ public void
testLogicalHandlerHandleFaultThrowsRuntimeExceptionServerOutbound() throws
PingException {
+ try {
+ handlerTest.pingWithArgs("handler1 inbound throw ProtocolException
"
+ +
"handler2HandleFaultThrowsRunException");
+ 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("handler2 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
testLogicalHandlerHandleFaultThrowsSOAPFaultExceptionServerOutbound() throws
PingException {
+ try {
+ handlerTest.pingWithArgs("handler1 inbound throw ProtocolException
"
+ +
"handler2HandleFaultThrowsSOAPFaultException");
+ fail("did not get expected SOAPFaultException");
+ } catch (SOAPFaultException 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("handler2 HandleFault throws SOAPFaultException") >
-1);
+ assertTrue("Did not get expected
javax.xml.ws.soap.SOAPFaultException", baos.toString()
+ .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
+ }
+ }
+
@Test
@Ignore
public void
testLogicalHandlerHandleMessageThrowsProtocolExceptionServerInbound()
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestHandler.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestHandler.java?view=diff&rev=540423&r1=540422&r2=540423
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestHandler.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/TestHandler.java
Mon May 21 22:00:32 2007
@@ -19,18 +19,31 @@
package org.apache.cxf.systest.handlers;
+import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
+import javax.xml.namespace.QName;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPFactory;
+import javax.xml.soap.SOAPFault;
+import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.LogicalMessage;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.soap.SOAPFaultException;
+import javax.xml.xpath.XPathConstants;
+
+import org.w3c.dom.Node;
+
+import org.apache.cxf.binding.soap.Soap11;
import org.apache.cxf.common.util.PackageUtils;
+import org.apache.cxf.helpers.XPathUtils;
import org.apache.handler_test.PingException;
import org.apache.handler_test.types.Ping;
import org.apache.handler_test.types.PingResponse;
@@ -127,20 +140,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
runtime 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);
}
}
@@ -169,16 +186,52 @@
} else if (obj instanceof Ping || obj instanceof PingWithArgs) {
getHandlerInfoList(ctx).add(getHandlerId());
}
- }
-
+ }
- public boolean handleFault(T ctx) {
- methodCalled("handleFault");
+ public final boolean handleFault(T ctx) {
+ methodCalled("handleFault");
printHandlerInfo("handleFault", isOutbound(ctx));
- //boolean outbound =
(Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
+
+ if (isServerSideHandler()) {
+
+ if (!"handler2".equals(getHandlerId())) {
+ return true;
+ }
+
+ DOMSource source = (DOMSource)ctx.getMessage().getPayload();
+ Node node = source.getNode();
+
+ Map<String, String> ns = new HashMap<String, String>();
+ ns.put("s", Soap11.SOAP_NAMESPACE);
+ XPathUtils xu = new XPathUtils(ns);
+ String exceptionText =
(String)xu.getValue("//s:Fault/faultstring/text()", node,
+ XPathConstants.STRING);
+ //XMLUtils.writeTo(node, System.out);
+
+ if ("handler2HandleFaultThrowsRunException".equals(exceptionText))
{
+ throw new RuntimeException("handler2 HandleFault throws
RuntimeException");
+ } else if
("handler2HandleFaultThrowsSOAPFaultException".equals(exceptionText)) {
+ throw createSOAPFaultException("handler2 HandleFault "
+ + "throws SOAPFaultException");
+ }
+
+ }
+
return true;
}
+ private SOAPFaultException createSOAPFaultException(String faultString) {
+ try {
+ SOAPFault fault = SOAPFactory.newInstance().createFault();
+ fault.setFaultString(faultString);
+ fault.setFaultCode(new QName("http://cxf.apache.org/faultcode",
"Server"));
+ return new SOAPFaultException(fault);
+ } catch (SOAPException e) {
+ // do nothing
+ }
+ return null;
+ }
+
public void close(MessageContext arg0) {
methodCalled("close");
}