use of javax.xml.ws.ProtocolException in project cxf by apache.
the class SmallNumberHandler method handleMessage.
// Implementation of javax.xml.ws.handler.Handler
public final boolean handleMessage(LogicalMessageContext messageContext) {
System.out.println("LogicalMessageHandler handleMessage called");
try {
boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
// get the LogicalMessage from our context
//
LogicalMessage msg = messageContext.getMessage();
// check the payload, if its an AddNumbers request, we'll intervene
//
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Object payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement<?>) payload).getValue();
}
if (payload instanceof AddNumbers) {
AddNumbers req = (AddNumbers) payload;
// now, if the arguments are small, let's do the calculation here
//
int a = req.getArg0();
int b = req.getArg1();
if (isSmall(a) && isSmall(b)) {
int answer = a + b;
// System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
// ok, we've done the calculation, so build the
// response and set it as the payload of the message
AddNumbersResponse resp = new AddNumbersResponse();
resp.setReturn(answer);
msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext);
Source src = msg.getPayload();
msg.setPayload(src);
payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement<?>) payload).getValue();
}
AddNumbersResponse resp2 = (AddNumbersResponse) payload;
if (resp2 == resp) {
throw new WebServiceException("Shouldn't be the same object");
}
// returned to the client
return false;
}
}
}
return true;
} catch (JAXBException ex) {
throw new ProtocolException(ex);
}
}
use of javax.xml.ws.ProtocolException in project cxf by apache.
the class HandlerChainInvokerTest method testHandleFaultReturnsFalse.
// JAXB spec 9.3.2.2: Return false This indicates that fault message processing
// should cease. Fault message processing stops, close is called on each previously invoked
// handler in the chain, the fault message is dispatched
@Test
public void testHandleFaultReturnsFalse() {
ProtocolException pe = new ProtocolException("banzai");
logicalHandlers[3].setException(pe);
invoker.setRequestor(true);
logicalHandlers[0].setHandleFaultRet(true);
logicalHandlers[1].setHandleFaultRet(true);
logicalHandlers[2].setHandleFaultRet(false);
logicalHandlers[3].setHandleFaultRet(true);
boolean continueProcessing = false;
try {
continueProcessing = invoker.invokeLogicalHandlers(false, lmc);
fail("did not get expected exception");
} catch (RuntimeException e) {
assertEquals("banzai", e.getMessage());
}
assertFalse(continueProcessing);
// assertTrue(invoker.isClosed());
assertEquals(1, logicalHandlers[0].getHandleMessageCount());
assertEquals(1, logicalHandlers[1].getHandleMessageCount());
assertEquals(1, logicalHandlers[2].getHandleMessageCount());
assertEquals(1, logicalHandlers[3].getHandleMessageCount());
assertEquals(0, logicalHandlers[0].getHandleFaultCount());
assertEquals(0, logicalHandlers[1].getHandleFaultCount());
assertEquals(1, logicalHandlers[2].getHandleFaultCount());
assertEquals(0, logicalHandlers[3].getHandleFaultCount());
assertEquals(1, logicalHandlers[0].getCloseCount());
assertEquals(1, logicalHandlers[1].getCloseCount());
assertEquals(1, logicalHandlers[2].getCloseCount());
assertEquals(1, logicalHandlers[3].getCloseCount());
assertTrue(logicalHandlers[3].getInvokeOrderOfClose() < logicalHandlers[2].getInvokeOrderOfClose());
assertTrue(logicalHandlers[2].getInvokeOrderOfClose() < logicalHandlers[1].getInvokeOrderOfClose());
assertTrue(logicalHandlers[1].getInvokeOrderOfClose() < logicalHandlers[0].getInvokeOrderOfClose());
}
use of javax.xml.ws.ProtocolException in project cxf by apache.
the class HandlerChainInvokerTest method testFaultRaised.
@Test
public void testFaultRaised() {
assertFalse(invoker.faultRaised());
invoker.setFault(new ProtocolException("test exception"));
assertTrue(invoker.faultRaised());
// reset
invoker.setFault(null);
assertFalse(invoker.faultRaised());
invoker.setFault(true);
assertTrue(invoker.faultRaised());
// reset
invoker.setFault(false);
invoker.setFault(null);
assertFalse(invoker.faultRaised());
invoker.setFault(true);
invoker.setFault(new ProtocolException("test exception"));
}
use of javax.xml.ws.ProtocolException in project cxf by apache.
the class HandlerChainInvokerTest method testHandleFaultThrowsRuntimeException.
// JAXB spec 9.3.2.2: Throw any other runtime exception This indicates
// that fault message processing should cease. Fault message processing stops,
// close is called on each previously invoked handler in the chain, the exception is
// dispatched
@Test
public void testHandleFaultThrowsRuntimeException() {
ProtocolException pe = new ProtocolException("banzai");
RuntimeException re = new RuntimeException("banzai");
// throw exception during handleFault processing
logicalHandlers[2].setException(pe);
logicalHandlers[1].setFaultException(re);
invoker.setRequestor(true);
boolean continueProcessing = false;
try {
continueProcessing = invoker.invokeLogicalHandlers(false, lmc);
fail("did not get expected exception");
} catch (RuntimeException e) {
assertEquals("banzai", e.getMessage());
}
assertFalse(continueProcessing);
assertTrue(invoker.isClosed());
assertEquals(1, logicalHandlers[0].getHandleMessageCount());
assertEquals(1, logicalHandlers[1].getHandleMessageCount());
assertEquals(1, logicalHandlers[2].getHandleMessageCount());
assertEquals(0, logicalHandlers[3].getHandleMessageCount());
assertEquals(0, logicalHandlers[0].getHandleFaultCount());
assertEquals(1, logicalHandlers[1].getHandleFaultCount());
assertEquals(0, logicalHandlers[2].getHandleFaultCount());
assertEquals(1, logicalHandlers[0].getCloseCount());
assertEquals(1, logicalHandlers[1].getCloseCount());
assertEquals(1, logicalHandlers[2].getCloseCount());
assertEquals(0, logicalHandlers[3].getCloseCount());
assertTrue(logicalHandlers[2].getInvokeOrderOfClose() < logicalHandlers[1].getInvokeOrderOfClose());
assertTrue(logicalHandlers[1].getInvokeOrderOfClose() < logicalHandlers[0].getInvokeOrderOfClose());
}
use of javax.xml.ws.ProtocolException in project cxf by apache.
the class HandlerChainInvoker method invokeHandlerChainHandleFault.
/*
* REVISIT: the logic of current implemetation is if the exception is thrown
* from previous handlers, we only invoke handleFault if it is
* ProtocolException (per spec), if the exception is thrown from other
* places other than handlers, we always invoke handleFault.
*/
private boolean invokeHandlerChainHandleFault(List<? extends Handler<?>> handlerChain, MessageContext ctx) {
if (handlerChain.isEmpty()) {
LOG.log(Level.FINEST, "no handlers registered");
return true;
}
if (isClosed()) {
return false;
}
// if the fault is a ProtocolException
if (fault != null) {
if (!(fault instanceof ProtocolException)) {
return true;
} else if (!responseExpected && !messageDirectionReversed) {
// in the chain, the exception is dispatched (see section 9.1.2.3).
return true;
}
}
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "invoking handlers, direction: " + (outbound ? "outbound" : "inbound"));
}
setMessageOutboundProperty(ctx);
if (!outbound) {
handlerChain = reverseHandlerChain(handlerChain);
}
boolean continueProcessing;
MessageContext oldCtx = null;
try {
oldCtx = WebServiceContextImpl.setMessageContext(ctx);
continueProcessing = invokeHandleFault(handlerChain, ctx);
} finally {
// restore the WebServiceContextImpl's ThreadLocal variable to the previous value
if (oldCtx == null) {
WebServiceContextImpl.clear();
} else {
WebServiceContextImpl.setMessageContext(oldCtx);
}
}
return continueProcessing;
}
Aggregations