简体   繁体   中英

How to create a global variable in WSO2 ESB - must be updatable

I've written an ESB resource which takes in a list of test cases, like the example below:

 { "tests": [ { "type": "DSS", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "ESB", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "GREG", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "GW", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "MB", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "ID", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "BOGUS", "url": "http://localhost:8280/testsuite/general/test" } ] } 

The resource takes this in as a JSON array and loops through the elements using the XPATH expression //tests/type and generates a status message ( 200 if it can do anything, ERR if not).

Ideally, I'd like to be able to incrementally add the types into some sort of a global array property which would eventually become [DSS, ESB, GREG, GW, MB, ID, BOGUS] so I can write a script to run through this and produce a payload to return something like the below:

 { "results": [ { "TYPE": "DSS", "STATUS": "200" }, { "TYPE": "ESB", "STATUS": "200" }, { "TYPE": "GREG", "STATUS": "200" }, { "TYPE": "GW", "STATUS": "200" }, { "TYPE": "MB", "STATUS": "200" }, { "TYPE": "ID", "STATUS": "200" }, { "TYPE": "BOGUS", "STATUS": "ERR" } ] } 

I've been searching for a way to do this for a couple of days now, to no avail so if it's simply not possible or if I've just been looking for the wrong thing, I don't know but I'd really like to avoid producing a static resource, enabling future expansion with minimal involvement for me.

You can save your payload in the gov registry with this script :

<script language="js"><![CDATA[
  importPackage(Packages.org.apache.synapse.config);
  mc.getConfiguration().getRegistry().newResource("gov:/trunk/Test/TestTypes",false);
  mc.getConfiguration().getRegistry().updateResource("gov:/trunk/Test/TestTypes",mc.getPayloadXML().toString());
]]></script> 
  • newResource create the resource if it does not exist
  • you can use mc.getPayloadJSON() instead of mc.getPayloadXML()

A sample with a resource 'TESTSOF' like this :

<root>
<value><child>1</child></value>
<value><child>2</child></value>
<value><child>3</child></value>
</root>

You can iter all "value" with :

<property name="TESTSOF" expression="get-property('registry','gov:/trunk/TESTSOF')" type="OM"/>
<iterate continueParent="true" sequential="false" preservePayload="false" expression="$ctx:TESTSOF//value">
    <target>
        <sequence>
            <log level="full"/>
        </sequence>
    </target>
</iterate>

A sample with JSON : Load JSON as the current message :

<payloadFactory media-type="json">
    <format>$1</format>
    <args>
        <arg evaluator="xml" expression="get-property('registry','gov:/trunk/Test/TestTypes')"/>
    </args>
</payloadFactory>

Iterate :

<iterate continueParent="true" sequential="false" preservePayload="false" expression="//tests">
    <target>
        <sequence>
            <log level="full"/>
        </sequence>
    </target>
</iterate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM