Skip to content Skip to sidebar Skip to footer

Get Property Value In Javascript With Script Mediator

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 proper

Solution 1:

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();

Solution 2:

You can get property using this code:

<propertyname="ComingRoles"expression="your property or value"scope="default"  /><scriptlanguage="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:

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

It should works with ESB 4.5.0 and above

Solution 3:

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:

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

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

Solution 4:

I did something like below

<propertyexpression="json-eval($.)"name="JSONPayload"scope="default"type="STRING"/><scriptlanguage="js"><![CDATA[var pl_string = mc.getProperty("JSONPayload");
                    var newPayload="{\"event\": " + pl_string + "}";
                    mc.setPayloadJSON(newPayload);]]></script>

Post a Comment for "Get Property Value In Javascript With Script Mediator"