Archive for the 'C#' Category

Building a JSON Axis2 REST Client for a WCF Service.

Using JSON as the message format for a WCF REST service has some advantages
if you are building a backend service for a purely browser based application.

For instance, JSON is natively supported in all of the major web browsers.

I had to build a REST client using Axis2 that uses the JSON message format.

The blog post Enabling JSON in Apache Axis2 explains the conventions that are used to map XML to JSON and how to configure axis2.xml so that your client application can use the XML/JSON mapping convention of your choice.

I chose the mapped namespace convention which can be configured by adding the two lines to your axis2.xml

<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>

<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONOMBuilder"/>

If you choose the mapped namespace convention you may get an exception that says “JSON mapped namespaces are not supported in Axis2”. If you see the above Axis2 exception then you need to patch jettison.

Instructions on the exception and the patch are in the “Building a JSON web service with Java and Axis2” post by Marcus Schiesser.

The ServiceClient (org.apache.axis2.client.ServiceClient) object can be used to make REST API calls in Axis2.

The ServiceClient can be configured using the Options (org.apache.axis2.client.Options) object.

For a REST style service invocation you need the following options,


Options options = new Options();
options.setTo(new EndpointReference("http://your.service.endpoint.url"));
options.setProperty(Configuration.MESSAGE_TYPE, "application/json");
options.setProperty(Configuration.ENABLE_REST, Constants.VALUE_TRUE);

//reuse HTTP clients
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);
options.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

The httpClient object is created via the MultiThreadedHttpConnectionManager. This is needed for managing the HTTP connections if your client is used in a multithreaded application. See the post “Improving Axis2 Client HTTP Transport Performance”

Finally, you have to engage the addressing module and set the ServiceClient options


client = new ServiceClient();
client.engageModule("addressing");
client.setOptions(options);

The ServiceClient sendReceive call takes an OMElement which I created using custom XML serialization on the message objects.

Make sure you cleanup the http connections via the httpconnectionManager used to create the httpClient. Again. see the post “Improving Axis2 Client HTTP Transport Performance” on how to do this correctly.

The sendReceive call can be made non-blocking if you are looking for further improvements in performance.

The response returned by the sendReceive call is an OMElement again and can be de-serialized using custom XML de-serialization on your message objects.

If you are looking for pure SOAP interoperability between WCF .NET SOAP client and an Axis2 service be sure to chek out my other post Axis2 and WCF .Net Interoperability (Interop)

TarTool – Windows tar gzip tgz extraction tool

TarTool is a tiny windows command line tool to extract tar gzip (tar.gz or tgz extension) files.

You can download TarTool , unzip and run TarTool.exe as a command line executable.

You can also download TarTool from this alternate download location if the link above is down or does not work for you.

The source code  for TarTool is now hosted on codeplex.

I wanted to play around with the Microsoft Shared Source CLI SSCLI. The download is only available as a tgz extension. Imagine that ūüôĀ

After searching the interwebs for tools that would extract tar gzip files , I was disappointed since there was no simple tool to extract tar gzip files on my windows machine.

There are a few tools out there like WinRAR etc., but I found them too bloated for my task.

So I wrote a little tool using SharpZipLib, the open source Zip, GZip, Tar and BZip2 library (great job guys, BTW).

SharpZipLib does most of the heavy lifting so the core of TarTool  is less than ten lines of C#. I can host the source code if there is enough interest.

The source is now hosted on codeplex.

Enjoy!

 Update (05/06/2009 17:00 CST) :

TarTool now has an addtional option to untar tar file formats.

TarTool -x sample.tar temp

will untar the sample.tar contents into the temp folder.

Update (04/02/2012 11:35 PM CDT)
bzip2 decompression is now supported in TarTool 2.0 Beta .
TarTool -xj sample.tar.bz2 temp
or
TarTool -j sample.bz2