Package org.apache.cxf.binding.soap

Examples of org.apache.cxf.binding.soap.SoapVersion


        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(soapVersion.getPrefix(), soapVersion.getNamespace());
                    writer.writeStartElement(soapVersion.getPrefix(),
                                          soapVersion.getEnvelope().getLocalPart(),
                                          soapVersion.getNamespace());
                    writer.writeNamespace(soapVersion.getPrefix(), soapVersion.getNamespace());
                    writer.writeEndElement();
                   
                    writer.flush();
                } catch (Exception e) {
                    // do nothing
View Full Code Here


                        // change mustUnderstand to false
                        SOAPMessage message = smc.getMessage();
                        SOAPHeader soapHeader = message.getSOAPHeader();
                        Element headerElementNew = (Element)soapHeader.getChildNodes().item(0);

                        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);
        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]).getChildNodes().item(0);
       
        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"));
    }
View Full Code Here

                        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);
        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(soapVersion.getPrefix(), soapVersion.getNamespace());
                    writer.writeStartElement(soapVersion.getPrefix(),
                                          soapVersion.getEnvelope().getLocalPart(),
                                          soapVersion.getNamespace());
                    writer.writeNamespace(soapVersion.getPrefix(), 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();
    }
View Full Code Here

        XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is);

        // skip until soap body
        if (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
            String ns = xmlReader.getNamespaceURI();
            SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
            // message.setVersion(soapVersion);

            QName qn = xmlReader.getName();
            while (!qn.equals(soapVersion.getBody()) && !qn.equals(soapVersion.getHeader())) {
                while (xmlReader.nextTag() != XMLStreamConstants.START_ELEMENT) {
                    // nothing to do
                }
                qn = xmlReader.getName();
            }
            if (qn.equals(soapVersion.getHeader())) {
                XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, soapVersion.getBody());

                StaxUtils.read(filteredReader);
            }
            // advance just past body.
            xmlReader.next();
View Full Code Here

        return xmlReader;
    }

    private Object[] prepareSOAPHeader() throws Exception {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        SoapVersion soapVersion = Soap11.getInstance();
        Element envElement = doc.createElementNS(soapVersion.getEnvelope().getNamespaceURI(),
                                                 soapVersion.getEnvelope().getLocalPart());
       
        Element headerElement = doc.createElementNS(soapVersion.getNamespace(),
                                                    soapVersion.getHeader().getLocalPart());
       
        Element bodyElement = doc.createElementNS(soapVersion.getBody().getNamespaceURI(),
                                                  soapVersion.getBody().getLocalPart());
       
        Element childElement = doc.createElementNS("http://apache.org/hello_world_rpclit/types",
                                                   "ns2:header1");
        Attr attr =
            childElement.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(),
                                                                  "SOAP-ENV:mustUnderstand");
        attr.setValue("true");
        childElement.setAttributeNodeNS(attr)
       
        headerElement.appendChild(childElement);
