代码之家  ›  专栏  ›  技术社区  ›  Cesar Justo

脚本中介Javascript在WSO2 EI ESB上无法正常工作

  •  0
  • Cesar Justo  · 技术社区  · 5 年前

    下午好,我是WSO2 ESB的新手,我正在使用一个api,在这个api中,我们使用了一个中介脚本和javascript,但是我发现这个脚本工作不正常,基本上是接受值并进行一些转换的脚本,然而,在日志级别,我看到变量只指示值True,好像它是布尔型的

    我的API如下所示:

     <filter xmlns:ns2="http://org.apache.synapse/xsd" xmlns:ns="http://org.apache.synapse/xsd" source="get-property('axis2', 'HTTP_SC')" regex="200">
        <then>
           <property xmlns:ns3="http://www.ericsson.com/OCS/OCS_Provision_types/v1" name="balance" expression="$body/ns3:queryTotalBalanceResponse/ns3:balance/text()"/>
           <property xmlns:ns3="http://www.ericsson.com/OCS/OCS_Provision_types/v1" name="negativeBalance" expression="$body/ns3:queryTotalBalanceResponse/ns3:negativeBalance/text()"/>
           <property xmlns:ns3="http://www.ericsson.com/OCS/OCS_Provision_types/v1" name="exponentBalance" expression="$body/ns3:queryTotalBalanceResponse/ns3:balance/@exponent"/>
           <property xmlns:ns3="http://www.ericsson.com/OCS/OCS_Provision_types/v1" name="exponentNegativeBalance" expression="$body/ns3:queryTotalBalanceResponse/ns3:negativeBalance/@exponent"/>
           <property name="balanceLength" expression="fn:string-length(get-property('balance'))"/>
           <property name="negativeBalanceLength" expression="fn:string-length(get-property('negativeBalance'))"/>
           <script language="js">var exponent = mc.getProperty('exponentBalance');                  var balance = mc.getProperty('balance');                  if(exponent > 0) {         var itera = exponent * -1;                    var zero = '0';          for (i = 0; i &lt; itera; ++i) {           zero = zero.concat('0');    }             var result = balance.concat(zero);                   }   mc.setProperty("RESULT", String(result));</script>
           <filter xpath="fn:number(get-property('exponentBalance')) > 0">
              <then>
                 <property name="beforeBalance" expression="fn:concat($ctx:balance,'0000000000000000000')"/>
                 <property name="balanceResponse" expression="fn:substring($ctx:beforeBalance,1,$ctx:balanceLength+$ctx:exponentBalance)"/>
              </then>
              <else>
                 <filter xpath="fn:number(get-property('exponentBalance')) = 0">
                    <then>
                       <property name="balanceResponse" expression="$ctx:balance"/>
                    </then>
                    <else>
                       <filter xpath="fn:number(get-property('balance')) = 0">
                          <then>
                             <property name="beforeBalance" expression="fn:substring($ctx:balance,1,$ctx:balanceLength+$ctx:exponentBalance)"/>
                             <property name="stringToInsertbalance" value="."/>
                             <property name="afterBalance" expression="fn:substring($ctx:balance,$ctx:balanceLength+$ctx:exponentBalance+1)"/>
                             <property name="balanceResponse" expression="fn:concat(0,$ctx:beforeBalance,$ctx:stringToInsertbalance,$ctx:afterBalance)"/>
                          </then>
                          <else>
                             <property name="beforeBalance" expression="fn:substring($ctx:balance,1,$ctx:balanceLength+$ctx:exponentBalance)"/>
                             <property name="stringToInsertbalance" value="."/>
                             <property name="afterBalance" expression="fn:substring($ctx:balance,$ctx:balanceLength+$ctx:exponentBalance+1)"/>
                             <property name="balanceResponse" expression="fn:concat($ctx:beforeBalance,$ctx:stringToInsertbalance,$ctx:afterBalance)"/>
                          </else>
                       </filter>
                    </else>
                 </filter>
              </else>
           </filter>
           <log>
              <property name="balance" expression="$ctx:balance"/>
              <property name="balanceLength" expression="$ctx:balanceLength"/>
              <property name="negativeBalanceLength" expression="$ctx:negativeBalanceLength"/>
              <property name="exponentBalance" expression="$ctx:exponentBalance"/>
              <property name="exponentNegativeBalance" expression="$ctx:exponentNegativeBalance"/>
              <property name="beforeBalance" expression="$ctx:beforeBalance"/>
              <property name="beforeNegativeBalance" expression="$ctx:beforeNegativeBalance"/>
              <property name="stringToInsertbalance" expression="$ctx:stringToInsertbalance"/>
              <property name="stringToInsertNegativeBalance" expression="$ctx:stringToInsertNegativeBalance"/>
              <property name="afterBalance" expression="$ctx:afterBalance"/>
              <property name="afterBalanceNegative" expression="$ctx:afterBalanceNegative"/>
              <property name="balanceResponse" expression="$ctx:balanceResponse"/>
              <property name="negativeBalanceResponse" expression="$ctx:negativeBalanceResponse"/>
              <property name="result" expression="get-property('result')"/>
              <property name="absolute" expression="get-property('absolute')"/>
           </log>
    

    我在javascript级别使用的脚本如下:

    <script language="js">var exponent = mc.getProperty('exponentBalance');                  var balance = mc.getProperty('balance');                  if(exponent > 0) {             var itera = exponent * -1;                    var zero = '0';               for (i = 0; i &lt; itera; ++i) {           zero = zero.concat('0');                        }             var result = balance.concat(zero);                   }                  mc.setProperty("RESULT", String(result));</script>
    

    enter image description here

    同样,如果我给结果变量添加一个固定值,它总是显示为True,我使用的是WSO2 EI 6.5

    会发生什么?

    使现代化

    更新行以设置变量,定义如下:

    <script language="js">var exponent = mc.getProperty('exponentBalance');                  var balance = mc.getProperty('balance');                  if(exponent > 0) {             var itera = exponent * -1;                    var zero = '0';               for (i = 0; i &lt; itera; ++i) {           zero = zero.concat('0');                        }             var result = balance.concat(zero);                   }                  mc.setProperty("result", String(result));</script>
    

    其中修改了以下行:

    mc.setProperty("result", String(result))
    

    但是,我在日志级别得到了以下信息:

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   Shanaka Premarathna    5 年前

    您可能已经为属性“result”设置了值“true”。这是因为您没有在脚本中介中为属性“result”设置任何值。

    在脚本中介程序中,您使用属性名“RESULT”设置值,而使用属性名“RESULT”获取属性值。

    mc.setProperty("RESULT", String(result));
    <property name="result" expression="get-property('result')"/>
    

    如果要访问脚本中介器中设置的值,需要按以下方式设置该值。

    mc.setProperty("result", String(result));
    

    根据第一条评论进行更新

    这种行为的原因是,您已经定义了变量“ 后果 在脚本中介器的if条件内。如果未执行if条件,则 后果 将是未定义的。您需要使用默认值在if条件之前定义var“result”。