Friday, June 17, 2016

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;  
      }  
 }  


Monday, April 11, 2016

HL7 Inbound endpoints - WSO2 ESB 4.9.0

I had to test ESB hl7 inbound endpoints for a project requirement and I will share the steps followed in there.

I'm using HL7 browser tool to validate the hl7 message and send it to the inbound endpoint. You can download the HL7 browser from this link. Download the ZIP file from the site.  I will guide you how to use this tool in later on this post. Now lets go towards the WSO2 ESB and start configuring the hl7 inbound endpoint.

Following is the inbound endpoint configuration. (This inbound endpoint is configured to listen on port 9090)

 <inboundEndpoint name="hl7_inbound" onError="hl7_errseq"  
     protocol="hl7" sequence="hl7_inseq" suspend="false">  
     <parameters>  
       <parameter name="inbound.hl7.ValidateMessage">true</parameter>  
       <parameter name="inbound.hl7.CharSet">UTF-8</parameter>  
       <parameter name="inbound.hl7.BuildInvalidMessages">false</parameter>  
       <parameter name="inbound.hl7.TimeOut">10000</parameter>  
       <parameter name="inbound.hl7.AutoAck">false</parameter>  
       <parameter name="inbound.hl7.PassThroughInvalidMessages">false</parameter>  
       <parameter name="inbound.hl7.Port">9090</parameter>  
     </parameters>  
   </inboundEndpoint>  


hl7_inseq configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <sequence name="hl7_inseq" onError="hl7_errseq" xmlns="http://ws.apache.org/ns/synapse">  
   <log level="full">  
     <property name="where" value="in hl7_inseq"/>  
   </log>  
   <property name="HL7_APPLICATION_ACK" scope="default" value="true"/>  
   <property name="HL7_RESULT_MODE" scope="default" value="ACK"/>  
   <respond/>  
 </sequence>  

hl7_errseq configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <sequence name="hl7_errseq" xmlns="http://ws.apache.org/ns/synapse">  
   <log level="custom">  
     <property name="where" value="in hl7_errseq"/>  
   </log>  
   <property name="HL7_RESULT_MODE" scope="default" value="NACK"/>  
   <property name="HL7_NACK_MESSAGE" scope="default" value="internal error"/>  
 </sequence>  



Now we can test the inbound endpoint functionality, but for that we need a valid hl7 message. I have created a valid hl7 message and you can download it from here.

Extract the HL7 browser archived file, go inside to the extracted folder and you can see the lib folder, HL7Browser.jar and README.txt

Open a terminal in this location and execute the below command.

 java -jar "Hl7Browser.jar"   

Then you can see the HL7Browser UI.

Open the hl7 message contained file. (You can use the file which I shared) Remember to click "use Advance HL7 v2 parser".


Click on the green right icon to validate the message.


Using connect utility connect to the Host with inbound endpoint port and send the message. (untick the "send  all" and just send the 1st message). You can see the Ack for the sent message.









Sunday, March 20, 2016

ESB 4.9.0 Call mediator, How to get the response headers when the blocking enabled.

In ESB 4.9.0, The Call mediator can invoke a service, either in a synchronous or asynchronous manner. If we use the call mediator in an asynchronous manner (blocking=false) it uses the default pass through transport implementation of the ESB. When the blocking enabled it uses the blocking message sender to send the request to the backend and will wait on the same thread until the response arrive.

If we require to preserver the response message headers we required to set the following property before the call mediator.


  <property name="BLOCKING_SENDER_PRESERVE_REQ_HEADERS" value="false"/>  

Sample proxy configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="first"  
     transports="https"  
     statistics="enable"  
     trace="enable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <property name="BLOCKING_SENDER_PRESERVE_REQ_HEADERS" value="false"/>  
      <call blocking="true">  
       <endpoint>  
         <address uri="https://localhost:8243/services/backend1"  
             trace="enable"  
             statistics="enable"/>  
       </endpoint>  
      </call>  
      <log level="full"/>  
    </inSequence>  
    <outSequence/>  
   </target>  
   <description/>  
 </proxy>  

Wednesday, March 9, 2016

Creating Simple HTTP server to share the files

Some time we required to share our files with the colleagues in the same network. Recently I found an easy way to share files using the python server.

Open a terminal and go to the directory where need to share the files.

Using "cd" command.

Then we can start the server like below.

python -m SimpleHTTPServer

This will start a server with the port 8000 which is the default one. If we need to set a separate port we can use the command like below.

python -m SimpleHTTPServer 5008

We can access the http server using the browser. Like below.

http://IP:port






Friday, March 4, 2016

Get the CarbonApplication list and Deployed artifacts using the Admin services in WSO2 ESB

WSO2 products are managed internally using SOAP Web services known as admin services. Management console uses the Admin Service to facilitate administration capabilities through the UI.
So every activity done through the Management console can be achieved using the Admin services.
Using those Admin services, we can list the installed CarbonApplications with their versions. Also, we can get the artifact list in each Carbonapplication separately.

We need to follow the below steps to enable admin service in the ESB. More details available in this document [1]

1) Set the <HideAdminServiceWSDLs> element to false in the <PRODUCT_HOME>/repository/conf/carbon.xml file.
2) Restart the server.

The adminservices related to the Carbonapplications available in following WSDL.

https://localhost:9443/services/ApplicationAdmin?wsdl

