Get property value in JavaScript with script media

2019-07-06 13:43发布

Is there a way, inside the javascript code from WSO2 ESB's Script mediator, to get a property's value when this property has a scope different from "default" ?

In case of a property with default scope :

get-property('MyProperty') 

OR

<script language="js">
   mc.getProperty("MyProperty");
</script>

In case of a property with 'transport' scope :

get-property('transport','FILE_NAME')

OR

<script language="js">
   mc.????????
</script>

4条回答
神经病院院长
2楼-- · 2019-07-06 14:10

I don't think mc have get-property method.
Script Mediator use the Apache Bean Scripting Framework for scripting language support. And the mc variable represents an implementation of the MessageContext, named ScriptMessageContext.java.

[Here is the Class of ScriptMessageContext][1]

[1]: https://synapse.apache.org/apidocs/org/apache/synapse/mediators/bsf/ScriptMessageContext.html
You can check any DEFAULT scope property(method) in there.

If not, you may need to put these scope property in custom property. Like:

<property name="CustomAction" expression="get-property('Action')"/>

Then use the getProperty("CustomAction") in JS to get them.

查看更多
趁早两清
3楼-- · 2019-07-06 14:18

It seems that you can not get properties of other scopes than synapse using

mc.get-property("Property Name")

since mc is instance of Synapse.MessageContext in order to get other message context properties I do something like this in java, I don't know that if it is applicable in javascript or not. I do this for axis2 message context properties. Here "context" is the instance of Synapse.MessageContext.

org.apache.axis2.context.MessageContext axis2MessageContext;
            axis2MessageContext = ((Axis2MessageContext)context).getAxis2MessageContext();
查看更多
Lonely孤独者°
4楼-- · 2019-07-06 14:21

You can get property using this code:

<property name="ComingRoles" expression="your property or value" scope="default"  />
<script language="js">var rolelist = mc.getProperty('ComingRoles');</script>

I test it with WSO2ESB 4.9.0

Update:

If your property is not define in default scope, first you must define it in default scope.

for example:

<property name="authheader" expression="get-property('transport','X-JWT-Assertion')"></property>
<script language="js"> var temp_auth = mc.getProperty('authheader')
</script>

It should works with ESB 4.5.0 and above

查看更多
smile是对你的礼貌
5楼-- · 2019-07-06 14:30

I did something like below

 <property expression="json-eval($.)" name="JSONPayload"
      scope="default" type="STRING"/>
    <script language="js"><![CDATA[var pl_string = mc.getProperty("JSONPayload");
                    var newPayload="{\"event\": " + pl_string + "}";
                    mc.setPayloadJSON(newPayload);]]></script>
查看更多
登录 后发表回答