Standalone Java REST client

We write a standalone Java REST client for the RESTful service described in this post – The code can be integrated into a web application as well when the need to consume a RESTful service is required.


import java.math.BigInteger;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class JsonRestful {

	StatusLine statusLine = null;

	public InputStream processRequest(String baseUrl, String contextPath,
			String requestBody, String contextParam, String requestType)
			throws Exception {
		HttpResponse response = null;
		InputStream responseStream = null;
		try {
			HttpParams httpParameters = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParameters, 100000);
			HttpConnectionParams.setSoTimeout(httpParameters, 100000);
			HttpClient httpclient = new DefaultHttpClient(httpParameters);
			String url = makeUrl(baseUrl, contextPath, requestBody,
			if (requestType.equals("GET")) {
				HttpGet httpGet = new HttpGet(url);
				httpGet.setHeader("Content-type", "application/json");
				response = httpclient.execute(httpGet);
			} else if (requestType.equals("POST")) {
				HttpPost httppost = new HttpPost(url);
				StringEntity params = new StringEntity(requestBody, HTTP.UTF_8);
				httppost.setHeader("Content-type", "application/json");
				response = httpclient.execute(httppost);
			statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				responseStream = response.getEntity().getContent();
			} else {
				throw new Exception("" + statusLine.getStatusCode());
		} catch (ClientProtocolException e) {
			throw new Exception("" + statusLine.getStatusCode());
		} catch (NoSuchAlgorithmException e) {
			throw new Exception("" + statusLine.getStatusCode());
		} catch (UnsupportedEncodingException e) {
			throw new Exception("" + statusLine.getStatusCode());
		} catch (IOException e) {
			throw new Exception("" + statusLine.getStatusCode());
		return responseStream;

	private String makeUrl(String baseUrl, String contextPath,
			String requestBody, String contextParam)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		StringBuilder urlBuilder = new StringBuilder();
		// urlBuilder.append(URLEncoder.encode(makeToken(requestBody)));
		// if MD5 auth is required
		System.out.println(":-) " + urlBuilder.toString());
		return urlBuilder.toString();

	private String makeToken(String request) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		String str = request + "password";
		final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		final byte[] resultByte = messageDigest.digest();
		BigInteger bigInt = new BigInteger(1, resultByte);
		String hashtext = bigInt.toString(16);
		// We need to zero pad it if you actually want the full 32 chars.
		while (hashtext.length() < 32) {
			hashtext = "0" + hashtext;
		return hashtext;



import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.http.client.ClientProtocolException;

public class MattizRestClient {

	public static void main(String args[]) throws Exception {
		new MattizRestClient().getResponseData();

	public JSONObject getResponseData() throws Exception {
		JSONObject reqObj = new JSONObject();
		JSONObject responseObj = null;
		try {
			String baseUrl = "http://localhost:8080/RestServiceExampleWithJson/restservices/";
			String contextPath = "mattiz/";
			String requestBody = reqObj.toString();
			System.out.println("requestBody--->>" + requestBody);
			String contextParam = null;
			String requestType = "GET";
			InputStream is = new JsonRestful().processRequest(baseUrl,
					contextPath, requestBody, contextParam, requestType);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				System.out.println("Response raw string :" + line);
				// Prints out JSON response string
			// Parse response string
			responseObj = (JSONObject) JSONSerializer.toJSON(sb.toString());
		} catch (ClientProtocolException e) {
			throw new RuntimeException("MattizException");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("MattizException");
		} catch (IOException e) {
			throw new RuntimeException("MattizException");
		return responseObj;


Start tomcat so that the REST service described in this post- is available with this URL –


On running the main class above the following response is obtained:

requestBody—:-) >>{}
Response raw string :{“data”:”Hallo Mattiz World!”}

See snapshot below to find the jars required on the classpath.

The application can be downloaded here

Short Note On Application Performance Tuning

We discuss the following basic points in this post- Heap, Heap Dumps and Thread Dumps, Memory Leaks and Connection leaks and Connection pooling
What is java heap?
When new java objects are created they are allocated memory in the java heap. Once they are no longer referenced by other java objects they may be garbage collected and memory is returned to the heap. Xms and Xmx are java switches that allow you to manipulate memory allocated to the heap.
There are two different types of heap – the new generation heap where new objects are allocated memory and the old generation heap that accommodates objects that have been in memory over a longer period. Perm memory is memory allocated to objects that have been loaded into memory through the classloader and will be removed from the perm memory only when the classloader is garbage collected.
OuOfMemory errors can be due to too many large objects in the session object in a web application and when there is not enough memory to create new objects in the java heap. This may occur when java objects are not returned back to the heap by the garbage collector. OutOfMemory exception may occur due to memory leaks. Memory leaks can be debugged by using the command:

java -verbose:gc

which will provide information on the garbage collector, objects garbage collected, etc and is used to fix garbage collector problems. A memory leak may be fixed by analyzing the logs from the above command.
Calls to Runtime.gc or Sytem.gc() should not be explicitly made. Note that threads including the JVM are paused when the major GC runs. Minor GCs do not cause the JVM to pause, but to some extent it is okay, as long it doesn’t cause noticeable slowness.
Java heap dump and thread dumps can be used to produce a stacktrace of the heap/ running threads. The stacktraces look similar to an exception stacktrace. Connection leaks are more difficult to detect since we may not know exactly where in the code a certain connection was not closed. This may be analysed by generating heap/ thread dumps using use case by use case.
Sometimes a memory leak/ application slowdown / hangup, may be difficult to find after it has been fixed. In that case one should enable GC logging / heap/ thread dumps tracing so that the next time this happens the cause is correctly found.
If a thread dump shows too many threads running it may indicate a reason for application slowdown. Thread dumps are mostly to find what the JVM is busy doing. If we see an unusually high number of threads such as more than 300 or so that means the JVM is working too hard on something and then you should look at what these threads are doing. If you see a lot of threads stuck on reading from the db that indicates db slowness. If you see too many threads servicing Servlet requests that means the JVM is overloaded with too many users.
Sometimes it helps to take thread dumps a few minutes apart and compare the two thread dumps to see if threads are moving along or stuck. If thread A in dump1 is doing the same thing in dump2 taken later, then it’s taking too long; usually you should be able to see it moving along and do different things.
It’s ok to have long running threads as some threads are by design supposed to be long lived e.g. you can have a pool of threads to consume JMS messages. Those threads will be alive for a long time and will be reused over and over again. So that’s normal as long as they don’t hold big objects for a long time, it’s not a memory leak.

Instance Variable Declaration in a Servlet vs Instance Variables Declaration in Handler classes

There is a common misunderstanding of how servlet instance variables work.
Let’s consider a Servlet, say the Controller Servlet in Struts(Action Servlet). At any given time there is only one instance of this Servlet. This Servlet is multi-threaded so it does not have state. State in the sense it has no instance variables with values. Many requests can be serviced by this Servlet by running multiple threads on the same instance.
Instance variables can be declared in a servlet but these variables are shared by all threads. No single thread can rely on using the variable for itself, so threads can never depend on the value of an instance variable of a Servlet.
This Servlet passes control to handlers say Action classes in Struts. There are multiple instances of these action classes with their own state and these action classes are not multi-threaded in that sense.
There is one multi-threaded Servlet and multiple handler classes running on different threads. One multi-threaded Servlet can create new action-class instances which are dedicated instances.

Creating a Simple Web Service using Apache Axis 1.4

Create two projects namely AxisServerExample and AxisClient example.
Download the Axis installation and add the jars in the lib folder of the installation to the lib folder of the Server project. Implement in the server project.
MattizAxisServerImpl class has the following code:


public class MattizAxisServerImpl {
 * @param person
 * @return message
	public String returnMessage(String person){

		return "Hallo "+person+" to the World of Axis!";


Run the following two commands from the command prompt to use the java2wsdl utility to create the wsdl from the java class.

set PROJECT_HOME=I:/juno-ws/mvn_custom_plugin/AxisServerExample (This is the location of my axis server project)

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar;%PROJECT_HOME%/bin"  org.apache.axis.wsdl.Java2WSDL -o %PROJECT_HOME%/resources/mattiz.wsdl -l "http://localhost:8080/axis/services/MattizAxisTest" -n  "urn:mattiz" -p"" "urn:mattiz"

This will generate the wsdl file from MattizAxisServerImpl class in the resources folder of the Server project.

Run the following from the command prompt to use the wsdl2java utility to create the java client classes and deploy.wsdd from the wsdl file.

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar" org.apache.axis.wsdl.WSDL2Java -o %PROJECT_HOME%/src -p com.mattiz.axis.request -s %PROJECT_HOME%/resources/mattiz.wsdl

deploy.wsdd is created in com.mattiz.axis.request folder along with web serice client stub classes in the Server project.

Copy contents of axis installation webapps folder to tomcat. Modify web.xml by uncommenting this line:


Start tomcat and run the following from the command prompt to use the Apache Admin Client to generate your server-config file (wsdd).

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar" org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService %PROJECT_HOME%/src/com/mattiz/axis/request/deploy.wsdd

server-config.wsdd is created in the axis deployment in tomcat in the WEB-INF folder. Copy this to resources folder under Server project.

You are free to delete the axis installation in webapps of tomcat after this step.

Copy web.xml from the axis distribution at axis-1_4/webapps/axis/WEB-INF to WEB-INF under Server project. No changes are required to this file.
Modify returnMessage method in AxisServerExample/src/com/mattiz/axis/request/

which was generated with the stubs, to look like this:

    public java.lang.String returnMessage(java.lang.String person) throws java.rmi.RemoteException {
        return new MattizAxisServerImpl().returnMessage(person);

Run the following build.xml which creates the server side web service war file – mattiz.war.

<?xml version="1.0"?>
<project name="mattiz" default="war" basedir=".">
	<path id="project.class.path">
		<fileset dir="lib">
			<include name="*.jar" />
	<target name="clean">
		 <delete failonerror="true" includeemptydirs="true">
            <fileset dir="target/classes" includes="**/*" />
            <fileset dir="target" includes="**/*" />
	<target name="compile" depends="clean">
		<mkdir dir="target" />
		<mkdir dir="target/classes" />
		<javac srcdir="src" destdir="target/classes" optimize="off"
			classpathref="project.class.path" />
	<target name="war" depends="compile">
		<copy file="resources/server-config.wsdd" todir="target/classes" />
		<war destfile="target/mattiz.war" webxml="WEB-INF/web.xml">
			<classes dir="target/classes">
				<include name="**/*.*"/>
			<lib dir="lib">
				<include name="**/*.jar" />

Copy mattiz.war to tomcat and start tomcat server.

Run the following url on a browser to view the wsdl file. If the web service is successfully created you will see a xml version of the wsdl file.


Copy contents of com.mattiz.axis.request package to the Client project. Exclude MattizAxisTestSoapBindingImpl class.

Add activation.jar and javamail jars to the classpath of Client project in addition to the axis jars in Server project’s lib folder.

Create thus:

package com.mattiz.axis.request;

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;

public class MattizWebServiceConsumer {

	public static void main(String[] args) {
		try {
			String endpoint = "http://localhost:8080/mattiz/services/MattizAxisTest";

			MattizAxisServerImplServiceLocator srvLoc = new MattizAxisServerImplServiceLocator();
			srvLoc.setEndpointAddress(new javax.xml.namespace.QName(
					"urn:mattiz", "MattizAxisTest"), endpoint);
			MattizAxisServerImpl serviceimpl = srvLoc.getMattizAxisTest();

			((javax.xml.rpc.Stub) serviceimpl)._setProperty(
					"javax.xml.rpc.session.maintain", Boolean.TRUE);
			String person = "Matty";

			String ret = serviceimpl.returnMessage(person);
			System.out.println("Message " + ret);

		} catch (ServiceException e) {
			// TODO Auto-generated catch blockS
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
		} catch (Exception e) {
			// TODO Auto-generated catch block

Run as standalone.

You see the following messsage on the console when the web service java client calls the server:

Message Hallo Matty to the World of Axis!


The Axis server example can be downloaded here here
The Axis client example can be downloaded here

Custom Maven Plugin To Investigate Contents of Zip File

Run the following command on command line at the root of your workspace folder

mvn archetype:create -DgroupId=com.mattiz.maven.plugins -DartifactId=zipped-file-content-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo

It will create a default pom.xml similar to this in a folder called zipped-file-content-plugin:

<project xmlns="" xmlns:xsi=""
  <name>zipped-file-content-plugin Maven Mojo</name>

For our purpose we don’t need to add any other dependencies. This may not be the case for complex functionalities such as parsing an xml file.

Goto MattizCustomPlugin/zipped-file-content-plugin folder on command line and run mvn eclipse:eclipse. This will create an eclipse project for you. Import the eclipse project into a new workspace.

A java file called is created under src/main/java/com/mattiz/mavenplugins. Rename it to and modify the contents to look like this:

package com.mattiz.maven.plugins;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.util.Enumeration;

 * @goal explore
 * @phase process-sources
public class MattizZipDetectivePlugin extends AbstractMojo {
	 * Location of the file.
	 * @parameter expression="${}"
	 * @required
	public void execute() throws MojoExecutionException {
		try {
			listFiles(new File(
		} catch (ZipException e) {
		} catch (IOException e) {
		} catch (Exception e) {

	public void listFiles(File zippedFile) throws ZipException, IOException {
		ZipFile zipFile = new ZipFile(zippedFile);
		try {
			Enumeration<? extends ZipEntry> entries = zipFile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				File file = new File(entry.getName());
		} finally {

Note this change:

@goal explore
This defines the plugin goal.

Go to folder where the pom file lies, on command prompt and run
mvn install. This creates the snapshot jar containing your custom maven plugin here:

Create a package called package_holder under root of project and place a zip file with arbitrary content called here.

Run the following command from the command prompt.

mvn com.mattiz.maven.plugins:zipped-file-content-plugin:1.0-SNAPSHOT:explore

The command line will list the contents of

The source code can be downloaded here