Archive for October, 2013

WSO2 ESB as a JMS sender with ActiveMQ

Posted: October 12, 2013 in esb, jms, proxy

WSO2 ESB as a JMS sender with ActiveMQ.

Advertisements

WSO2 ESB as a JMS sender with ActiveMQ

Posted: October 12, 2013 in linux, ubuntu
Tags: , ,

This tutorial will illustrate how to configure WSO2 ESB as a JMS sender/producer. ESB receives SOAP requests and they are converted to JMS and send to a given queue in ActiveMQ. Fallow the steps below.

  • Download ActiveMQ

Download ActiveMQ from http://activemq.apache.org/download.html

  • Start ActiveMQ
cd [activemq_install_directory]
bin/activemq start
  • Copy Jars to WSO2 ESB

If you are using ActiveMQ version 5.8.0 or later, copy following jars  to <ESB_HOME>/repository/components/lib directory.

  • hawtbuf-1.2.jar
  • activemq-broker-5.9.0.jar
  • activemq-client-5.9.0.jar
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • geronimo-jms_1.1_spec-1.1.1.jar hawtbuf-1.2.jar

If you are using ActiveMQ below 5.8.0 copy the following client libraries from <AMQ_HOME>/lib directory to <ESB_HOME>/repository/components/lib directory.

  • activemq-core-5.5.1.jar
  • geronimo-j2ee-management_1.0_spec-1.0.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • Enable JMS sender for ESB
<transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"\>
  • Add a proxy to ESB
<proxy xmlns="http://ws.apache.org/ns/synapse" name="udara-http-jms" transports="http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="OUT_ONLY" value="true"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
<endpoint>
<address uri="jms:/QueueName?transport.jms.DestinationType=queue&transport.jms.ContentTypeProperty=Content-Type&java.naming.provider.url=tcp://localhost:61616&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&transport.jms.ConnectionFactoryType=queue&transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory"/>
</endpoint>
</target>
</proxy>

“QueueName” – name of the queue in activeMQ the message are stored “tcp://localhost:61616” – ActiveMQ is running locally on port 61616
“QueueConnectionFactory” – We are using the Queue type, not topic

OUT_ONLY indicates that the message exchange flow is one way, only going out. So no response from ESB is expected.

uri="jms:/QueueName?transport.jms.DestinationType=queue&amp;transport.jms.ContentTypeProperty=Content-Type&amp;java.naming.provider.url=tcp://localhost:61616&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;transport.jms.ConnectionFactoryType=queue&amp;transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory"
  • Send a SOAP request to the above created proxy
  • Check the ActiveMQ web console

Browse http://localhost:8161/admin/queues.jsp . You will see a queue name “QueueName” is created and one message is enqueued. It the request we sent in the previous step. Send more requests and refresh the ActiveMQ web console. You will see more messages are coming to the JMS queue.

ActiveMQ Queues

WSO2 ESB Content Aware Routing 1

Posted: October 12, 2013 in linux, ubuntu
Tags: , ,
  • ESB filter mediater

The Filter Mediator can be used for XPath filtering of messages. There are two modes of operation.

We can use the filter mediater when we want to do filtering messages based on XPath. It can be used in two ways

  1. Specify a XPath – The XPath will be evaluated to either True or False
  2. Expression and a regular expression –  Here the expression is matched with the given regular expression. Return true if it matches, false if doesn’t match.

In both cases, if returns true “then” part will be executed, if returns false “else” part will be executed.

In the fallowing example we use the filter in the 2nd way. We try to filter the message based on the action of the message. If the action of the message contains “getQuote”, we log “GetQuote service is called”. Else we log “Another service is called”.


<filter source="get-property('Action')" regex=".*getQuote">
<then>
<log level="custom">
<property name="output" value="GetQuote service is called"/>
</log>
</then>
<else>
<log level="custom">
<property name="output" value="Another service is called"/>
</log>
</else>
</filter>

Example ESB proxy with above filter mediator is below.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="filter_mediater" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<filter source="get-property('Action')" regex=".*getQuote">
<then>
<log level="custom">
<property name="output" value="GetQuote service is called"/>
</log>
</then>
<else>
<log level="custom">
<property name="output" value="Another service is called"/>
</log>
</else>
</filter>
</inSequence>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
</target>
<description></description>
</proxy>
  • This post will describe basic steps in calling an admin service using an axis2 client. Note that if you want to invoke admin services using SOAP UI, please fallow the post by Charitha on Invoking WSO2 Carbon admin services with soapUI
    Let’s say we want to call the admin service name “ProvisioningAdminService” which will give us the set of installed features of the carbon server.

A call to an admin service should be authenticated first. So we call the AuthenticationAdmin admin service first to get a authentication session cookie. We add this session cookie to the subsequent requests so they will be authenticated.

private static String SEVER_URL = "https://backend.appserver.ec2-test.wso2.com/services/";
private static String USER_NAME = "admin";private static String PASSWORD = "admin";
String AUTHENTICATION_ADMIN_SERVICE_URL = "AuthenticationAdmin";

//Create an instance of a Authentication stub
AuthenticationAdminStub authenticationStub = new AuthenticationAdminStub(SEVER_URL +  AUTHENTICATION_ADMIN_SERVICE_URL);

// log in with the username=admin and password=admin.
// They are the default username, passowrd of carbon super admin user.
boolean loginStatus = authenticationStub.login(USER_NAME, PASSWORD,(new URL(SEVER_URL)).getHost());
System.out.println("login " + loginStatus);

ServiceContext serviceContext = authenticationStub._getServiceClient()
.getLastOperationContext()
.getServiceContext();

//Let's get the session cookie string of the last call to authentication admin service. Subsequent requests will be authenticated only if the request contains this session cookie header.
String sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);

// Let's try to call ProvisioningAdminService admin service.
serviceEndPoint = SEVER_URL + "ProvisioningAdminService";
ProvisioningAdminServiceStub provisioningAdminService = new ProvisioningAdminServiceStub(serviceEndPoint);

ServiceClient client2 = provisioningAdminService._getServiceClient();
Options option2 = client2.getOptions();
option2.setManageSession(true);

// set the session cookie of the previous call to AuthenticationAdmin service
option2.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, sessionCookie);

//print the installed services
ProvisioningAdminServiceStub.Feature [] features;
features = provisioningAdminService.getAllInstalledFeatures();
for (ProvisioningAdminServiceStub.Feature f : features) {
     System.out.println(f.getFeatureName());
}

}

If you didn’t set the sessionManage to true for the request to the ProvisioningAdminService you will get the following error since your request does not carries the authentication session cookie.

org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:308)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:450)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:277)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:398)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:224)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
at org.wso2.carbon.feature.mgt.services.prov.ProvisioningAdminServiceStub.getAllInstalledFeatures(ProvisioningAdminServiceStub.java:2132)
at org.wso2.carbon.user.mgt.sample.SampleUserRoleMgtClient.main(SampleUserRoleMgtClient.java:148)

Following are recommended for a comprehensive knowledge on WSO2 authentication.
Authentication and Authorization Choices in WSO2 Platform 
Securing Web Service Integration