我们可以在WSO2 ESB的脚本调解器(语言:javascript)中使用atob()或btoa()javascript函数吗?

时间:2017-03-20 15:22:49

标签: javascript wso2 wso2esb

我试图从查询参数中检索一个值,base64对其进行编码,然后将其加载到属性中并将其转发到端点。

代理服务代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<inSequence xmlns="http://ws.apache.org/ns/synapse">
    <log level="full"/>
    <property expression="$url:companyId" name="companyId"
        scope="default" type="STRING"/>
    <script language="js"><![CDATA[mc.setProperty('auth',btoa(mc.getProperty('companyId')));]]></script>
    <send>
        <endpoint key="AddToTenant"/>
    </send>
</inSequence> 

我在日志中遇到以下错误:

TID: [-1234] [] [2017-03-20 20:38:57,827] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} -  The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: ReferenceError: "btoa" is not defined. (<Unknown Source>#1) in <Unknown Source> at line number 1
    at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
    at javax.script.CompiledScript.eval(CompiledScript.java:92)
    at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:333)
    at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:265)
    at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:233)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:97)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:59)
    at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
    at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:210)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:325)
    at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:371)
    at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:151)
    at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "btoa" is not defined. (<Unknown Source>#1)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
    at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
    at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2176)
    at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:61)
    at org.mozilla.javascript.gen._Unknown_Source__3603._c_script_0(<Unknown Source>:1)
    at org.mozilla.javascript.gen._Unknown_Source__3603.call(<Unknown Source>)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.gen._Unknown_Source__3603.call(<Unknown Source>)
    at org.mozilla.javascript.gen._Unknown_Source__3603.exec(<Unknown Source>)
    at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
    ... 16 more

我还尝试先将btoa()结果保存在var中,然后使用mc.setProperty()但仍遇到同样的错误。

1 个答案:

答案 0 :(得分:0)

您可以使用base64Encode()函数,请参考[1]。

<property expression="base64Encode(get-property('companyId'))" name="EncodedCompanyId" scope="default"/>

[1] https://docs.wso2.com/display/ESB490/XPath+Extension+Functions#XPathExtensionFunctions-base64Encode()function