Search in sources :

Example 11 with ProtocolException

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);
    }
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) WebServiceException(javax.xml.ws.WebServiceException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) JAXBElement(javax.xml.bind.JAXBElement) Source(javax.xml.transform.Source) ObjectFactory(org.apache.handlers.types.ObjectFactory) AddNumbers(org.apache.handlers.types.AddNumbers) LogicalMessage(javax.xml.ws.LogicalMessage)

Example 12 with ProtocolException

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 = true;
    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;
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext)

Example 13 with ProtocolException

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"));
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) Test(org.junit.Test)

Example 14 with ProtocolException

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());
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) Test(org.junit.Test)

Example 15 with ProtocolException

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());
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) Test(org.junit.Test)

Aggregations

ProtocolException (javax.xml.ws.ProtocolException)17 Test (org.junit.Test)10 LogicalMessage (javax.xml.ws.LogicalMessage)4 JAXBContext (javax.xml.bind.JAXBContext)3 JAXBElement (javax.xml.bind.JAXBElement)3 JAXBException (javax.xml.bind.JAXBException)3 Source (javax.xml.transform.Source)3 AddNumbers (org.apache.handlers.types.AddNumbers)3 StringTokenizer (java.util.StringTokenizer)2 SOAPException (javax.xml.soap.SOAPException)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 WebServiceException (javax.xml.ws.WebServiceException)2 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)2 PingResponse (org.apache.handler_test.types.PingResponse)2 PingWithArgs (org.apache.handler_test.types.PingWithArgs)2 AddNumbersResponse (org.apache.handlers.types.AddNumbersResponse)2 ObjectFactory (org.apache.handlers.types.ObjectFactory)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MessageFactory (javax.xml.soap.MessageFactory)1 SOAPBody (javax.xml.soap.SOAPBody)1