Search in sources :

Example 66 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class UnitTestMockRegistry method lookup.

@Override
public OMNode lookup(String key) {
    if (key == null) {
        handleException("Resource cannot be found.");
    }
    if (key.isEmpty()) {
        handleException("Resource cannot be empty");
    }
    String resourcePath = getAbsolutePathToRegistry(key);
    if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
        RegistryResource resource = testMockRegistry.get(resourcePath);
        String sourceOfResource = resource.getArtifact();
        OMNode omNode = null;
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceOfResource.getBytes());
            try {
                XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
                StAXOMBuilder builder = new StAXOMBuilder(parser);
                omNode = builder.getDocumentElement();
            } catch (OMException ignored) {
                omNode = readNonXML(resource);
            } catch (XMLStreamException ignored) {
                omNode = readNonXML(resource);
            } catch (Exception e) {
                log.error("Error while reading the resource '" + key + "'", e);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Error while closing the input stream", e);
                }
            }
        } catch (Exception e) {
            log.error("Creating OMNode from registry resource artifact failed", e);
        }
        return omNode;
    } else {
        return null;
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Example 67 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class SamplingService method setSoapHeaderBlock.

private void setSoapHeaderBlock(MessageContext synCtx) {
    // Send the SOAP Header Blocks to support WS-Addressing
    if (synCtx.getEnvelope().getHeader() != null) {
        Iterator iHeader = synCtx.getEnvelope().getHeader().getChildren();
        SOAPFactory fac;
        if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(synCtx.getEnvelope().getBody().getNamespace().getNamespaceURI())) {
            fac = OMAbstractFactory.getSOAP11Factory();
        } else {
            fac = OMAbstractFactory.getSOAP12Factory();
        }
        List<OMNode> newHeaderNodes = new ArrayList<OMNode>();
        while (iHeader.hasNext()) {
            try {
                Object element = iHeader.next();
                if (!(element instanceof SOAPHeaderBlock)) {
                    if (element instanceof OMElement) {
                        newHeaderNodes.add(ElementHelper.toSOAPHeaderBlock((OMElement) element, fac));
                    }
                    iHeader.remove();
                }
            } catch (OMException e) {
                log.error("Unable to convert to SoapHeader Block", e);
            } catch (Exception e) {
                log.error("Unable to convert to SoapHeader Block", e);
            }
        }
        for (OMNode newHeaderNode : newHeaderNodes) {
            synCtx.getEnvelope().getHeader().addChild(newHeaderNode);
        }
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) OMElement(org.apache.axiom.om.OMElement) OMException(org.apache.axiom.om.OMException) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SynapseException(org.apache.synapse.SynapseException) OMException(org.apache.axiom.om.OMException)

Example 68 with OMNode

use of org.apache.axiom.om.OMNode 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);
            }
            /**
             * Copy the namespaces declared in envelope , Fix for
             * https://wso2.org/jira/browse/CARBON-16086
             */
            Iterator allDeclaredNamespaces = envelope.getAllDeclaredNamespaces();
            while (allDeclaredNamespaces.hasNext()) {
                newEnvelope.declareNamespace((OMNamespace) allDeclaredNamespaces.next());
            }
        }
    }
    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 69 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class PayloadHelper method getTextPayload.

// Text payload is carried in a wrapper element with QName TEXTELT
public static String getTextPayload(SOAPEnvelope envelope) {
    OMElement el = getXMLPayload(envelope);
    if (el == null)
        return null;
    if (!el.getQName().equals(TEXTELT)) {
        log.error("Wrong QName " + el.getQName());
        return null;
    }
    OMNode textNode = el.getFirstOMChild();
    if (textNode.getType() != OMNode.TEXT_NODE) {
        log.error("Text Node not found");
        return null;
    }
    OMText text = (OMText) textNode;
    return text.getText();
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 70 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class CalloutMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Callout mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    try {
        if (!initClientOptions) {
            blockingMsgSender.setInitClientOptions(false);
        }
        if (endpointKey != null) {
            endpoint = synCtx.getEndpoint(endpointKey);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            if (!isWrappingEndpointCreated) {
                synLog.traceOrDebug("Using the defined endpoint : " + endpoint.getName());
            } else {
                if (serviceURL != null) {
                    synLog.traceOrDebug("Using the serviceURL : " + serviceURL);
                } else {
                    synLog.traceOrDebug("Using the To header as the EPR ");
                }
                if (securityOn) {
                    synLog.traceOrDebug("Security enabled within the Callout Mediator config");
                    if (wsSecPolicyKey != null) {
                        synLog.traceOrDebug("Using security policy key : " + wsSecPolicyKey);
                    } else {
                        if (inboundWsSecPolicyKey != null) {
                            synLog.traceOrDebug("Using inbound security policy key : " + inboundWsSecPolicyKey);
                        }
                        if (outboundWsSecPolicyKey != null) {
                            synLog.traceOrDebug("Using outbound security policy key : " + outboundWsSecPolicyKey);
                        }
                    }
                }
            }
        }
        org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        if (isWrappingEndpointCreated) {
            if (Constants.VALUE_TRUE.equals(axis2MsgCtx.getProperty(Constants.Configuration.ENABLE_MTOM))) {
                ((AbstractEndpoint) endpoint).getDefinition().setUseMTOM(true);
            }
        }
        if (this.serviceURL != null && this.serviceURL.contains(DISTRIBUTED_TX_BEGIN_CHECK_STR)) {
            try {
                initContext(synCtx);
                try {
                    TranscationManger.lookUp(txContext);
                } catch (Exception e) {
                    handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
                }
                TranscationManger.beginTransaction();
                axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION, TranscationManger.getTransaction());
                axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION_MANAGER, TranscationManger.getTransactionManager());
            } catch (Exception e) {
                handleException("Error starting transaction", synCtx);
            }
        }
        MessageContext synapseOutMsgCtx = MessageHelper.cloneMessageContext(synCtx);
        // Send the SOAP Header Blocks to support WS-Addressing
        setSoapHeaderBlock(synapseOutMsgCtx);
        // Set NTLM options from parent message context to cloned message context
        setNTLMOptions(synCtx, synapseOutMsgCtx);
        if (!useEnvelopeAsSource && // if the payload is JSON, we do not consider the request (ie. source) path. Instead, we use the complete payload.
        !JsonUtil.hasAJsonPayload(((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext())) {
            SOAPBody soapBody = synapseOutMsgCtx.getEnvelope().getBody();
            for (Iterator itr = soapBody.getChildElements(); itr.hasNext(); ) {
                OMElement child = (OMElement) itr.next();
                child.detach();
            }
            soapBody.addChild(getRequestPayload(synCtx));
        }
        if (action != null) {
            synapseOutMsgCtx.setWSAAction(action);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("About to invoke the service");
            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Request message payload : " + synapseOutMsgCtx.getEnvelope());
            }
        }
        synapseOutMsgCtx.setProperty(SynapseConstants.BLOCKING_MSG_SENDER, blockingMsgSender);
        // Clear the message context properties related to endpoint in last service invocation
        Set keySet = synapseOutMsgCtx.getPropertyKeySet();
        if (keySet != null) {
            keySet.remove(SynapseConstants.RECEIVING_SEQUENCE);
            keySet.remove(EndpointDefinition.DYNAMIC_URL_VALUE);
            keySet.remove(SynapseConstants.LAST_ENDPOINT);
            keySet.remove(SynapseConstants.BLOCKING_SENDER_ERROR);
        }
        Object faultHandlerBeforeInvocation = getLastSequenceFaultHandler(synapseOutMsgCtx);
        synapseOutMsgCtx.setProperty(SynapseConstants.LAST_SEQ_FAULT_HANDLER, faultHandlerBeforeInvocation);
        endpoint.send(synapseOutMsgCtx);
        // Check for rollback status
        org.apache.axis2.context.MessageContext outAxis2MsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
        if (outAxis2MsgCtx.getProperty(SynapseConstants.SET_ROLLBACK_ONLY) != null) {
            axis2MsgCtx.setProperty(SynapseConstants.SET_ROLLBACK_ONLY, outAxis2MsgCtx.getProperty(SynapseConstants.SET_ROLLBACK_ONLY));
        }
        // check whether fault sequence is already invoked
        if (faultHandlerBeforeInvocation != getLastSequenceFaultHandler(synapseOutMsgCtx)) {
            return false;
        }
        if (!("true".equals(synapseOutMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR)))) {
            if (synapseOutMsgCtx.getProperty(SynapseConstants.OUT_ONLY) == null || "false".equals(synapseOutMsgCtx.getProperty(SynapseConstants.OUT_ONLY))) {
                setResponseHttpSc(synapseOutMsgCtx, synCtx);
                if (synLog.isTraceTraceEnabled()) {
                    synLog.traceTrace("Response payload received : " + synapseOutMsgCtx.getEnvelope());
                }
                if (synapseOutMsgCtx.getEnvelope() != null) {
                    org.apache.axis2.context.MessageContext resultAxisMsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
                    org.apache.axis2.context.MessageContext inAxisMsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                    if (JsonUtil.hasAJsonPayload(resultAxisMsgCtx)) {
                        JsonUtil.cloneJsonPayload(resultAxisMsgCtx, inAxisMsgCtx);
                    } else {
                        if (targetXPath != null) {
                            Object o = targetXPath.evaluate(synCtx);
                            OMElement result = synapseOutMsgCtx.getEnvelope().getBody().getFirstElement();
                            if (o != null && o instanceof OMElement) {
                                OMNode tgtNode = (OMElement) o;
                                tgtNode.insertSiblingAfter(result);
                                tgtNode.detach();
                            } else if (o != null && o instanceof List && !((List) o).isEmpty()) {
                                // Always fetches *only* the first
                                OMNode tgtNode = (OMElement) ((List) o).get(0);
                                tgtNode.insertSiblingAfter(result);
                                tgtNode.detach();
                            } else {
                                handleException("Evaluation of target XPath expression : " + targetXPath.toString() + " did not yeild an OMNode", synCtx);
                            }
                        } else if (targetKey != null) {
                            OMElement result = synapseOutMsgCtx.getEnvelope().getBody().getFirstElement();
                            synCtx.setProperty(targetKey, result);
                        } else {
                            synCtx.setEnvelope(synapseOutMsgCtx.getEnvelope());
                        }
                    }
                    // Set HTTP Status code
                    inAxisMsgCtx.setProperty(SynapseConstants.HTTP_SC, resultAxisMsgCtx.getProperty(SynapseConstants.HTTP_SC));
                    if ("false".equals(synCtx.getProperty(SynapseConstants.BLOCKING_SENDER_PRESERVE_REQ_HEADERS))) {
                        inAxisMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, resultAxisMsgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
                    }
                } else {
                    synLog.traceOrDebug("Service returned a null response");
                }
            }
        } else {
            log.info("Error while performing the callout operation");
            return false;
        }
    } catch (AxisFault e) {
        handleException("Error invoking service : " + serviceURL + (action != null ? " with action : " + action : ""), e, synCtx);
    } catch (JaxenException e) {
        handleException("Error while evaluating the XPath expression: " + targetXPath, e, synCtx);
    }
    synLog.traceOrDebug("End : Callout mediator");
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) OMElement(org.apache.axiom.om.OMElement) JaxenException(org.jaxen.JaxenException) NamingException(javax.naming.NamingException) SynapseException(org.apache.synapse.SynapseException) OMException(org.apache.axiom.om.OMException) OMNode(org.apache.axiom.om.OMNode) SOAPBody(org.apache.axiom.soap.SOAPBody) SynapseLog(org.apache.synapse.SynapseLog) JaxenException(org.jaxen.JaxenException) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

OMNode (org.apache.axiom.om.OMNode)150 OMElement (org.apache.axiom.om.OMElement)87 Iterator (java.util.Iterator)28 OMText (org.apache.axiom.om.OMText)23 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)20 BXMLItem (org.ballerinalang.model.values.BXMLItem)18 StringReader (java.io.StringReader)17 SynapseException (org.apache.synapse.SynapseException)15 OMAttribute (org.apache.axiom.om.OMAttribute)14 OMFactory (org.apache.axiom.om.OMFactory)14 BValue (org.ballerinalang.model.values.BValue)14 BXML (org.ballerinalang.model.values.BXML)14 Test (org.testng.annotations.Test)14 QName (javax.xml.namespace.QName)13 BJSON (org.ballerinalang.model.values.BJSON)13 OMDocument (org.apache.axiom.om.OMDocument)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 SOAPHeader (org.apache.axiom.soap.SOAPHeader)10 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)10