Search in sources :

Example 31 with SOAPHeader

use of javax.xml.soap.SOAPHeader 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 ("http://apache.org/hello_world_rpclit/types".equals(elem.getNamespaceURI()) && "header1".equals(elem.getLocalName())) {
                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 32 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.

@Test
public void testChangeSOAPHeaderOutBound() 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();
                    Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
                    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
                    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();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    message.setContent(OutputStream.class, originalEmptyOs);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                Object[] headerInfo = prepareSOAPHeader();
                StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
    Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    originalEmptyOs.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) 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) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Iterator(java.util.Iterator) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) 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) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Example 33 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project cxf by apache.

the class MessageModeOutInterceptor method doSoap.

private void doSoap(Message message) {
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null || list.isEmpty()) {
        return;
    }
    Object o = list.get(0);
    if (o instanceof SOAPMessage) {
        SOAPMessage soapMessage = (SOAPMessage) o;
        if (soapMessage.countAttachments() > 0) {
            message.put("write.attachments", Boolean.TRUE);
        }
        try {
            if (message instanceof org.apache.cxf.binding.soap.SoapMessage) {
                org.apache.cxf.binding.soap.SoapMessage cxfSoapMessage = (org.apache.cxf.binding.soap.SoapMessage) message;
                String cxfNamespace = cxfSoapMessage.getVersion().getNamespace();
                SOAPHeader soapHeader = soapMessage.getSOAPHeader();
                String namespace = soapHeader == null ? null : soapHeader.getNamespaceURI();
                if (Soap12.SOAP_NAMESPACE.equals(namespace) && !namespace.equals(cxfNamespace)) {
                    cxfSoapMessage.setVersion(Soap12.getInstance());
                    cxfSoapMessage.put(Message.CONTENT_TYPE, cxfSoapMessage.getVersion().getContentType());
                }
            }
        } catch (SOAPException e) {
        // ignore
        }
        try {
            Object enc = soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
            if (enc instanceof String) {
                message.put(Message.ENCODING, enc);
            }
        } catch (SOAPException e) {
        // ignore
        }
        try {
            Object xmlDec = soapMessage.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
            if (xmlDec != null) {
                boolean b = PropertyUtils.isTrue(xmlDec);
                message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, b);
            }
        } catch (SOAPException e) {
        // ignore
        }
    }
    message.getInterceptorChain().add(internal);
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPException(javax.xml.soap.SOAPException) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 34 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project cxf by apache.

the class SOAPMessageContextImpl method getHeaders.

public Object[] getHeaders(QName name, JAXBContext context, boolean allRoles) {
    SOAPMessage msg = getMessage();
    SOAPHeader header;
    try {
        header = msg.getSOAPPart().getEnvelope().getHeader();
        if (header == null || !header.hasChildNodes()) {
            return new Object[0];
        }
        List<Object> ret = new ArrayList<>();
        Iterator<SOAPHeaderElement> it = CastUtils.cast(header.examineAllHeaderElements());
        while (it.hasNext()) {
            SOAPHeaderElement she = it.next();
            if ((allRoles || roles.contains(she.getActor())) && name.equals(she.getElementQName())) {
                ret.add(JAXBUtils.unmarshall(context, she));
            }
        }
        return ret.toArray(new Object[0]);
    } catch (SOAPException | JAXBException e) {
        throw new WebServiceException(e);
    }
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) WebServiceException(javax.xml.ws.WebServiceException) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 35 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project cxf by apache.

the class TestMustUnderstandHandler method handleMessage.

public boolean handleMessage(SOAPMessageContext ctx) {
    boolean continueProcessing = true;
    try {
        Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        boolean outbound = (Boolean) b;
        SOAPMessage msg = ctx.getMessage();
        if (isServerSideHandler()) {
            if (outbound) {
                QName qname = new QName("http://cxf.apache.org/mu", "MU");
                SOAPPart soapPart = msg.getSOAPPart();
                SOAPEnvelope envelope = soapPart.getEnvelope();
                SOAPHeader header = envelope.getHeader();
                if (header == null) {
                    header = envelope.addHeader();
                }
                SOAPHeaderElement headerElement = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));
                // QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
                // "mustUnderstand");
                Name name = SOAPFactory.newInstance().createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
                headerElement.addAttribute(name, "1");
            } else {
                getHandlerInfoList(ctx).add(getHandlerId());
            }
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return continueProcessing;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Aggregations

SOAPHeader (javax.xml.soap.SOAPHeader)49 SOAPException (javax.xml.soap.SOAPException)30 SOAPMessage (javax.xml.soap.SOAPMessage)29 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)22 SOAPElement (javax.xml.soap.SOAPElement)18 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)17 QName (javax.xml.namespace.QName)13 SOAPBody (javax.xml.soap.SOAPBody)13 Name (javax.xml.soap.Name)10 WebServiceException (javax.xml.ws.WebServiceException)9 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)8 ArrayList (java.util.ArrayList)7 SOAPPart (javax.xml.soap.SOAPPart)7 Test (org.junit.Test)7 Element (org.w3c.dom.Element)7 IOException (java.io.IOException)6 SOAPFactory (javax.xml.soap.SOAPFactory)6 NodeList (org.w3c.dom.NodeList)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4