Search in sources :

Example 21 with SOAPHeader

use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.

the class MessageHelper method cloneSOAPEnvelope.

/**
 * This method will clone the provided SOAPEnvelope and returns the cloned envelope
 * as an exact copy of the provided envelope
 *
 * @param envelope - this will be cloned to get the new envelope
 * @return cloned SOAPEnvelope from the provided one
 */
public static SOAPEnvelope cloneSOAPEnvelope(SOAPEnvelope envelope) {
    SOAPFactory fac;
    if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelope.getBody().getNamespace().getNamespaceURI())) {
        fac = OMAbstractFactory.getSOAP11Factory();
    } else {
        fac = OMAbstractFactory.getSOAP12Factory();
    }
    SOAPEnvelope newEnvelope = fac.getDefaultEnvelope();
    Iterator childIterator;
    if (envelope.getHeader() != null) {
        SOAPHeader body = envelope.getHeader();
        childIterator = body.getChildren();
        while (childIterator.hasNext()) {
            Object bodyNs = childIterator.next();
            if (bodyNs instanceof SOAPHeaderBlock) {
                try {
                    newEnvelope.getHeader().addChild(ElementHelper.toSOAPHeaderBlock(((OMElement) bodyNs).cloneOMElement(), fac));
                } catch (Exception e) {
                    handleException(e);
                }
            } else if (bodyNs instanceof OMElement) {
                newEnvelope.getHeader().addChild(((OMElement) bodyNs).cloneOMElement());
            }
        }
    }
    if (envelope.getBody() != null) {
        // fault would lead to class cast exceptions if accessed through the getFault method
        if (envelope.getBody().getFirstElement() instanceof SOAPFault && envelope.getBody().hasFault()) {
            SOAPFault fault = envelope.getBody().getFault();
            newEnvelope.getBody().addFault(cloneSOAPFault(fault));
        } else {
            OMElement body = envelope.getBody().cloneOMElement();
            Iterator ns = body.getAllDeclaredNamespaces();
            OMNamespace bodyNs = body.getNamespace();
            String nsUri = bodyNs.getNamespaceURI();
            String nsPrefix = bodyNs.getPrefix();
            while (ns.hasNext()) {
                OMNamespace namespace = ((OMNamespace) ns.next());
                if (nsUri != null && !nsUri.equals(namespace.getNamespaceURI()) && nsPrefix != null && !nsPrefix.equals(namespace.getPrefix())) {
                    newEnvelope.getBody().declareNamespace(namespace);
                }
                ns.remove();
            }
            Iterator attributes = body.getAllAttributes();
            while (attributes.hasNext()) {
                OMAttribute attrb = (OMAttribute) attributes.next();
                newEnvelope.getBody().addAttribute(attrb);
                attributes.remove();
            }
            Iterator itr = body.getChildren();
            while (itr.hasNext()) {
                OMNode node = (OMNode) itr.next();
                itr.remove();
                newEnvelope.getBody().addChild(node);
            }
        }
    }
    return newEnvelope;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) OMNode(org.apache.axiom.om.OMNode) Iterator(java.util.Iterator) SOAPFault(org.apache.axiom.soap.SOAPFault) OMAttribute(org.apache.axiom.om.OMAttribute) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 22 with SOAPHeader

use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.

the class MessageConverter method getSoapEnvelope.

private static SOAPEnvelope getSoapEnvelope(String soapEnvelpe) {
    OMElement response;
    try {
        response = AXIOMUtil.stringToOM(soapEnvelpe);
    } catch (Exception e) {
        logger.error("Cannot create SOAP Envelop. Error:" + e.getLocalizedMessage(), e);
        return null;
    }
    SOAPFactory soapFactory;
    String soapNamespace = response.getNamespace().getNamespaceURI();
    if (soapNamespace.equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
        soapFactory = OMAbstractFactory.getSOAP12Factory();
    } else {
        soapFactory = OMAbstractFactory.getSOAP11Factory();
    }
    SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
    // Set the headers of the message
    if (response.getFirstElement().getLocalName().contains(HEADER_STRING)) {
        SOAPHeader header = soapEnvelope.getHeader();
        SOAPFactory fac = (SOAPFactory) soapEnvelope.getOMFactory();
        Iterator headers = response.getFirstElement().getChildElements();
        while (headers.hasNext()) {
            OMElement soapHeader = (OMElement) headers.next();
            SOAPHeaderBlock hb = header.addHeaderBlock(soapHeader.getLocalName(), fac.createOMNamespace(soapHeader.getNamespace().getNamespaceURI(), soapHeader.getNamespace().getPrefix()));
            hb.setText(soapHeader.getText());
            // Add the child elements of the header (if any)
            Iterator elements = soapHeader.getChildElements();
            while (elements.hasNext()) {
                hb.addChild((OMNode) elements.next());
            }
        }
        response.getFirstElement().detach();
    }
    if (response.getFirstElement().getFirstElement() != null) {
        soapEnvelope.getBody().addChild(response.getFirstElement().getFirstElement());
    }
    return soapEnvelope;
}
Also used : Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 23 with SOAPHeader

use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.

the class RelaySecuirtyMessageBuilderDispatchandler method invoke.

