3

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>
Community
  • 1
  • 1
Jean-Michel
  • 5,926
  • 1
  • 13
  • 19

4 Answers4

3

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();
Reza Ameri
  • 1,803
  • 3
  • 24
  • 32
3

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

Milad Kianmehr
  • 337
  • 2
  • 14
  • when this property has a scope different from "default" ? – Jean-Michel Oct 12 '15 at 07:02
  • I test it with default scope. I think it's work with other scope. If not, define it in default scope. See this: http://sanjeewamalalgoda.blogspot.com/2014/08/how-to-retrive-property-and.html – Milad Kianmehr Oct 13 '15 at 08:03
  • I found an article that get property from transfer scope. The author defines property in default scope. You can see this link:http://wso2.com/library/articles/2013/07/use-of-json-web-tokens-in-an-api-fa%C3%A7ade-pattern/. I updated my answer with this fact. your question in very useful. please inform me if this answer is true. – Milad Kianmehr Nov 11 '15 at 06:59
1

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.

zizifn
  • 395
  • 1
  • 2
  • 14
0

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>