Madhup Srivastava
Madhup Srivastava

Reputation: 446

Modifying config registry resource content during mediation in WSO2 ESB

I have a scenario where I need to store simple counter in config registry and increment it at end of sequence flow. Reason we need to store in config registry is in case server get restarted we have last counter value persisted. Can someone suggest how to increment the counter in config registry ?

Upvotes: 2

Views: 814

Answers (3)

I used this way to get json payload from POST request and stored in the registry on xml format

        <datamapper config="gov:datamapper/conversionToSaveInRegistry.dmc" description="conversionToSaveInRegistry" inputSchema="gov:datamapper/conversionToSaveInRegistry_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/conversionToSaveInRegistry_outputSchema.json" outputType="XML" xsltStyleSheet="gov:datamapper/conversionToSaveInRegistry_xsltStyleSheet.xml"/>
        <property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
        <script language="nashornJs"><![CDATA[
            var body = mc.getPayloadXML();
            var registryPath = "gov:/generated/date.xml";
                
                if(body != null && body != ''){
                    var existingProperty = mc.getConfiguration().getRegistry().getResource(registryPath);
                    if(existingProperty == null){
                        // Create the registry entry if no such entry exists.
                        mc.getConfiguration().getRegistry().newResource(registryPath, false);
                        mc.getConfiguration().getRegistry().updateResource(registryPath, body);
                        
                    } else {
                        // Update the registry entry if it already exists.
                        mc.getConfiguration().getRegistry().updateResource(registryPath, body);
                    }
                }]]></script>
        <property name="NO_ENTITY_BODY" scope="axis2" type="BOOLEAN" value="true"/>
        <property name="HTTP_SC" scope="axis2" type="STRING" value="201"/>

Upvotes: 0

I have this solution for you!!

  <script language="nashornJs"><![CDATA[
                 
                var body = mc.getPayloadXML();
                print(body);
                var registryPath = "gov:/portales/date.xml";
                    
                    if(body != null && body != ''){
                        var existingProperty = mc.getConfiguration().getRegistry().getResource(registryPath);
                        print(body);
                        if(existingProperty == null){
                        print(body);
                            // Create the registry entry if no such entry exists.
                            mc.getConfiguration().getRegistry().newResource(registryPath, false);
                            mc.getConfiguration().getRegistry().updateResource(registryPath, body);
                            
                        } else {
                        print(body);
                            // Update the registry entry if it already exists.
                            mc.getConfiguration().getRegistry().updateResource(registryPath, body);
                        }
                    }]]></script>

the idea was taken from http://wso2-oxygen-tank.10903.n7.nabble.com/How-to-Store-Log-Message-in-a-Registry-File-in-EI-td159169.html

Upvotes: 0

Jean-Michel
Jean-Michel

Reputation: 5946

Sample javascript you can use in your mediation to save current message inside registry :

<script language="js"><![CDATA[
    importPackage(Packages.org.apache.synapse.config);
    mc.getConfiguration().getRegistry().newResource("gov:/trunk/mypath/MyResource.xml",false);
    mc.getConfiguration().getRegistry().updateResource("gov:/trunk/mypath/MyResource.xml",mc.getPayloadXML().toString());
]]></script>

newResource is used the first time to create the resource

Upvotes: 4

Related Questions