Java Client for REST SPI

Starting from WPS 6.1 you can access the Portal SPI model even from outside the portal JVM. The SPI is accessed using REST API. Lets assume that you want to find out the portlet model of Login Portlet. You can do that by writing a simple Java Client like this.

I am using Apache Abdera library to access the ATOM feed and print the response out on System.out


import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Feed;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.commons.httpclient.UsernamePasswordCredentials;

public class RESTSPIClient {
private static final String REALM_URL = "http://localhost:10040";
private static final String CONTENTHANDER_URL = REALM_URL +"/wps/mycontenthandler";
private static final String LOGIN_USERNAME ="wasadmin";
private static final String LOGIN_PASSWORD ="wasadmin";

public static void main(String[] args) {
try {
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
UsernamePasswordCredentials credential = new UsernamePasswordCredentials(LOGIN_USERNAME, LOGIN_PASSWORD);
client.addCredentials(REALM_URL, null, null,credential);
ClientResponse resp = client.get(CONTENTHANDER_URL+"?uri=pm:oid:wps.p.Login");
if (resp.getType() == ResponseType.SUCCESS) {
Document response = resp.getDocument();
Feed feed = response.getRoot();
abdera.getWriterFactory().getWriter("prettyxml").writeTo(feed, System.out);
} else {
System.out.println("Error in response " + resp.getStatusText());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}


As you can see first you should login into the portal using the BASIC authentication client.addCredentials(REALM_URL, null, null,credential);. After that make a HTTP GET request to http://localhost:10040/wps/mycontenthandler?uri=pm:oid:wps.p.Login URL. Look at the last part of the uri which is uri=pm:oid:wps.p.Login. In this pm indicates that this request is for portlet mode. and the wps.p.Login is the unique name of the portlet.

After making the request i am checking if the response status was successful, if yes you can simply read the feed and pretty print it out console using abdera utilities

One important thing you will need to add quite a lot of .jar files in class path to get this sample working. If not you will get few difficult to understand exceptions like NullPointerException because it was not able to create abdera parser


3 comments:

Apps said...

Hi,
Thank you very much for providing lot of information in your blog. This indeed was very helpful for us for debugging couple of server issues.
I've a question regarding the REST SPI. In WPS 6.1 we create a uri to content handler servlet and get the ATOM Feed. How do we convert that to normal Html? For example I need to get the markup of login page, I created a link to the Portlet mode and got the atom feed. Now how do I convert that to HTML. I tried XSLT, but was not successful. I you have done it, will it be possible to publish it in your blog?

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer