2013-07-03 3 views
0

중첩 된 메시지에서 AAA 메시지가 나타납니다. 모든 어린이에게 의 값을 CCC으로 바꿔주세요. 그런 다음 AAA메시지를 변경하고 완전히 보내려면 (wso2esb)

<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> 

하지만 반복 처리 중재자를 사용하는 경우 메시지가

+0

을 위해 그것을 사용하는'newValue'는' TEST1'에 따라 데이터베이스에서로드됩니다 –

답변

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; 
    } 
} 
2

에 저장되지 않은 변경 당신이 얻을 수있는 결과를 집계해야 변경된 메세지 어떻게 이것이 xslt 조정자를 사용하여 달성 될 수 있는지. yourxsltkey 당신의 XSLT 정의의 핵심입니다

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

을 다음과 같이 샘플 프록시 구성을 보면 될 것이다. 로컬 항목 또는 레지스트리로 선언 할 수 있습니다. 여기 샘플 나는 로컬 항목으로 정의했습니다.

<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> 
관련 문제