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>  




Saturday, January 9, 2016

Installing Erlang OTP 18.2.1 from source on Ubuntu 15.04

Run these commands on the command line to do a quick install: Assume you have installed the oracle JDK 1.7 or later. (Open JDK is also supported)


sudo apt-get install fop 
sudo apt-get install libncurses5-dev
sudo apt-get install unixodbc-dev
sudo apt-get install g++
sudo apt-get install libssl-dev


Download Erlang OTP source file using http://www.erlang.org/download.html . Make sure it is OTP source.

Let assume you downloaded  the otp_src_18.2.1.tar.gz file.
Then run following commands.

tar -zxf otp_src_18.2.1.tar.gz
cd  otp_src_18.2.1
./configure
make
sudo make install

Should be ready to go now!


Troubleshooting


What happens at each step if the prerequisite packages are not installed first

If you download, extract and try to configure Erlang without installing the prerequisites first, this is what you will see:

./configure
...
configure: error: No curses library functions found
configure: error: /bin/bash '/usr/local/src/otp_src_R14B02/erts/configure' failed for erts

So now you do this:

sudo apt-get install libncurses5-dev

Now let's try it again:

./configure

....

*********************************************************************
**********************  APPLICATIONS DISABLED  **********************
*********************************************************************

crypto         : No usable OpenSSL found
jinterface     : No Java compiler found
odbc           : ODBC library - link check failed
orber          : No C++ compiler found
ssh            : No usable OpenSSL found
ssl            : No usable OpenSSL found

*********************************************************************
*********************************************************************
**********************  APPLICATIONS INFORMATION  *******************
*********************************************************************

wx             : wxWidgets not found, wx will NOT be usable

*********************************************************************
*********************************************************************
**********************  DOCUMENTATION INFORMATION  ******************
*********************************************************************

documentation  : 
                 fop is missing.
                 Using fakefop to generate placeholder PDF files.

*********************************************************************


ok that is better, but lets knock off some of these missing items.

Run the following at the command line:

sudo apt-get install fop 
sudo apt-get install libssl-dev
sudo apt-get install unixodbc-dev
sudo apt-get install g++

./configure
....
*********************************************************************
**********************  APPLICATIONS INFORMATION  *******************
*********************************************************************

wx             : wxWidgets not found, wx will NOT be usable

*********************************************************************

ok - looking better

Now lets run these:

sudo apt-get install libwxbase2.8
sudo apt-get install libwxgtk2.8-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libqt4-opengl-dev

./configure

...
configure: creating ./config.status
...

ah, good nothing missing and no errors

time to run make

make

Ok that passed, now run make install:

sudo make install

Ok, now where is erlang going to run from:

which erl

/usr/local/bin/erl

good, this will override /usr/bin/erl 
Now time to do a little test of erlang:

 erl
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.8.3  (abort with ^G)
1> wx:demo().
{wx_ref,35,wxFrame,<0.34.0>}
2> 

If you get the wxErlang widgets panel, then things are probably ready to go.