Author: jliu
Date: Thu May 3 09:27:00 2007
New Revision: 534925
URL: http://svn.apache.org/viewvc?view=rev&rev=534925
Log:
CXF-630: When JAX-WS handlers' handleMessage throws ProtocolException, the
message direction is reversed, and the Message should be replaced with a fault
Message
Modified:
incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/HandlerChainInvoker.java
incubator/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/handler/HandlerChainInvokerTest.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=534925&r1=534924&r2=534925
==============================================================================
---
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
Thu May 3 09:27:00 2007
@@ -26,11 +26,17 @@
import java.util.logging.Level;
import java.util.logging.Logger;
+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.ProtocolException;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.jaxws.context.WebServiceContextImpl;
@@ -236,15 +242,7 @@
}
if (!continueProcessing) {
- // stop processing handlers, change direction and return
- // control to the bindng. Then the binding will invoke on
- // the next set on handlers and they will be processing in
- // the correct direction. It would be good refactor it
- // and control all of the processing here.
- //changeMessageDirection(ctx);
handlerProcessingAborted = true;
-
- //TODO: reverse chain, call handlerMessage or close
}
return continueProcessing;
}
@@ -301,6 +299,7 @@
LOG.log(Level.FINE, "handleMessage raised exception", e);
changeMessageDirection(ctx);
if (responseExpected) {
+ setFaultMessage(ctx, e);
invokeReversedHandleFault(ctx);
} else {
invokeReversedClose();
@@ -320,10 +319,29 @@
return continueProcessing;
}
-
- //The message direction is reversed, if the message is not already a fault
message then it
- //is replaced with a fault message, and the runtime invokes handleFault on
the next handler
- //or dispatches the message if there are no further handlers.
+ //When the message direction is reversed, if the message is not already a
fault message then it is
+ //replaced with a fault message
+ private void setFaultMessage(MessageContext mc, Exception exception) {
+ if (mc instanceof LogicalMessageContext) {
+
((WrappedMessageContext)mc).getWrappedMessage().setContent(Exception.class,
exception);
+ } else if (mc instanceof SOAPMessageContext) {
+ try {
+ SOAPMessage soapMessage =
((SOAPMessageContext)mc).getMessage();
+ SOAPBody body = soapMessage.getSOAPBody();
+ // Fault f = (Fault)exception;
+ SOAPFault soapFault = body.addFault();
+ soapFault.setFaultCode(new
QName("http://cxf.apache.org/faultcode", "HandlerFault"));
+ soapFault.setFaultString(exception.getMessage());
+ } catch (SOAPException e) {
+ e.printStackTrace();
+ // do nothing
+ }
+ }
+ }
+
+ // The message direction is reversed, if the message is not already a
fault message then it
+ // is replaced with a fault message, and the runtime invokes handleFault on
+ // the next handler or dispatches the message if there are no further
handlers.
@SuppressWarnings("unchecked")
private boolean invokeReversedHandleFault(MessageContext ctx) {
boolean continueProcessing = true;
Modified:
incubator/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/handler/HandlerChainInvokerTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/handler/HandlerChainInvokerTest.java?view=diff&rev=534925&r1=534924&r2=534925
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/handler/HandlerChainInvokerTest.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/handler/HandlerChainInvokerTest.java
Thu May 3 09:27:00 2007
@@ -27,11 +27,13 @@
import java.util.List;
import java.util.Map;
+import javax.xml.soap.MessageFactory;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.handler.Handler;
@@ -42,6 +44,7 @@
import junit.framework.TestCase;
import org.apache.cxf.jaxws.context.WrappedMessageContext;
+import org.apache.cxf.jaxws.handler.soap.SOAPMessageContextImpl;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageImpl;
@@ -53,7 +56,6 @@
Message message;
LogicalMessageContext lmc;
MessageContext pmc;
- Source payload;
TestLogicalHandler[] logicalHandlers = new
TestLogicalHandler[HANDLER_COUNT];
TestProtocolHandler[] protocolHandlers = new
TestProtocolHandler[HANDLER_COUNT];
@@ -76,9 +78,9 @@
message = new MessageImpl();
lmc = new LogicalMessageContextImpl(message);
pmc = new WrappedMessageContext(message);
-
+/*
payload = new DOMSource();
- message.setContent(Source.class, payload);
+ message.setContent(Source.class, payload);*/
}
@@ -233,6 +235,16 @@
continueProcessing = invoker.invokeLogicalHandlers(false, lmc);
assertTrue(continueProcessing);
+ //create an empty SOAP body for testing
+ try {
+ pmc = new SOAPMessageContextImpl(message);
+ MessageFactory factory = MessageFactory.newInstance();
+ SOAPMessage soapMessage = factory.createMessage();
+ ((SOAPMessageContext)pmc).setMessage(soapMessage);
+ } catch (SOAPException e) {
+ //do nothing
+ }
+
try {
invoker.setProtocolMessageContext(pmc);
continueProcessing = invoker.invokeProtocolHandlers(false, pmc);
@@ -244,6 +256,12 @@
assertFalse((Boolean)pmc.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY));
assertFalse((Boolean)lmc.get(LogicalMessageContext.MESSAGE_OUTBOUND_PROPERTY));
assertTrue(invoker.isInbound());
+
+ //the message is replaced by fault message
+ Source responseMessage = lmc.getMessage().getPayload();
+ System.out.println(getSourceAsString(responseMessage));
+ assertTrue(getSourceAsString(responseMessage).indexOf("banzai") > -1);
+
//assertFalse(continueProcessing);
assertEquals(1, logicalHandlers[0].getHandleMessageCount());
@@ -412,7 +430,6 @@
assertFalse(continueProcessing);
assertFalse((Boolean)lmc.get(LogicalMessageContext.MESSAGE_OUTBOUND_PROPERTY));
- assertEquals(payload, lmc.getMessage().getPayload());
assertEquals(2, logicalHandlers[0].getHandleMessageCount());
assertEquals(2, logicalHandlers[1].getHandleMessageCount());
@@ -504,10 +521,7 @@
assertSame(pe, invoker.getFault());
assertFalse((Boolean)lmc.get(LogicalMessageContext.MESSAGE_OUTBOUND_PROPERTY));
- Source responseMessage = lmc.getMessage().getPayload();
- System.out.println(getSourceAsString(responseMessage));
- //assertTrue(getSourceAsString(responseMessage).indexOf("banzai") >
-1);
-
+
assertEquals(1, logicalHandlers[0].getHandleMessageCount());
assertEquals(1, logicalHandlers[1].getHandleMessageCount());
assertEquals(1, logicalHandlers[2].getHandleMessageCount());