View Full Code Here

        // Add a final interceptor to write end elements
        message.getInterceptorChain().add(new SoapOutEndingInterceptor());
    }
   
    private void writeSoapEnvelopeStart(final SoapMessage message) {
        final SoapVersion soapVersion = message.getVersion();
        try {           
            XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class);
            String soapPrefix = xtw.getPrefix(soapVersion.getNamespace());
            if (StringUtils.isEmpty(soapPrefix)) {
                soapPrefix = "soap";
            }
            if (message.hasAdditionalEnvNs()) {
                Map<String, String> nsMap = message.getEnvelopeNs();
                for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                    if (soapVersion.getNamespace().equals(entry.getValue())) {
                        soapPrefix = entry.getKey();
                    }
                }
                xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
                xtw.writeStartElement(soapPrefix,
                                      soapVersion.getEnvelope().getLocalPart(),
                                      soapVersion.getNamespace());
                xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
                for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                    if (!soapVersion.getNamespace().equals(entry.getValue())) {
                        xtw.writeNamespace(entry.getKey(), entry.getValue());
                    }
                }               
            } else {
                xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
                xtw.writeStartElement(soapPrefix,
                                      soapVersion.getEnvelope().getLocalPart(),
                                      soapVersion.getNamespace());
                String s2 = xtw.getPrefix(soapVersion.getNamespace());
                if (StringUtils.isEmpty(s2) || soapPrefix.equals(s2)) {
                    xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
                } else {
                    soapPrefix = s2;
                }
            }
            boolean preexistingHeaders = message.hasHeaders();

            if (preexistingHeaders) {
                xtw.writeStartElement(soapPrefix,
                                      soapVersion.getHeader().getLocalPart(),
                                      soapVersion.getNamespace());  
                List<Header> hdrList = message.getHeaders();
                for (Header header : hdrList) {
                    XMLStreamWriter writer = xtw;
                    if (header instanceof SoapHeader) {
                        SoapHeader soapHeader = (SoapHeader)header;
                        writer = new SOAPHeaderWriter(xtw, soapHeader, soapVersion, soapPrefix);
                    }
                    DataBinding b = header.getDataBinding();
                    if (b == null) {
                        HeaderProcessor hp = bus.getExtension(HeaderManager.class)
                                .getHeaderProcessor(header.getName().getNamespaceURI());
                        if (hp != null) {
                            b = hp.getDataBinding();
                        }
                    }
                    if (b != null) {
                        MessagePartInfo part = new MessagePartInfo(header.getName(), null);
                        part.setConcreteName(header.getName());
                        b.createWriter(XMLStreamWriter.class)
                            .write(header.getObject(), part, writer);
                    } else {
                        Element node = (Element)header.getObject();
                        StaxUtils.copy(node, writer);
                    }
                }
            }
            boolean endedHeader = handleHeaderPart(preexistingHeaders, message, soapPrefix);
            if (preexistingHeaders && !endedHeader) {
                xtw.writeEndElement();
            }

            xtw.writeStartElement(soapPrefix,
                                  soapVersion.getBody().getLocalPart(),
                                  soapVersion.getNamespace());
           
            // Interceptors followed such as Wrapped/RPC/Doc Interceptor will write SOAP body
        } catch (XMLStreamException e) {
            throw new SoapFault(
                new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
        }
    }
View Full Code Here

        if (parts.size() > 0) {
            MessageContentsList objs = MessageContentsList.getContentsList(message);
            if (objs == null) {
                return endedHeader;
            }
            SoapVersion soapVersion = message.getVersion();
            List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
            if (headers == null) {
                return endedHeader;
            }           

            for (SoapHeaderInfo header : headers) {
                MessagePartInfo part = header.getPart();
                if (wrappedBmi != bmi) {
                    part = wrappedBmi.getMessageInfo().addMessagePart(part.getName());
                }
                if (part.getIndex() >= objs.size()) {
                    // The optional out of band header is not a part of parameters of the method
                    continue;
                }
                Object arg = objs.get(part);
                objs.remove(part);
                if (!(startedHeader || preexistingHeaders)) {
                    try {
                        xtw.writeStartElement(soapPrefix,
                                              soapVersion.getHeader().getLocalPart(),
                                              soapVersion.getNamespace());
                    } catch (XMLStreamException e) {
                        throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE),
                            e, soapVersion.getSender());
                    }
                    startedHeader = true;
                }
                DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message);
                dataWriter.write(arg, header.getPart(), xtw);
            }
           
            if (startedHeader || preexistingHeaders) {
                try {
                    xtw.writeEndElement();
                    endedHeader = true;
                } catch (XMLStreamException e) {
                    throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE),
                        e, soapVersion.getSender());
                }
            }
        }
        return endedHeader;
    }      
View Full Code Here

                    xtw.writeEndDocument();
                   
                    xtw.flush();
                }
            } catch (XMLStreamException e) {
                SoapVersion soapVersion = message.getVersion();
                throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e,
                                    soapVersion.getSender());
            }
        }
