0

我收到来自AAA嵌套子级的消息。我希望每个孩子都BBB替换CCC. 然后发送修改后的消息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>

但是更改消息不会存储在

4

2 回答 2

2

如果您使用迭代调解器,则必须聚合结果以获取修改后的消息。如何通过使用 xslt 调解器来实现这一点。示例代理配置如下所示

<proxy name="yourpproxy" transports="https http" startOnLoad="true"   trace="disable">
      <description/>
      <target>
         <inSequence>
            <xslt key="yourxsltkey"/> 
            <send/>
         </inSequence>
         <outSequence>
            <send/>
         </outSequence>
      </target>
</proxy>

其中yourxsltkey是 xslt 定义的关键。这可以声明为本地条目或在注册表中。作为此处的示例,我已将其定义为本地条目。

<localEntry key="yourxsltkey">
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

       <xsl:template match="/">
        <AAA xmlns="http://ws.apache.org/ns/synapse">
          <xsl:for-each select="AAA/BBB">
            <BBB><xsl:value-of select="CCC"/></BBB>
          </xsl:for-each>
        </AAA>
       </xsl:template>
    </xsl:stylesheet>

 </localEntry>
于 2013-07-04T11:22:15.387 回答
0

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

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;
    }
}
于 2013-07-04T13:16:50.477 回答