Simple REST client in java

Today most of the mobile applications that used to communicate to some server use REST services. These services are also common practice to use with JavaScript or jQuery. Right now I know 2 ways to create client for REST service in java and in this article I will try to demonstrate both the ways I know hoping that it will help someone in some way.

1. Using Apache HttpClient

The Apache HttpClient library simplifies handling HTTP requests. To use this library you have to download the binaries with dependencies from their website.
Here is the code for HTTP GET method:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet("http://restUrl");
		HttpResponse response = client.execute(request);
		BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
		String line = "";
		while ((line = rd.readLine()) != null) {
		  System.out.println(line);
		}
	}
}

And for Post method; for sending simple string in post:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://restUrl");
		StringEntity input = new StringEntity("product");
		post.setEntity(input);
		HttpResponse response = client.execute(post);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		String line = "";
		while ((line = rd.readLine()) != null) {
			System.out.println(line);
		}
	}
}

You can also send full JSON or XML of a POJO by putting String representing JSON or XML as a parameter of StringEntity and then set the input content type. Something like this:

StringEntity input = new StringEntity("{\"name1\":\"value1\",\"name2\":\"value2\"}"); //here instead of JSON you can also have XML
input.setContentType("application/json");

For JSON you can use JSONObject to create string representation of JSON.

JSONObject json = new JSONObject();
json.put("name1", "value1");
json.put("name2", "value2");
StringEntity se = new StringEntity( json.toString());

And for sending multiple parameter in post request:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://restUrl");
		List nameValuePairs = new ArrayList(1);
		nameValuePairs.add(new BasicNameValuePair("name", "value")); //you can as many name value pair as you want in the list.
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		HttpResponse response = client.execute(post);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		String line = "";
		while ((line = rd.readLine()) != null) {
			System.out.println(line);
		}
	}
}

2. Using Jersey

Jersey is the reference implementation forJSR-311 specification, the specification of REST support in Java. Jersey contains basically a REST server and a REST client. it provides a library to communicate with the server producing REST services. For http get method:

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(UriBuilder.fromUri("http://restUrl").build());
		// getting XML data
		System.out.println(service. path("restPath").path("resourcePath").accept(MediaType.APPLICATION_JSON).get(String.class));
		// getting JSON data
		System.out.println(service. path("restPath").path("resourcePath").accept(MediaType.APPLICATION_XML).get(String.class));
	}
}

There are also other media formats in which you can get the response like PLAIN or HTML.
And for HTTP POST method:

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource webResource = client.resource(UriBuilder.fromUri("http://restUrl").build());
		MultivaluedMap formData = new MultivaluedMapImpl();
		formData.add("name1", "val1");
		formData.add("name2", "val2");
		ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);
		System.out.println("Response " + response.getEntity(String.class));
	}
}

If you are using your POJO in the POST then you can do something like following:

ClientResponse response = webResource.path("restPath").path("resourcePath").
type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, myPojo);
System.out.println("Response " + response.getEntity(String.class));

Here myPojo is an instance of custom POJO class.
You can also use Form class from Jersey to submit multiple parameters in POST request:

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.representation.Form;
public class Test {
	public static void main(String[] args) throws ClientProtocolException, IOException {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(UriBuilder.fromUri("http://restUrl").build());
		Form form = new Form();
		form.add("name1", "value1");
		form.add("name2", "value1");
		ClientResponse response = service.path("restPath").path("resourcePath").
		type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);
		System.out.println("Response " + response.getEntity(String.class));
	}
}

Let me know if you find any difficulty while using these code samples. Feel free to comment. 🙂

Advertisements

Gateway interaction in Smack API

Gateway interaction is described by XEP-0100. The query xmlns (namespace) used for this is same as IN-Band registration, that is “jabber:iq:register”. Smack API provides an easy way handle gateway interaction with the server. It has predefined classes for the required namespace and operations which one can easily use and do the thing. In this article I will explain how one can register, edit, unregister and retrieve gateway information using Smack library. I have listed both XML and code using Smack API for the purpose. I have used Openfire server with Kraken plugin in it for gateway related operations described in this article.

1. Gateway service discovery

First you have to send an IQ-get with xmlns of the service discovery info to the Gateway. The XML would look like this:

<iq id="uLQX1-3" to="gtalk.mtHost" type="get">
	<query xmlns="http://jabber.org/protocol/disco#info"></query>
</iq>

And in return Gateway returns identity information like this:

<iq id="uLQX1-3" to="user1@myHost/Smack" from="gtalk.myHost" type="result">
	<query xmlns="http://jabber.org/protocol/disco#info">
		<identity category="gateway" name="Google Talk Transport" type="xmpp"/>
		<feature var="http://jabber.org/protocol/disco#info"/>
		<feature var="http://jabber.org/protocol/disco#items"/>
		<feature var="jabber:iq:gateway"/>
		<feature var="jabber:iq:register"/>
		<feature var="jabber:iq:version"/>
		<feature var="vcard-temp"/>
	</query>
</iq>

By this you can identify which namespace features are supported by the gateway. To register and/or unregister the gateway “jabber:iq:register” support is required. This response can differ at your side based on your server and Gateway support. Now the code using Smack API for this operation is as follows:

DiscoverInfo iq = new DiscoverInfo();
iq.setType(IQ.Type.GET);
iq.setTo("gtalk.myHost");
PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
connection.sendPacket(iq);
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
System.out.println(response.toXML());

And to check for specific feature is supported or not and getting information of items in response do the following:

