问题
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>
回答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();
回答2:
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
回答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:
<property name="CustomAction" expression="get-property('Action')"/>
Then use the getProperty("CustomAction") in JS to get them.
回答4:
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>
来源:https://stackoverflow.com/questions/22836094/get-property-value-in-javascript-with-script-mediator