Friday, June 17, 2016

How to manipulate WSO2 ESB Registry resources using class mediator - ESB 4.9.0

In some scenarios  we may require to add registry resources (Gov or Conf) remotely without using WSO2 management console or CApps.

 (Note - WSO2 strongly recommend to use Carbon Applications to deploy the artifacts to the carbon products)

I have tested custom class mediator to create registry artifacts and  I was able to successfully manipulate them.

I used  Registry API for the Synapse, to create this mediator. 

Please find the class mediator and the proxy configuration on this scenario.

Class mediator source code
New resources are created at "conf:/repository/new/" location.

 package com.wso2.test;  
 import java.util.Map;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.core.axis2.Axis2MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 public class TemplateManupulator extends AbstractMediator {  
      String action;  
      String resourceName;  
      String resourcePath;  
      public boolean mediate(MessageContext messageContext) {  
           resourcePath = "conf:/repository/new/";  
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)  
                     .getAxis2MessageContext();  
           Object headers = axis2MessageContext  
                     .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
           if (headers != null && headers instanceof Map) {  
                Map headersMap = (Map) headers;  
                action = headersMap.get("Action").toString();  
                resourceName = headersMap.get("Path").toString();  
           }  
           if (!action.equals(null) && !action.equals("")  
                     && !resourceName.equals(null) && !resourceName.equals("")) {  
                if (action.equals("save")) {  
                     messageContext.getConfiguration().getRegistry()  
                               .newResource(resourcePath+resourceName, false);  
                     messageContext  
                               .getConfiguration()  
                               .getRegistry()  
                               .updateResource(  
                                         resourcePath+resourceName,  
                                         messageContext.getProperty("myProperty")  
                                                   .toString().getBytes());                 
                } else if (action.equals("delete")) {  
                     messageContext.getConfiguration().getRegistry()  
                               .delete(resourcePath+resourceName);  
                } else if (action.equals("update")) {  
                     messageContext  
                               .getConfiguration()  
                               .getRegistry()  
                               .updateResource(  
                                         resourcePath+resourceName,  
                                         messageContext.getProperty("myProperty")  
                                                   .toString().getBytes());  
                } else {  
                     System.out.println("Correct header parameters not recieved");  
                }  
           }  
           return true;  
      }  
 }  

Proxy Configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="resourceHandleProxy"  
     transports="https,http"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <log level="full"/>  
      <property name="myProperty"  
           expression="$body/child::*"  
           scope="default"  
           type="STRING"/>  
      <class name="com.wso2.test.TemplateManupulator"/>  
      <drop/>  
    </inSequence>  
   </target>  
   <description/>  
 </proxy>    

After deploying the proxy service with the class mediator we can invoke it using following kind SOAP request.

 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">  
   <soap:Header/>  
   <soap:Body>  
   <name>hello world </name>  
  </soap:Body>  
 </soap:Envelope>  

We required to send the resource name and action as HTTP headers in order to create registry resource. (Supporting actions are save, delete and update)

 Action: save  
 Path: hello.xml  

After  sending the request, log into the ESB management console and go to the "conf:/repository/new/" location using the registry browser. You will see a hello.xml in there.

This is a sample which I have tested and you can change the code and proxy configuration according to your requirement, As a suggestion, you can use VFS transport to get the file inside proxy service and class mediator to create the registry resource.



How to change WSO2 carbon product password using SHELL script

Sometimes users may forget the admin user password and need to change the admin user password. For this kind of a use case we can use the shell script ("chpasswd.sh") which shipped with WSO2 Carbon products.

I will share the steps on changing password on BAM 2.5.0 which configure to use mysql database as the user management datasource.


1) Open a command prompt and go to the <WSO2_BAM>/bin folder on the machine that hosts the Carbon server.
2) You can find "chpasswd.sh" inside the bin folder
3) Then execute the chpasswd.sh with following command line options.
--db-url (The database URL)
--db-driver (The database driver class)
--db-username (The username for the database)
--db-password (The password for the database)
--username (The username of the user whose password is to be changed. If this is not given, you will be prompted for this field later.)
--new-password (The new password of the user whose password is to be changed. If this is not given, you will be prompted for this field later.)
Example -
./chpasswd.sh  --db-driver "com.mysql.jdbc.Driver" --db-url "jdbc:mysql://localhost:3306/BAMDB"  --db-username "root" --db-password "root" --username "admin" --new-password "admin1234"
Note - Please shut down the WSo2 BAM server before execute the shpasswd.sh and if you got error "Database driver not found in classpath" please add the DB driver to the <WSO2_BAM>/lib folder and execute the command.
If success, You will see following output.
Password updated successfully

Sunday, June 12, 2016

How to read registry resource text (.txt file) using class mediator - ESB 4.9.0

In the mediation process, users might need to access the registry resources using a class mediator, which are stored in the governance/config registries in WSO2 ESB. In this sample I have tried to read a text file using a class mediator. 

You can follow Registry API for the Synapse  for more use full method. 


 package com.wso2.test;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 import org.apache.synapse.registry.Registry;  
 import org.apache.synapse.config.Entry;  
 import org.apache.axiom.om.impl.llom.OMTextImpl;  

 public class RecourceFinder extends AbstractMediator { 
  
      String REGISTRY_MAPPING_STORAGE_URL = "conf:/repository/new";  
      String fileName = "converted.txt";  
      public boolean mediate(MessageContext context) {   
           Registry wso2Reg = context.getConfiguration().getRegistry();  
           Entry entry = new Entry(REGISTRY_MAPPING_STORAGE_URL+"/"+fileName);  
           Object obj = wso2Reg.getResource(entry, null);  
           OMTextImpl textImpl = (OMTextImpl) obj;  
           try{  
           String value = textImpl.getText();  
           System.out.println(value);  
           }catch(Exception e){  
                e.printStackTrace();  
           }  
     return true;  
      }  
 }