Tuesday, July 20, 2010

Sending UTF-16 messages through Synapse

When fixing SYNAPSE-662 and SYNAPSE-670, I had to send UTF-16 messages to the SimpleStockQuoteService to verify the fixes. Following are the sample configuration and client code that I used to verify the fixes.

1. Start the Synapse ESB with the following configuration.

<definitions xmlns="http://ws.apache.org/ns/synapse">
<switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples/xsd">
<case regex="IBM">
<!-- the property mediator sets a local property on the *current* message -->
<property name="symbol" value="Great stock - IBM"/>
</case>
<case regex="MSFT">
<property name="symbol" value="Are you sure? - MSFT"/>
</case>
<default>
<!-- it is possible to assign the result of an XPath expression as well -->
<property name="symbol"
expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"
xmlns:m0="http://services.samples/xsd"/>
</default>
</switch>

<log level="custom">
<!-- the get-property() XPath extension function allows the lookup of local message properties
as well as properties from the Axis2 or Transport contexts (i.e. transport headers) -->
<property name="symbol" expression="get-property('symbol')"/>
<!-- the get-property() function supports the implicit message headers To/From/Action/FaultTo/ReplyTo -->
<property name="epr" expression="get-property('To')"/>
</log>

<!-- Send the messages where they are destined to (i.e. the 'To' EPR of the message) -->
<send/>
</definitions>


2. Start the Axis2 server and deploy the SimpleStockQuoteService.

3. Use the following axis2 client (which generates a UTF-16 message) to invoke the service.

package org.wso2.esb;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

import java.lang.*;

public class MyClient {
public static OMElement createPayload(){
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://services.samples", "ns");
OMElement method = fac.createOMElement("getQuote", omNs);
OMElement value1 = fac.createOMElement("request", omNs);
OMElement value2 = fac.createOMElement("symbol", omNs);

value2.addChild(fac.createOMText(value1, "IBM"));
value1.addChild(value2);
method.addChild(value1);
return method;
}

public static void main(String args[]){
OMElement payload = createPayload();
ServiceClient serviceclient;

ConfigurationContext configurationContext = null;
try {
configurationContext = ConfigurationContextFactory.
createConfigurationContextFromFileSystem("/home/heshan/Dev/trunk/synapse/modules/distribution/target/synapse-2.0.0-SNAPSHOT/samples/axis2Client/client_repo",
"/home/heshan/Dev/trunk/synapse/modules/distribution/target/synapse-2.0.0-SNAPSHOT/samples/axis2Client/client_repo/conf/axis2.xml");

serviceclient = new ServiceClient();
Options opt = new Options();

opt.setTo(new EndpointReference ("http://localhost:8281/services/StockQuote"));
opt.setAction("urn:getQuote");
opt.setProperty(org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,"UTF-16");
opt.setSoapVersionURI(org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
serviceclient.setOptions(opt);

OMElement ome = serviceclient.sendReceive(payload);
System.out.println("The output is : " + ome);

} catch (AxisFault axisFault) {
axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

}
}

Wednesday, July 14, 2010

Extend the Axis2 Framework to Support JVM Based Scripting Languages

This article explains how to extend the Axis2 framework to support Java Virtual Machine (JVM) based scripting languages such as Jython, JRuby, etc. It provides a high level overview of the subject, covering some key concepts of Apache Axis2 and how it can be used to come up with an extension to a JVM based scripting language. After going through this article, a developer will be able to extend the Axis2 framework to support the JVM based scripting language of his or her choice.

If you are interested, read the full article on InfoQ.com.

I take this opportunity to thank Nandika for doing the technical review and Charles Humble for doing the editorial review.