Code Sample for Calling Calais using POST or Axis2 Official Application

Submitted by admin on February 20, 2009 - 08:19

 

Thanks to Charles Goddard for asking some questions and providing the impetus for this Axis2 code. 

Notes:

The Calais URL is a URL of a .NET web-service, optionally containing *several* web-methods. Currently, it only contains a single web-method, named "Enlighten"; still, you must specify the method name in your POST request. (Otherwise, the server has no clue which web-method you are trying to invoke).

You can do this simply by appending "/Enlighten" to the URL, but this is where things become a little tricky: If you are using the "http://api.opencalais.com/enlighten/" URL, appending the above will not work, because Calais has a default page named "calais.asmx", and this shorter URL actually stands for the full form of the URL: http://api.opencalais.com/enlighten/calais.asmx.

So, for the POST purpose, you must use this longer form: you should add the method name, and use the following URL for the POST: http://api.opencalais.com/enlighten/calais.asmx/Enlighten.

By default Axis2 uses CHUNKED mode for sending the HTTP request body.This is not very "popular" with .NET web applications, which return an error, so you need to disable it. For it to work, the Axis stub should be set
programmatically (maybe there is another way) to work in non-chunked mode – see DISABLING_CHUNK_MODE
below.

Example client that works:

public class CalaisClient {
private final CalaisStub calaisStub;
public CalaisClient(String serviceURL) {
try {
calaisStub = new CalaisStub(serviceURL);
calaisStub._getServiceClient().getOptions().setTimeOutInMilliSeconds(180000);
//

DISABLING_CHUNK_MODE:

calaisStub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED,  Boolean.FALSE);
} catch (AxisFault e) {
throw new RuntimeException(
"Failed creating a connection to tags service in url: " +
serviceURL, e);
}
}
public String enlighten(String licenseId, String content, String  paramsXML)
throws RemoteException {
CalaisStub.Enlighten params = new CalaisStub.Enlighten();
params.setLicenseID(licenseId);
params.setContent(content);
params.setParamsXML(paramsXML);
CalaisStub.EnlightenResponse response = calaisStub.Enlighten(params);
return response.getEnlightenResult();
}
public static final void main(String[] args) throws RemoteException {
CalaisClient client = new CalaisClient(
"http://api.opencalais.com/enlighten/calais.asmx?wsdl");
String paramsXML = "
   ";
String result = client.enlighten("junkLicense-USEYOUROWN!",
"Hello USA from UK", paramsXML);
System.out.println("Result is:");
System.out.println(result);
}
}