Search in sources :

Example 26 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 27 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)

Example 28 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 29 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 30 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;
    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)

Aggregations

ProtocolException (javax.xml.ws.ProtocolException)32 Test (org.junit.Test)10 InstanceIdentifier (com.arjuna.webservices11.wsarj.InstanceIdentifier)9 SOAPException (javax.xml.soap.SOAPException)9 SOAPFactory (javax.xml.soap.SOAPFactory)9 SOAPFault (javax.xml.soap.SOAPFault)9 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)9 SOAPMessage (javax.xml.soap.SOAPMessage)8 JAXBContext (javax.xml.bind.JAXBContext)7 JAXBException (javax.xml.bind.JAXBException)7 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)6 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)6 CoordinationContextType (org.oasis_open.docs.ws_tx.wscoor._2006._06.CoordinationContextType)5 LogicalMessage (javax.xml.ws.LogicalMessage)4 SystemException (com.arjuna.wst.SystemException)3 UnknownTransactionException (com.arjuna.wst.UnknownTransactionException)3 BusinessActivityTerminator (com.arjuna.wst11.BusinessActivityTerminator)3 JAXBElement (javax.xml.bind.JAXBElement)3 SOAPHeader (javax.xml.soap.SOAPHeader)3 Source (javax.xml.transform.Source)3