Search in sources :

Example 31 with MessageContext

use of javax.xml.ws.handler.MessageContext 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 32 with MessageContext

use of javax.xml.ws.handler.MessageContext in project cxf by apache.

the class SOAPHandlerFaultInInterceptor method handleMessage.

public void handleMessage(SoapMessage message) {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    if (getInvoker(message).getProtocolHandlers().isEmpty()) {
        return;
    }
    checkUnderstoodHeaders(message);
    MessageContext context = createProtocolMessageContext(message);
    HandlerChainInvoker invoker = getInvoker(message);
    invoker.setProtocolMessageContext(context);
    if (!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
        handleAbort(message, context);
    }
    SOAPMessage msg = message.getContent(SOAPMessage.class);
    if (msg != null) {
        XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(msg);
        message.setContent(XMLStreamReader.class, xmlReader);
    }
}
Also used : HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) XMLStreamReader(javax.xml.stream.XMLStreamReader) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 33 with MessageContext

use of javax.xml.ws.handler.MessageContext in project cxf by apache.

the class WebServiceContextImplTest method testGetSetMessageContext.

@Test
public void testGetSetMessageContext() {
    WebServiceContextImpl wsci = new WebServiceContextImpl();
    assertNull(wsci.getMessageContext());
    MessageImpl msg = new MessageImpl();
    final MessageContext ctx = new WrappedMessageContext(msg);
    WebServiceContextImpl.setMessageContext(ctx);
    assertSame(ctx, wsci.getMessageContext());
    Thread t = new Thread() {

        public void run() {
            WebServiceContextImpl threadLocalWSCI = new WebServiceContextImpl();
            assertNull(threadLocalWSCI.getMessageContext());
            MessageImpl msg1 = new MessageImpl();
            MessageContext threadLocalCtx = new WrappedMessageContext(msg1);
            WebServiceContextImpl.setMessageContext(threadLocalCtx);
            assertSame(threadLocalCtx, threadLocalWSCI.getMessageContext());
            assertTrue(ctx != threadLocalWSCI.getMessageContext());
        }
    };
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 34 with MessageContext

use of javax.xml.ws.handler.MessageContext in project cxf by apache.

the class LogicalHandlerInterceptorTest method xtestReturnFalseClientSide.

// JAX-WS spec: If handler returns false, for a request-response MEP, if the message
// direction is reversed during processing of a request message then the message
// becomes a response message.
// NOTE: commented out as this has been covered by other tests.
@Test
@org.junit.Ignore
public void xtestReturnFalseClientSide() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new LogicalHandler<LogicalMessageContext>() {

        public void close(MessageContext arg0) {
        }

        public boolean handleFault(LogicalMessageContext messageContext) {
            return true;
        }

        public boolean handleMessage(LogicalMessageContext messageContext) {
            LogicalMessage msg = messageContext.getMessage();
            AddNumbersResponse resp = new AddNumbersResponse();
            resp.setReturn(11);
            msg.setPayload(resp, null);
            return false;
        }
    });
    HandlerChainInvoker invoker1 = new HandlerChainInvoker(list);
    IMocksControl control1 = createNiceControl();
    Binding binding1 = control1.createMock(Binding.class);
    @SuppressWarnings("rawtypes") List<Handler> hList = CastUtils.cast(list);
    expect(binding1.getHandlerChain()).andReturn(hList).anyTimes();
    Exchange exchange1 = control1.createMock(Exchange.class);
    expect(exchange1.get(HandlerChainInvoker.class)).andReturn(invoker1).anyTimes();
    Message outMessage = new MessageImpl();
    outMessage.setExchange(exchange1);
    InterceptorChain chain = control1.createMock(InterceptorChain.class);
    outMessage.setInterceptorChain(chain);
    chain.abort();
    EasyMock.expectLastCall();
    MessageObserver observer = control1.createMock(MessageObserver.class);
    expect(exchange1.get(MessageObserver.class)).andReturn(observer).anyTimes();
    observer.onMessage(isA(Message.class));
    EasyMock.expectLastCall();
    control1.replay();
    LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding1);
    li.handleMessage(outMessage);
    control1.verify();
}
Also used : Binding(javax.xml.ws.Binding) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageObserver(org.apache.cxf.transport.MessageObserver) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 35 with MessageContext

use of javax.xml.ws.handler.MessageContext in project cxf by apache.

the class LogicalHandlerInterceptorTest method testInterceptSuccess.

@Test
public void testInterceptSuccess() {
    List<LogicalHandler<?>> list = new ArrayList<LogicalHandler<?>>();
    list.add(new LogicalHandler<LogicalMessageContext>() {

        public void close(MessageContext arg0) {
        }

        public boolean handleFault(LogicalMessageContext arg0) {
            return true;
        }

        public boolean handleMessage(LogicalMessageContext arg0) {
            return true;
        }
    });
    @SuppressWarnings("rawtypes") List<Handler> hList = CastUtils.cast(list);
    expect(binding.getHandlerChain()).andReturn(hList).anyTimes();
    expect(invoker.getLogicalHandlers()).andReturn(list);
    expect(message.getExchange()).andReturn(exchange).anyTimes();
    expect(message.get(Message.REQUESTOR_ROLE)).andReturn(Boolean.TRUE).anyTimes();
    expect(message.keySet()).andReturn(new TreeSet<String>()).anyTimes();
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker);
    expect(exchange.getOutMessage()).andReturn(message);
    expect(invoker.invokeLogicalHandlers(eq(true), isA(LogicalMessageContext.class))).andReturn(true);
    control.replay();
    LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding);
    assertEquals("unexpected phase", "pre-protocol-frontend", li.getPhase());
    li.handleMessage(message);
    control.verify();
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TreeSet(java.util.TreeSet) LogicalHandler(javax.xml.ws.handler.LogicalHandler) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) Test(org.junit.Test)

Aggregations

MessageContext (javax.xml.ws.handler.MessageContext)46 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 QName (javax.xml.namespace.QName)10 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)9 Exchange (org.apache.cxf.message.Exchange)9 MessageImpl (org.apache.cxf.message.MessageImpl)9 HandlerChainInvoker (org.apache.cxf.jaxws.handler.HandlerChainInvoker)8 List (java.util.List)7 Header (org.apache.cxf.headers.Header)7 IOException (java.io.IOException)6 Handler (javax.xml.ws.handler.Handler)6 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpSession (javax.servlet.http.HttpSession)5 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 Binding (javax.xml.ws.Binding)5