Search in sources :

Example 6 with MessageContext

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

the class SOAPHandlerInterceptorTest method testGetUnderstoodHeadersReturnsNull.

@Test
public void testGetUnderstoodHeadersReturnsNull() {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    SoapMessage message = control.createMock(SoapMessage.class);
    Exchange exchange = control.createMock(Exchange.class);
    expect(message.getExchange()).andReturn(exchange).anyTimes();
    expect(message.keySet()).andReturn(new HashSet<>());
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker);
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    Set<QName> understood = li.getUnderstoodHeaders();
    assertNotNull(understood);
    assertTrue(understood.isEmpty());
}
Also used : Binding(javax.xml.ws.Binding) Set(java.util.Set) HashSet(java.util.HashSet) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) Test(org.junit.Test)

Example 7 with MessageContext

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

the class WebServiceContextImpl method setMessageContext.

/**
 * Sets reference to the specified MessageContext and returns the previous reference, if any.
 *
 * @param ctx       The MessageContext to set
 * @return          The former MessageContext reference, if any.
 */
public static MessageContext setMessageContext(MessageContext ctx) {
    MessageContext oldCtx = context.get();
    context.set(ctx);
    return oldCtx;
}
Also used : MessageContext(javax.xml.ws.handler.MessageContext)

Example 8 with MessageContext

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

the class HandlerChainInvoker method invokeHandlerChain.

private boolean invokeHandlerChain(List<? extends Handler<?>> handlerChain, MessageContext ctx) {
    if (handlerChain.isEmpty()) {
        LOG.log(Level.FINEST, "no handlers registered");
        return true;
    }
    if (isClosed()) {
        return false;
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "invoking handlers, direction: " + (outbound ? "outbound" : "inbound"));
    }
    if (!outbound) {
        handlerChain = reverseHandlerChain(handlerChain);
    }
    boolean continueProcessing = true;
    MessageContext oldCtx = null;
    try {
        oldCtx = WebServiceContextImpl.setMessageContext(ctx);
        continueProcessing = invokeHandleMessage(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 : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext)

Example 9 with MessageContext

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

the class SOAPHandlerFaultOutInterceptor method handleMessageInternal.

private void handleMessageInternal(SoapMessage message) {
    MessageContext context = createProtocolMessageContext(message);
    HandlerChainInvoker invoker = getInvoker(message);
    invoker.setProtocolMessageContext(context);
    try {
        if (!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
        // handleAbort(message, context);
        }
    } catch (RuntimeException exception) {
        /*
             * handleFault throws exception, in this case we need to replace
             * SOAPFault with the exception thrown from HandleFault so that the
             * exception can be dispatched.
             */
        try {
            SOAPMessage originalMsg = message.getContent(SOAPMessage.class);
            SOAPBody body = originalMsg.getSOAPPart().getEnvelope().getBody();
            body.removeContents();
            SOAPFault soapFault = body.addFault();
            if (exception instanceof SOAPFaultException) {
                SOAPFaultException sf = (SOAPFaultException) exception;
                soapFault.setFaultString(sf.getFault().getFaultString());
                SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
                soapFault.setFaultActor(sf.getFault().getFaultActor());
                if (sf.getFault().hasDetail()) {
                    Node nd = originalMsg.getSOAPPart().importNode(sf.getFault().getDetail().getFirstChild(), true);
                    soapFault.addDetail().appendChild(nd);
                }
            } else if (exception instanceof Fault) {
                SoapFault sf = SoapFault.createFault((Fault) exception, message.getVersion());
                soapFault.setFaultString(sf.getReason());
                SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
                if (sf.hasDetails()) {
                    soapFault.addDetail();
                    Node nd = originalMsg.getSOAPPart().importNode(sf.getDetail(), true);
                    nd = nd.getFirstChild();
                    while (nd != null) {
                        soapFault.getDetail().appendChild(nd);
                        nd = nd.getNextSibling();
                    }
                }
            } else {
                soapFault.setFaultString(exception.getMessage());
                SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandleFault"));
            }
        } catch (SOAPException e) {
            // do nothing
            e.printStackTrace();
        }
    }
    onCompletion(message);
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SoapFault(org.apache.cxf.binding.soap.SoapFault) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) SOAPException(javax.xml.soap.SOAPException) SOAPFault(javax.xml.soap.SOAPFault) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Fault(org.apache.cxf.interceptor.Fault) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 10 with MessageContext

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

the class SOAPHandlerInterceptor method handleMessageInternal.

private boolean handleMessageInternal(SoapMessage message) {
    MessageContext context = createProtocolMessageContext(message);
    if (context == null) {
        return true;
    }
    HandlerChainInvoker invoker = getInvoker(message);
    invoker.setProtocolMessageContext(context);
    if (!invoker.invokeProtocolHandlers(isRequestor(message), context)) {
        handleAbort(message, context);
    }
    // If this is the outbound and end of MEP, call MEP completion
    if (isRequestor(message) && invoker.getLogicalHandlers().isEmpty() && !isOutbound(message) && isMEPComlete(message)) {
        onCompletion(message);
    } else if (isOutbound(message) && isMEPComlete(message)) {
        onCompletion(message);
    }
    return false;
}
Also used : HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext)

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