View Full Code Here

    }
   
    private void readHeaders(XMLStreamReader xmlReader, SoapMessage message) throws XMLStreamException {

        // read header portion of SOAP document into DOM
        SoapVersion version = message.getVersion();
        XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, version.getBody());
        Node nd = message.getContent(Node.class);
        W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
        Document doc = null;
        if (writer != null) {
            StaxUtils.copy(filteredReader, writer);
            doc = writer.getDocument();
        } else if (nd instanceof Document) {
            doc = (Document)nd;
            StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
        } else {
            doc = StaxUtils.read(filteredReader);
            message.setContent(Node.class, doc);
        }

        // get the actual SOAP header
        Element element = doc.getDocumentElement();
        QName header = version.getHeader();               
        List<Element> elemList =
            DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
        for (Element elem : elemList) {
           
            // set all child elements as headers for message transmission
View Full Code Here

             * needs to send a HTTP 202 message (with no content)
             */
            if (mc == null) {
                return;
            }
            SoapVersion version = mc.getVersion();
            RequestData reqData = new RequestData();
           
            /*
             * The overall try, just to have a finally at the end to perform some
             * housekeeping.
             */
            try {
                WSSConfig config = WSSConfig.getNewInstance();
                reqData.setWssConfig(config);
               
                /*
                 * Setup any custom actions first by processing the input properties
                 * and reconfiguring the WSSConfig with the user defined properties.
                 */
                this.configureActions(mc, doDebug, version, config);
               
                /*
                 * Get the action first.
                 */
                List<HandlerAction> actions =
                    CastUtils.cast((List<?>)getProperty(mc, WSHandlerConstants.HANDLER_ACTIONS));
                if (actions == null) {
                    // If null then just fall back to the "action" String
                    String action = getString(WSHandlerConstants.ACTION, mc);
                    if (action == null) {
                        throw new SoapFault(new Message("NO_ACTION", LOG), version
                                .getReceiver());
                    }
   
                    actions = WSSecurityUtil.decodeHandlerAction(action, config);
                }
                if (actions.isEmpty()) {
                    return;
                }

                translateProperties(mc);
                reqData.setMsgContext(mc);
                reqData.setAttachmentCallbackHandler(new AttachmentCallbackHandler(mc));
               
                handleSecureMTOM(mc, actions);
   
                /*
                 * For every action we need a username, so get this now. The
                 * username defined in the deployment descriptor takes precedence.
                 */
                reqData.setUsername((String) getOption(WSHandlerConstants.USER));
                if (reqData.getUsername() == null || reqData.getUsername().equals("")) {
                    String username = (String) getProperty(reqData.getMsgContext(),
                            WSHandlerConstants.USER);
                    if (username != null) {
                        reqData.setUsername(username);
                    }
                }
   
                /*
                 * Now we perform some set-up for UsernameToken and Signature
                 * functions. No need to do it for encryption only. Check if
                 * username is available and then get a passowrd.
                 */
                boolean userNameRequired = false;
                for (HandlerAction handlerAction : actions) {
                    if ((handlerAction.getAction() == WSConstants.SIGN
                        || handlerAction.getAction() == WSConstants.UT
                        || handlerAction.getAction() == WSConstants.UT_SIGN)
                        && (handlerAction.getActionToken() == null
                            || handlerAction.getActionToken().getUser() == null)) {
                        userNameRequired = true;
                        break;
                    }
                }
                if (userNameRequired && (reqData.getUsername() == null || reqData.getUsername().equals(""))
                        && (String)getOption(WSHandlerConstants.SIGNATURE_USER) == null) {
                    /*
                     * We need a username - if none throw an SoapFault. For
                     * encryption there is a specific parameter to get a username.
                     */
                    throw new SoapFault(new Message("NO_USERNAME", LOG), version
                            .getReceiver());
                }
                if (doDebug) {
                    LOG.fine("Actor: " + reqData.getActor());
                }
                /*
                 * Now get the SOAP part from the request message and convert it
                 * into a Document. This forces CXF to serialize the SOAP request
                 * into FORM_STRING. This string is converted into a document.
                 * During the FORM_STRING serialization CXF performs multi-ref of
                 * complex data types (if requested), generates and inserts
                 * references for attachments and so on. The resulting Document
                 * MUST be the complete and final SOAP request as CXF would send it
                 * over the wire. Therefore this must shall be the last (or only)
                 * handler in a chain. Now we can perform our security operations on
                 * this request.
                 */             
   
                SOAPMessage saaj = mc.getContent(SOAPMessage.class);
   
                if (saaj == null) {
                    LOG.warning("SAAJOutHandler must be enabled for WS-Security!");
                    throw new SoapFault(new Message("NO_SAAJ_DOC", LOG), version
                            .getReceiver());
                }
   
                Document doc = saaj.getSOAPPart();

                doSenderAction(doc, reqData, actions, Boolean.TRUE
                        .equals(getProperty(mc, org.apache.cxf.message.Message.REQUESTOR_ROLE)));
   
                if (doDebug) {
                    LOG.fine("WSS4JOutInterceptor: exit handleMessage()");
                }
            } catch (WSSecurityException e) {
                throw new SoapFault(new Message("SECURITY_FAILED", LOG), e, version
                        .getSender());
            } finally {
                reqData.clear();
                reqData = null;
            }
View Full Code Here

TOP

Related Classes of org.apache.cxf.binding.soap.SoapVersion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.