We can check this by calling the above URL in a browser. We can simply call this admin service using the SOAP UI.

Create a new SOAP project in the SOAP UI, using the above WSDL link. Then you will find the project like below [2].



Please use the serverlet port to invoke the adminservices. In our case, we used the offset value as 0. so our serverlet port is 9443.





Using the "listAllApplication" method you can get all the current installed CarbonApplicaions.
Using the "getAppData" method youcan retrieve the CarbonApplication and its artifacts separetely.
Following is sample request, I tested.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mgt="http://mgt.application.carbon.wso2.org">
   <soapenv:Header/>
   <soapenv:Body>
      <mgt:getAppData>
         <!--Optional:-->
         <mgt:appName>asdsd_1.0.0</mgt:appName>
      </mgt:getAppData>
   </soapenv:Body>
</soapenv:Envelope>

The response I received.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:getAppDataResponse xmlns:ns="http://mgt.application.carbon.wso2.org">
         <ns:return xsi:type="ax23:ApplicationMetadata" xmlns:ax23="http://mgt.application.carbon.wso2.org/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ax23:appName>asdsd</ax23:appName>
            <ax23:appVersion>1.0.0</ax23:appVersion>
            <ax23:artifactsDeploymentStatus xsi:type="ax23:ArtifactDeploymentStatus">
               <ax23:artifactName>simpleProxy</ax23:artifactName>
               <ax23:deploymentStatus>Deployed</ax23:deploymentStatus>
            </ax23:artifactsDeploymentStatus>
         </ns:return>
      </ns:getAppDataResponse>
   </soapenv:Body>
</soapenv:Envelope>


[1] https://docs.wso2.com/display/ESB481/Calling+Admin+Services+from+Apps
[2] http://charithaka.blogspot.com/2013/06/invoking-wso2-carbon-admin-services.html


Sunday, February 7, 2016

Writing a Custom Handler for the WSO2 APIM

In various cases, we required to process incoming requests before going into the APIM mediation level (e.g - When we want to authenticate the user) and also we may need to process the response before sending it back to the client. Using Custom Handler we can achieve mentioned use-case.

You can get the details on Custom handlers by following the WSO2 official document.

In this post I will share a custom handler which I used.

Following is the Java class.

 package com.myOwn.test.handler;  
 import java.util.Map;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.core.axis2.Axis2MessageContext;  
 import org.apache.synapse.core.axis2.Axis2Sender;  
 import org.apache.synapse.rest.AbstractHandler; 
 
 public class newTestHandler extends AbstractHandler {  

      public boolean handleRequest(MessageContext messageContext) {  
           // Check whether there is Authorization header, if not available send 401 Status code.   
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)  
         .getAxis2MessageContext();  
     Object headers = axis2MessageContext  
         .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
     try {  
       if (headers != null && headers instanceof Map) {  
         Map headersMap = (Map) headers;  
         if (headersMap.get("Authorization") == null) {  
           headersMap.clear();  
           axis2MessageContext.setProperty("HTTP_SC", "401");  
           headersMap.put("WWW-Authenticate",  
               "Can not authorized without the Authoraization header");  
           axis2MessageContext.setProperty("NO_ENTITY_BODY",  
               new Boolean("true"));  
           messageContext.setProperty("RESPONSE", "true");  
           messageContext.setTo(null);  
           Axis2Sender.sendBack(messageContext);  
           return false;  
         } else {  
           return true;  
         }  
       }  
       return false;  
     } catch (Exception e) {  
       return false;  
     }  
      }  

      public boolean handleResponse(MessageContext messageContext) {  
           // get the message context and read custom header "name".  
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();  
            Object headers = axis2MessageContext.getProperty( org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
           Map headersMap = (Map) headers;  
           System.out.println(headersMap.get("name"));  
           return true;  
      }  
 }  


You can create a Maven project, and import the following POM file.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.virtusa.test</groupId>  
  <artifactId>handler</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>jar</packaging>  
  <name>handler</name>  
  <url>http://maven.apache.org</url>  
  <properties>  
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  <repositories>  
   <repository>  
     <id>wso2-nexus</id>  
     <name>WSO2 internal Repository</name>  
     <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>  
     <releases>  
       <enabled>true</enabled>  
       <updatePolicy>daily</updatePolicy>  
       <checksumPolicy>ignore</checksumPolicy>  
     </releases>  
   </repository>  
   </repositories>  
   <dependencies>  
   <dependency>  
       <groupId>org.apache.axis2.wso2</groupId>  
       <artifactId>axis2</artifactId>  
       <version>1.6.1.wso2v14</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.3-wso2v11</version>  
     </dependency>  
     <dependency>  
       <groupId>commons-logging</groupId>  
       <artifactId>commons-logging</artifactId>  
       <version>1.1.1</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-extensions</artifactId>  
       <version>2.1.2-wso2v6</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.2-wso2v6</version>  
       <exclusions>  
         <exclusion>  
           <groupId>org.apache.axis2</groupId>  
           <artifactId>axis2-codegen</artifactId>  
         </exclusion>  
       </exclusions>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.0</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon</groupId>  
       <artifactId>org.wso2.carbon.apimgt.impl</artifactId>  
       <version>1.2.3</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.3-wso2v11</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon.apimgt</groupId>  
       <artifactId>org.wso2.carbon.apimgt.gateway</artifactId>  
       <version>1.2.4</version>  
     </dependency>  
  </dependencies>  
 </project>