我编写了一个 ESB 资源,它接受测试用例列表,如下例所示:
{
"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" }
]
}
资源将其作为 JSON 数组接收,并使用 XPATH 表达式 //tests/type
循环遍历元素,并生成状态消息(如果可以,则为 200
做任何事情,如果不做的话,ERR
)。
理想情况下,我希望能够将类型增量添加到某种全局数组属性中,最终成为[DSS、ESB、GREG、GW、MB、ID、BOGUS]
所以我可以编写一个脚本来运行它并生成一个有效负载以返回如下所示的内容:
{
"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" }
]
}
我几天来一直在寻找一种方法来做到这一点,但没有成功,所以如果它根本不可能或者我只是在寻找错误的东西,我不知道,但我我真的很想避免生成静态资源,从而以最少的参与实现 future 的扩展。
最佳答案
您可以使用以下脚本将有效负载保存在政府注册表中:
<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 如果资源不存在则创建该资源
- 你可以使用 mc.getPayloadJSON() 而不是 mc.getPayloadXML()
具有资源“TESTSOF”的示例如下:
<root>
<value><child>1</child></value>
<value><child>2</child></value>
<value><child>3</child></value>
</root>
您可以使用以下方式迭代所有“值”:
<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>
JSON 示例: 加载 JSON 作为当前消息:
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('registry','gov:/trunk/Test/TestTypes')"/>
</args>
</payloadFactory>
迭代:
<iterate continueParent="true" sequential="false" preservePayload="false" expression="//tests">
<target>
<sequence>
<log level="full"/>
</sequence>
</target>
</iterate>
关于javascript - 如何在 WSO2 ESB 中创建全局变量 - 必须可更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062949/