User offline. Last seen 6 years 8 weeks ago. Offline
Joined: 05/28/2008

I've been working with a couple implementations to request keywords/phrases for content from Calais.

The first implementation is using Axis2.  Axis2 creates the stubs needed to access the service fine.  However, it does not allow me to send the paramsXML as a string in an unescaped format.

The second implementation is using an HTTP POST...

        String paramsXML = null;
        String line = null;

        FileReader fr = new FileReader("C:/paramsXML.xml");
        BufferedReader br = new BufferedReader(fr);

        while((line = br.readLine())!= null){

            paramsXML += line;
            System.out.println(line);
        }

        String post = "licenseID=" + URLEncoder.encode(CALAIS_API_KEY, "UTF-8") + "&content=" + URLEncoder.encode(text, "UTF-8") + "&paramsXML=" + URLEncoder.encode(paramsXML, "UTF-8");

        URL url = new URL(CALAIS_SERVER);
        URLConnection conn = url.openConnection();
        HttpURLConnection connection = (HttpURLConnection)conn;

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty( "Content-Length", String.valueOf( post.length() ) );

        OutputStream out = connection.getOutputStream();
        OutputStreamWriter w = new OutputStreamWriter(out);
        w.write(post);

        w.flush();
        w.close();

        InputStream is = connection.getInputStream();
        br = new BufferedReader(new InputStreamReader(is));

        while((line = br.readLine()) != null) {

            System.out.println(line);
        }

However, getting the inputstream throws an IO exception due to a 500 server error.  The same code works fine calling Yahoo!'s Term Extraction service.

My preferred method for accessing the service is using Axis2.  Any suggestions?  Thanks.

Regards,

Charles

Trackback URL for this post:

http://www.opencalais.com/trackback/1279

Login or Register to post a comment.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
User offline. Last seen 4 years 27 weeks ago. Offline
Joined: 01/20/2010

Hello Guys

I am new to this technology, can you please assist me what is "paramsXML.xml" - how does it looks like? any sample of "paramsXML.xml" should be appreciated.

Thank you
Bandari

User offline. Last seen 3 weeks 4 days ago. Offline
Joined: 04/30/2008

Bandari -

Welcome to OpenCalais.

Take a look at our Showcase (opencalais.com/showcase). If you screen for Code Examples, you'll find some information that should be helpful.

Regards,

User offline. Last seen 4 years 42 weeks ago. Offline
Joined: 06/01/2008

Hi Charles,

I asked Yoel, one of our engineers, to test the Axis2 issue.
He managed to get it to work, but there is a twist: By default, Axis2 uses CHUNKED mode for sending the HTTP request body.
This is not very "popular" with .NET web-apps, so you need to disable it.

I'm pasting Yoel's response, including sample code, below:

==============================================
OK, it’s working but there is a twist,

The twist is that Axis2 works by default in chunk mode, meaning that it sends http data in chunk format while .Net probably don’t like it much and returns an error, so in order 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);

}

}
===========================================

User offline. Last seen 6 years 8 weeks ago. Offline
Joined: 05/28/2008

Meir/Yoel,

Thanks again for looking into this. I'm sure this will be valuable for other Java developers that stumble upon these questions.

Regards,

Charles

User offline. Last seen 4 years 42 weeks ago. Offline
Joined: 06/01/2008

Hi Charles,

The Calais URL is a URL of a .NET web-service, optionally containing *several* web-methods.

Of course, 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)

The way to do it is 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, then appending the above is no good, because the calais web-app 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"

This should do the trick...

Meir

User offline. Last seen 6 years 8 weeks ago. Offline
Joined: 05/28/2008

Meir,

Excellent! that worked. I'm not sure how I overlooked that looking at the example post message, and the fact that we use REST style services for A2A messaging.

In any case, thank you for looking into this.

Regards,

Charles