Is there any way by which, I can update local entry from esb service. I have to store a token in global variable and need to update it when it is expired. I want to keep it in local entry. Looks like I can not update it from ESB service/sequence.
<localEntry key="TestLocalEntry" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[TestValue]]></localEntry>
If you requirement is to store a global variable and not really update a local entry, you can use esb registry
Try this js to create / update an entry in governance registry (and store current payload xml in this sample) :
<script language="js"><![CDATA[
importPackage(Packages.org.apache.synapse.config);
mc.getConfiguration().getRegistry().newResource("gov:/trunk/test/MyEntry.xml",false);
mc.getConfiguration().getRegistry().updateResource("gov:/trunk/test/MyEntry.xml",mc.getPayloadXML().toString());
]]></script>
Try this xpath expression in your mediation to read the entry :
get-property('gov:/trunk/test/MyEntry.xml')
I have managed to fix this problem with java script.
To get property value use :
<script language="js"><![CDATA[var Token = mc.getEnvironment().getServerContextInformation().getProperty("TokenVal");
mc.setProperty("TokenVal",Token);]]></script>
<property expression="$ctx:TokenVal" name="TokenValue"
type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
and to update value use:
<script language="js"><![CDATA[var TokenValue = mc.getProperty("TokenValue");
mc.getEnvironment().getServerContextInformation().addProperty("TokenVal",TokenValue);
]]></script>
but even then, I want to use registry resources if we have any in wso2 esb
Related
I am getting the server date using the below property mediator
<property expression="get-property('SYSTEM_DATE', 'yyyy-MM-dd')" name="date" scope="default" type="STRING"/>
I get the value as '2021-09-07'. Now I would like to get the previous day and next day from the SYSTEM_DATE (E.g., 2021-09-06 and 2021-09-08). How can I achieve this in WSO2 ESB?
There is no easy way to achieve that, so you need use script mediator. For example something like below.
<script language="js">
var date = new Date();
date.setDate(date.getDate() + 1);
mc.setProperty('NextDay',date.toISOString().slice(0, 10))
date.setDate(date.getDate() - 2);
mc.setProperty('PreviousDay',date.toISOString().slice(0, 10))
</script>
Keep in mind that the above solution does not take into account your timezone offset.
I've created a custom cookie via Javascript and every time I logout, I want to remove this cookie from the browser. How/Where is the best place to perform this operation in the Hybris/Sap Commerce ecosystem?
You can use Spring MVC Interceptors like this.
<alias name="defaultBeforeControllerHandlersList" alias="beforeControllerHandlersList"/>
<util:list id="defaultBeforeControllerHandlersList">
<!-- List of handlers to run -->
<bean class="xyz.CheckUrlsBeforeControllerHandler">
<property name="checkedUrls" ref="checkUrlsList"/>
</bean>
</util:list>
Then Inside CustomHandler use guidCookieStrategy.deleteCookie(request, response);
to delete cookies.
I have a requirement to parse JSON response which contain array of products(P1,P2,P3,etc.). Each product contains multiple information like name, type, cost, etc.
I need to read each product one by one and append additional data got from the another service into an new JSON output. I am thinking of using ForEach component of WSO2 ESB to iterate each product one by one.
Problem is that ForEach component uses ForEachExpression which expects XML expression in the configuration.
Please suggest on the method to parse array of JSON response one by one in WSO2 ESB.
/Abhishek
Can use both Iterate or ForEach mediator for iterating the JSON array, since both are content aware mediators and support JSON.
Which one to use depends upon the specific usecase as Iterate provides the capability to use call / callout / send mediators in the sequence whereas ForEach doesn´t allow it.
For the below given sample JSON request, the following iteration works and the JSON array and objects can be referred like the same.
{
"products":[
{
"product":{
"id":"1234",
"size":"20",
"quantity":"1",
"price":"990",
"type":"Electronics",
"store":{
"id":"001"
}
}
}
]
}
<iterate expression="//products" id="PRD_ITR">
<target>
<sequence>
<sequence key="Product_Enrich_Sequence_s"/>
<!-- For example, Switching sequence based on the product type -->
<switch source="//products/product/type">
<case regex="Computer">
<sequence key="Computer_Product_Enrich_Sequence_s"/>
</case>
<case regex="Mobile">
<sequence key="Mobile_Product_Enrich_Sequence_s"/>
</case>
<case regex="Grocery">
<sequence key="GR_Product_Enrich_Sequence_s"/>
</case>
<default>
<!-- default stuff --!>
</default>
</switch>
</sequence>
</target>
</iterate>
FYR
https://docs.wso2.com/display/ESB490/Iterate+Mediator
https://docs.wso2.com/display/ESB490/ForEach+Mediator
Note: tested in WSO2 ESB 4.9.0
I have the config with development and production sections. This sections contain the URLs of backends. In my inSequence i need it to Callout to these backends several times per request.
<config>
<env>prod</env>
<backend env="prod">http://localhost:1234/</backend>
<backend env="dev">http://localhost:2345/</backend>
</config>
I read this config from Local Entry (as XML) and want to set Callout's URL as an Property.
I don't want to hardcode these backends inside my code with "Switch" statement, because it's possible to use more than two environments.
Could you please show me an example?
Thank you.
You can read xml file in registry. Simply define property of OM type like this:
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM" />
Then you can see the value by logging like this:
<log level="custom"> <property name="test.b" expression="$ctx:test//b" /> </log>
And in the xml file that you have put in the root of registry, you would fill it like:
<a>Hello<b>WSO2</b></a>
I have learned it from this link.
I found the answer. According to source of Callout mediator:
CalloutMediator.java
It uses "To" header if URL is not specified.
[WSO2 ESB V4.5.0]
What is wrong with how I'm configuring the enrich mediator to accumulate XML? I have a sequence of n PojoMediators that retrieve XML from a database with each setting a context property with the XML represented as a string. For example, after the first PojoMediator executes, its' context property is set to:
customerInformation = <cust><id>1</id><oc></oc><ca>0</ca></cust>
and I'm trying to enrich the body with that XML content but end up with:
[snip]
</header>
<cust><id>1</id><oc></oc><ca>0</ca></cust></root></soapenv:Body></soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
..where the enrich mediator is escaping the referenced "custInfo" XML. My enrich configuration is:
<enrich>
<source type="property" property="custInfo"/>
<target type="body"/>
</enrich>
Is there a means to coerce the enrich mediator to treat the property ("custInfo") as an XML fragment rather than as straight text? I'm assuming that this is why the XML is getting escaped as the mediator believes it is setting the content of a node rather than specifying an XML fragment.
How you defined property ? Can you try after adding
type="OM"
to the property definition and try again?