@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
    InvocationResponse invocationResponse = super.invoke(messageContext);
    EndpointReference toEPR = messageContext.getTo();
    Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null) {
        if (toEPR != null) {
            ConfigurationContext configurationContext = messageContext.getConfigurationContext();
            AxisConfiguration registry = configurationContext.getAxisConfiguration();
            String filePart = toEPR.getAddress();
            if (filePart != null) {
                String serviceOpPart = Utils.getServiceAndOperationPart(filePart, messageContext.getConfigurationContext().getServiceContextPath());
                AxisService axisService = null;
                // only service context path onwards values will be taken
                if (messageContext.getConfigurationContext().getServiceContextPath() != null && serviceOpPart != null) {
                    axisService = registry.getService(serviceOpPart);
                    if (axisService != null) {
                        Parameter parameter = axisService.getParameter(SERVICE_TYPE);
                        if (parameter != null) {
                            if (!parameter.getValue().equals(PROXY)) {
                                build(messageContext);
                            }
                        } else {
                            build(messageContext);
                        }
                    }
                }
            }
        }
        if (messageContext.isEngaged(PassThroughConstants.SECURITY_MODULE_NAME)) {
            SOAPHeader header = null;
            if (messageContext.getEnvelope().getHeader() != null) {
                header = messageContext.getEnvelope().getHeader();
            }
            build(messageContext);
            this.handlePOXRequests(messageContext, header);
        }
    }
    return invocationResponse;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AxisService(org.apache.axis2.description.AxisService) Parameter(org.apache.axis2.description.Parameter) Pipe(org.apache.synapse.transport.passthru.Pipe) SOAPHeader(org.apache.axiom.soap.SOAPHeader) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 24 with SOAPHeader

use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.

the class NashornJavaScriptMessageContext method addHeader.

/**
 * Add a new SOAP header to the message.
 *
 * @param mustUnderstand the value for the <code>soapenv:mustUnderstand</code> attribute
 * @param content the XML for the new header
 * @throws ScriptException if an error occurs when converting the XML to OM
 */
public void addHeader(boolean mustUnderstand, Object content) throws ScriptException {
    SOAPEnvelope envelope = mc.getEnvelope();
    SOAPFactory factory = (SOAPFactory) envelope.getOMFactory();
    SOAPHeader header = envelope.getHeader();
    if (header == null) {
        header = factory.createSOAPHeader(envelope);
    }
    OMElement element = xmlHelper.toOMElement(content);
    // We can't add the element directly to the SOAPHeader. Instead, we need to copy the
    // information over to a SOAPHeaderBlock.
    SOAPHeaderBlock headerBlock = header.addHeaderBlock(element.getLocalName(), element.getNamespace());
    for (Iterator it = element.getAllAttributes(); it.hasNext(); ) {
        headerBlock.addAttribute((OMAttribute) it.next());
    }
    headerBlock.setMustUnderstand(mustUnderstand);
    OMNode child = element.getFirstOMChild();
    while (child != null) {
        // Get the next child before addChild will detach the node from its original place.
        OMNode next = child.getNextOMSibling();
        headerBlock.addChild(child);
        child = next;
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 25 with SOAPHeader

use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.

the class StockQuoteSampleClient method buildSoapEnvelope.

private SOAPEnvelope buildSoapEnvelope(String clientID, String value) {
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();
    SOAPHeader header = soapFactory.createSOAPHeader();
    envelope.addChild(header);
    OMNamespace synNamespace = soapFactory.createOMNamespace("http://ws.apache.org/ns/synapse", "syn");
    OMElement clientIDElement = soapFactory.createOMElement("ClientID", synNamespace);
    clientIDElement.setText(clientID);
    header.addChild(clientIDElement);
    SOAPBody body = soapFactory.createSOAPBody();
    envelope.addChild(body);
    OMElement valueElement = soapFactory.createOMElement("Value", null);
    valueElement.setText(value);
    body.addChild(valueElement);
    return envelope;
}
Also used : SOAPBody(org.apache.axiom.soap.SOAPBody) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Aggregations

SOAPHeader (org.apache.axiom.soap.SOAPHeader)56 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)33 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)32 OMElement (org.apache.axiom.om.OMElement)18 OMNamespace (org.apache.axiom.om.OMNamespace)15 QName (javax.xml.namespace.QName)13 Iterator (java.util.Iterator)10 OMNode (org.apache.axiom.om.OMNode)10 SOAPFactory (org.apache.axiom.soap.SOAPFactory)9 SOAPBody (org.apache.axiom.soap.SOAPBody)8 SOAPFault (org.apache.axiom.soap.SOAPFault)5 OMException (org.apache.axiom.om.OMException)4 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 OMAttribute (org.apache.axiom.om.OMAttribute)3 SOAPFaultCode (org.apache.axiom.soap.SOAPFaultCode)3 SOAPFaultDetail (org.apache.axiom.soap.SOAPFaultDetail)3 SOAPFaultReason (org.apache.axiom.soap.SOAPFaultReason)3 BooleanAttributeAccessor (org.apache.axiom.ts.soap.BooleanAttributeAccessor)3 Element (org.w3c.dom.Element)3 StringReader (java.io.StringReader)2