Update Registry file content in WSO2 EI 6.1.1 dynamically

552 views Asked by At

I am storing config file in Config registry like below.

Config Registry File

My use case here is, need to update accessToken tag content from wso2 ei service,because it expires within an hour. how can i pass regenerated accesstoken to this file?

    <!-- Reading config registry file content -->
    <property name="ZohoAppConfig" expression="get-property('registry','conf:/ZohoConfig/ZohoAppConfigFile.xml')" scope="default" type="OM" />
           <property description="accessToken" expression="$ctx:ZohoAppConfig//*[local-name()='accessToken']" name="accessToken" scope="default" type="STRING"/>
            <property description="refreshToken" expression="$ctx:ZohoAppConfig//*[local-name()='refreshToken']" name="refreshToken" scope="default" type="STRING"/>
            <property description="AllPortalsEP" expression="$ctx:ZohoAppConfig//*[local-name()='AllPortals']" name="uri.var.AllPortalsEP" scope="default" type="STRING"/>
            <log level="custom">
                <property name="===ZohoAppConfig===" expression="get-property('ZohoAppConfig')"/>
                <property expression="get-property('accessToken')" name="===accessToken===="/>
                <property expression="get-property('uri.var.AllPortalsEP')" name="===uri.var.AllPortalsEP===="/>
            </log>
<!-- Need to update registry content here -->

Awaiting for your response.

1

There are 1 answers

0
tmoasz On BEST ANSWER

You can modify your ZohoAppConfig, for example using enrich mediator:

<enrich>
   <source clone="true" property="someNewAccesToken" type="property"/>
   <target action="replace" xpath="$ctx:ZohoAppConfig//accessToken" type="custom" xmlns:ns="http://org.apache.synapse/xsd"/>
</enrich>

This will update in your property ZohoAppConfig the token. Next to store back to registry you need use property:

<property expression="$ctx:ZohoAppConfig" name="conf:/ZohoConfig/ZohoAppConfigFile.xml"
        scope="registry" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>

That should works in 6.1.1, I tested it on 6.3.0. but that way is caching the read in memory for 15sec, what could be in some cases problematic. Another way is to store using script mediator, and set the cacheDuration to 0:

    <script language="js"><![CDATA[
mc.getConfiguration().getRegistry().updateResource('conf:/ZohoConfig/ZohoAppConfigFile.xml',mc.getProperty('sample'));
var regEntry = mc.getConfiguration().getRegistry().getRegistryEntry('conf:/ZohoConfig/ZohoAppConfigFile.xml');
regEntry.setCachableDuration(0);
mc.getConfiguration().getRegistry().updateRegistryEntry(regEntry);
]]></script>

I have described it much more with an example, along with some encountered problem here