Search in sources :

Example 6 with HandlerChainInvoker

use of org.apache.cxf.jaxws.handler.HandlerChainInvoker in project cxf by apache.

the class LogicalHandlerFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    HandlerChainInvoker invoker = getInvoker(message);
    if (invoker.getLogicalHandlers().isEmpty()) {
        return;
    }
    XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
    Document doc = DOMUtils.newDocument();
    message.setContent(Node.class, doc);
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter(doc);
    // set up the namespace context
    try {
        writer.setNamespaceContext(origWriter.getNamespaceContext());
    } catch (XMLStreamException ex) {
    // don't set the namespaceContext
    }
    // Replace stax writer with DomStreamWriter
    message.setContent(XMLStreamWriter.class, writer);
    message.put(ORIGINAL_WRITER, origWriter);
    message.getInterceptorChain().add(ending);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Document(org.w3c.dom.Document)

Example 7 with HandlerChainInvoker

use of org.apache.cxf.jaxws.handler.HandlerChainInvoker in project cxf by apache.

the class LogicalHandlerInInterceptor method handleMessage.

@Override
public void handleMessage(Message message) {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    HandlerChainInvoker invoker = getInvoker(message);
    if (invoker.getLogicalHandlers().isEmpty()) {
        return;
    }
    LogicalMessageContextImpl lctx = new LogicalMessageContextImpl(message);
    invoker.setLogicalMessageContext(lctx);
    boolean requestor = isRequestor(message);
    if (!requestor) {
        setupBindingOperationInfo(message.getExchange(), lctx);
    }
    if (!invoker.invokeLogicalHandlers(requestor, lctx)) {
        if (!requestor) {
            // server side
            handleAbort(message, null);
        } else {
        // Client side inbound, thus no response expected, do nothing, the close will
        // be handled by MEPComplete later
        }
    }
    // If this is the inbound and end of MEP, call MEP completion
    if (!isOutbound(message) && isMEPComlete(message)) {
        onCompletion(message);
    }
}
Also used : HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker)

Example 8 with HandlerChainInvoker

use of org.apache.cxf.jaxws.handler.HandlerChainInvoker 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 9 with HandlerChainInvoker

use of org.apache.cxf.jaxws.handler.HandlerChainInvoker in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderInBound.

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

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (!outboundProperty.booleanValue()) {
                    // change mustUnderstand to false
                    SOAPMessage message = smc.getMessage();
                    SOAPHeader soapHeader = message.getSOAPHeader();
                    Element headerElementNew = (Element) soapHeader.getFirstChild();
                    SoapVersion soapVersion = Soap11.getInstance();
                    Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
                    attr.setValue("false");
                    headerElementNew.setAttributeNodeNS(attr);
                }
            } catch (Exception e) {
                throw new Fault(e);
            }
            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();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    // This is to set direction to inbound
    expect(exchange.getOutMessage()).andReturn(null);
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
    message.setContent(XMLStreamReader.class, reader);
    Object[] headerInfo = prepareSOAPHeader();
    message.setContent(Node.class, headerInfo[0]);
    Node node = ((Element) headerInfo[1]).getFirstChild();
    message.getHeaders().add(new Header(new QName(node.getNamespaceURI(), node.getLocalName()), node));
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    Element headerElementNew = DOMUtils.getFirstElement(soapMessageNew.getSOAPHeader());
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    // Verify XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
    // Verify Header Element
    Iterator<Header> iter = message.getHeaders().iterator();
    Element requiredHeader = null;
    while (iter.hasNext()) {
        Header localHdr = iter.next();
        if (localHdr.getObject() instanceof Element) {
            Element elem = (Element) localHdr.getObject();
            if (elem.getNamespaceURI().equals("http://apache.org/hello_world_rpclit/types") && elem.getLocalName().equals("header1")) {
                requiredHeader = (Element) localHdr.getObject();
                break;
            }
        }
    }
    assertNotNull("Should have found header1", requiredHeader);
    assertEquals("false", requiredHeader.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) Attr(org.w3c.dom.Attr) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) Binding(javax.xml.ws.Binding) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPHeader(javax.xml.soap.SOAPHeader) Header(org.apache.cxf.headers.Header) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 10 with HandlerChainInvoker

use of org.apache.cxf.jaxws.handler.HandlerChainInvoker in project cxf by apache.

the class SOAPHandlerInterceptorTest method testGetSOAPMessageInBound.

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

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                smc.getMessage();
            } catch (Exception e) {
                throw new Fault(e);
            }
            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);
    Exchange exchange = control.createMock(Exchange.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    // This is to set direction to inbound
    expect(exchange.getOutMessage()).andReturn(null);
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
    message.setContent(XMLStreamReader.class, reader);
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPBody bodyNew = soapMessageNew.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements();
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    assertEquals("sendReceiveData", bodyElementNew.getLocalName());
    // Verify the XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
}
Also used : Binding(javax.xml.ws.Binding) Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) SOAPBody(javax.xml.soap.SOAPBody) 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) MessageImpl(org.apache.cxf.message.MessageImpl) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) Test(org.junit.Test)

Aggregations

HandlerChainInvoker (org.apache.cxf.jaxws.handler.HandlerChainInvoker)12 MessageContext (javax.xml.ws.handler.MessageContext)8 SOAPMessage (javax.xml.soap.SOAPMessage)7 QName (javax.xml.namespace.QName)6 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Binding (javax.xml.ws.Binding)5 Handler (javax.xml.ws.handler.Handler)5 SOAPHandler (javax.xml.ws.handler.soap.SOAPHandler)5 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)5 Fault (org.apache.cxf.interceptor.Fault)5 Exchange (org.apache.cxf.message.Exchange)5 IMocksControl (org.easymock.IMocksControl)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)4 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)4 MessageImpl (org.apache.cxf.message.MessageImpl)4