if (response != null && !response.getType().equals(IQ.Type.ERROR) && response instanceof DiscoverInfo) {
	DiscoverInfo info = (DiscoverInfo) response;
	Iterator<Identity> identities = info.getIdentities();
	while (identities.hasNext()) {
		DiscoverInfo.Identity identity = (DiscoverInfo.Identity) identities.next();
		System.out.println("identity name -> " + identity.getName());
		System.out.println("identity category -> " + identity.getCategory());
		System.out.println("identity type -> " + identity.getType());
	}
	System.out.println(info.containsFeature("jabber:iq:register"));
}

2. Get information of gateway

To query gateway id about which information is required to register, you need to send following xml:

<iq id="VT12T-4" to="gtalk.myHost" type="get">
	<query xmlns="jabber:iq:register"></query>
</iq>

In response server will respond something like this:

<iq id="VT12T-4" to="user1@myHost/Smack" from="gtalk.myHost" type="result">
	<query xmlns="jabber:iq:register">
		<instructions>Please enter your e-mail address and password used with GMail and GTalk</instructions>
		<username></username>
		<password></password>
		<x xmlns="jabber:x:data" type="form">
			<instructions>Please enter your e-mail address and password used with GMail and GTalk.</instructions>
			<field label="Address" var="username" type="text-single"></field>
			<field label="Password" var="password" type="text-private"></field>
		</x>
		<x xmlns="jabber:iq:gateway:register"></x>
	</query>
</iq>

By this you will know that which fields are required for gateway registration. Here in this example 2 fields are require, one is username and another is password.
Code to do this in smack is:
1) Send GET request to gateway

Registration iq = new Registration();
iq.setType(IQ.Type.GET);
iq.setTo("gtalk.myHost");
connection.sendPacket(iq);

2) Get the response.

PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
System.out.println(response.toXML());

3) Process the response for extracting required information like this:

if (response != null && !response.getType().equals(IQ.Type.ERROR) && response instanceof Registration) {
Registration registration = (Registration) response;
System.out.println(registration.getInstructions());
	if (registration.getAttributes() != null && !registration.getAttributes().isEmpty()) {
		Map<String, String> map = registration.getAttributes();
		System.out.println(map);
	}
}

3. Register gateway:

To register a gateway using one has to send IQ SET with the required attributes:

<iq id="WiISc-2" to="gtalk.myHost" type="set">
	<query xmlns="jabber:iq:register">
		<username>user</username>
		<password>pass</password>
	</query>
</iq>

And as a successful response server will send back following xml:

<iq type='result' from='gtalk.myHost' to=user1@myHost/Smack' id='WiIsc-2'/>

And here is the code to do registration using Smack API:
1) Send an IQ set packet. For jabber:iq:register we will use Registration class from smack library:

Map<String, String> attributes = new HashMap<String, String>();
attributes.put("username", "user");
attributes.put("password", "pass");
Registration registration = new Registration();
registration.setType(IQ.Type.SET);
registration.setTo("aim.myHost");
registration.setAttributes(attributes);
connection.sendPacket(registration);

2) Get the response:

PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(registration.getPacketID()));
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();

4. Retrieve registered gateway information:

To retrieve registered gateway information one has to send an IQ GET packet with “jabber:iq:register” query to the gateway id (i.e. gtalk.myHost). This is similar what we did to get gateway information. In short when you can query gateway for information it will return the information with currently registered information, if any and if there there are no registered information then response is just about the required field names to register.

<iq id="VT12T-4" to="gtalk.myHost" type="get">
	<query xmlns="jabber:iq:register"></query>
</iq>

The gateway returns registration requirements with already registered information like following:

<iq type='result' id='VT12T-4'>
   <query xmlns='jabber:iq:register'>
      <registered/>
      <username>user</username>
      <password>pass</password>
   </query>
</iq>

One can do this thing in Smack API as follows:
1) Send IQ GET packet to gateway:

Registration message = new Registration();
message.setTo("gtalk.myHost");
message.setType(IQ.Type.GET);
connection.sendPacket(message);

2) Get and process the response returned by gateway:

PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(message.getPacketID()));
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response != null && !response.getType().equals(IQ.Type.ERROR) && response instanceof Registration) {
Registration registration = (Registration) response;
if (registration.getAttributes() != null && !registration.getAttributes().isEmpty()) {
Map<String, String> map = registration.getAttributes();
// map contains information about registration.
}
}

5. Edit registered information:

To edit registered information one has to first fetch information as shown in above section and then follow the same code for registering gateway. In simple words do point 3 & 4 form this article in reverse order.

6. Unregister gateway

To unregister a gateway first query for the registered gateway to see if any gateway information is registered or not. If it is registered then to unregister you just have to add a “remove” attribute in the IQ set as follows:

<iq type='set' from=user1@myHost/Smack' to='gtalk.myHost' id='VTiLk-4'>
   <query xmlns='jabber:iq:register'>
      <remove/> <!—remove is for unregisterting the gateway -->
   </query>
</iq>

This is very similar to the register gateway xml so difference in code is very small. Instead of adding username and password as attributes in Map you just add a “remove” attribute and everything else is same.

Remove these 2 lines from register gateway code:

attributes.put("username", "user");
attributes.put("password", "pass");

And add following line instead:

attributes.put("remove", ""); // no need to provide any value as remove tag is an empty tag

Everything else in the code is same. Response is also similar to the register gateway response.

This is it for now!!! 🙂

%d bloggers like this: