就像在迭代中更改消息并完全发送一样(wso2esb) [英] As in the iterate to change the message and send it fully (wso2esb)

查看:27
本文介绍了就像在迭代中更改消息并完全发送一样(wso2esb)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自 AAA 嵌套子项的消息.我希望每个孩子 BBB 替换 CCC 的值.然后在AAA

I receive a message from AAA nested children. I want every child BBB replace the value of CCC. Then send the modified message on AAA

<AAA>
    <BBB>
        <CCC>test1</CCC>
        <DDD>testing</DDD>
    </BBB>
    <BBB>
        <CCC>test2</CCC>
        <DDD>testing</DDD>
    </BBB>
    <BBB>
        <CCC>test3</CCC>
        <DDD>testing</DDD>
    </BBB>
    <BBB>
        <CCC>test4</CCC>
        <DDD>testing</DDD>
    </BBB>
    <BBB>
        <CCC>test5</CCC>
        <DDD>testing</DDD>
    </BBB>
</AAA>

我做到了:

<iterate continueParent="true" expression="/AAA/BBB">
    <target>
        <sequence>
            <property name="newValue" value="chang testing" scope="default" type="STRING"/>
            <enrich>
                <source clone="false" type="custom" xpath="get-property('newValue')"/>
                <target action="replace" type="custom" xpath="//DDD"/>
            </enrich>
         </sequence>
    </target>
</iterate>

但是更改的消息不会存储在

But changing the message is not stored on

推荐答案

我编写了我的调解器并将其用于此目的

I wrote my mediator and use it for this purpose

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.dom.NamespaceImpl;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.synapse.Mediator;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.apache.synapse.mediators.eip.EIPUtils;
import org.apache.synapse.util.MessageHelper;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.jaxen.JaxenException;

import java.util.List;


public class SplitMediator extends AbstractMediator {

    private String sequenceRef = null;
    private String xpathString = null;
    private String attachPathString = null;
    private String uri = null;
    private String prefix = null;

    public boolean mediate(MessageContext synCtx) {
        if (sequenceRef == null || xpathString == null || attachPathString == null) {
            handleException("Error creating a mediate due to sequenceRef or xpathString  attachPathString is null", synCtx);
            return false;
        }

        try {
            SOAPEnvelope envelope = synCtx.getEnvelope();
            Mediator sequenceMediator = synCtx.getSequence(sequenceRef);

            SynapseXPath expression = new SynapseXPath(xpathString);
            if (uri != null && prefix != null)
                expression.addNamespace(new NamespaceImpl(uri, prefix));
            SynapseXPath attachPath = new SynapseXPath(attachPathString);
            if (uri != null && prefix != null)
                attachPath.addNamespace(new NamespaceImpl(uri, prefix));

            List<OMNode> splitElements = EIPUtils.getDetachedMatchingElements(envelope, synCtx, expression);
            MessageContext templateMessageContext = MessageHelper.cloneMessageContext(synCtx);
            OMElement omElement = getOMElementByXPath(attachPath, envelope, synCtx);

            for (OMNode o : splitElements) {
                 MessageContext changeCtx = getNewMessageContextToSequence(templateMessageContext, o, attachPath);
                sequenceMediator.mediate(changeCtx);
                List elementList = EIPUtils.getMatchingElements(changeCtx.getEnvelope(), expression);
                OMNode changeElement = (OMNode) elementList.get(0);
                omElement.addChild(changeElement);
            }
        } catch (JaxenException e) {
            handleException("Error evaluating split XPath expression : " + xpathString, e, synCtx);
        } catch (AxisFault af) {
            handleException("Error creating an iterated copy of the message", af, synCtx);
        }
        return true;
    }

    private MessageContext getNewMessageContextToSequence(MessageContext templateMessageContext, OMNode o, SynapseXPath attachPath) throws AxisFault, JaxenException {
        MessageContext synCtx = MessageHelper.cloneMessageContext(templateMessageContext);
        SOAPEnvelope envelope = synCtx.getEnvelope();
        OMElement omElement = getOMElementByXPath(attachPath, envelope, synCtx);
        omElement.addChild(o);
        return synCtx;
    }

    private OMElement getOMElementByXPath(SynapseXPath attachPath, SOAPEnvelope envelope, MessageContext synCtx) {
        Object attachElem = attachPath.evaluate(envelope, synCtx);
        if (attachElem != null &&
                attachElem instanceof List && !((List) attachElem).isEmpty()) {
            attachElem = ((List) attachElem).get(0);
        }
        // for the moment attaching element should be an OMElement
        if (attachElem != null && attachElem instanceof OMElement) {
            return ((OMElement) attachElem);
        } else {
            handleException("Error in attaching the splitted elements :: " +
                    "Unable to get the attach path specified by the expression " +
                    attachPath, synCtx);
        }
        return null;
    }


    ///////////////////////////////////////////////////////////////////////////////////////
    //                        Getters and Setters                                        //
    ///////////////////////////////////////////////////////////////////////////////////////


    public String getXpathString() {
        return xpathString;
    }

    public void setXpathString(String xpathString) {
        this.xpathString = xpathString;
    }

    public String getAttachPathString() {
        return attachPathString;
    }

    public void setAttachPathString(String attachPathString) {
        this.attachPathString = attachPathString;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSequenceRef() {
        return sequenceRef;
    }

    public void setSequenceRef(String sequenceRef) {
        this.sequenceRef = sequenceRef;
    }
}

这篇关于就像在迭代中更改消息并完全发送一样(wso2esb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