Sunday, December 20, 2009

Reading List

I'm going to read these two books in the coming weeks until the next vacation, and going to write any ideas and thoughts during the reading. Hopefully this way can force me to have reading tasks more regularly and more frequently.

Book 1. Learning Python, Mark Lutz and David Ascher, O'Reilly

Book 2. Beginning Unix, Paul Love, Joe Merlino, Jeremy C. Reed, Craig Zimmerman, Paul Weinstein, Wrox

Sunday, December 13, 2009

Get Request Header in Restlet

When using Restlet library to invoke RESTful method, sometimes you need to parse the request header for further use. And luckily, though there is explicit ways to solve this problem, we can still get what we need.

Calling getRequestAttributes().get("org.restlet.http.headers"); will return all the headers in the request. However, the type of returned result is "org.restlet.data.Form". Consequently, we need to convert it into the Form format, and then using the getFirstValue("") method to retrieve the value. One catch there is the key is always start with capital letter, for instant even is "location" in the header, you have to use "Location" instead.

Form headers = (Form) getRequestAttributes().get("org.restlet.http.headers");
location = headers.getFirstValue("Location");

Wednesday, December 9, 2009

Solve the annoyance with Eclipse Access Restriction

Sometimes, you may encounter a problem like this:
is not accessible due to restriction on required library /usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/rt.jar.

After several times checking, there is no reason to properly explain and your program gets to stuck since no compilation is allowed when error existed.

One way to work around this issue is by resetting the configuration in the Eclipse project properties. Right click the project name, get into its properties and the Java compiler tab. There is an Errors/Warnings line listing there, select it, and update the Forbidden Reference (access rules) into Warning in the Deprecated and restricted API. Then rebuild the project, and problem solved.

Similar errors can be handled in the same way.

Monday, December 7, 2009

How to invoke a remote web service in GAE

Well, with GAE it is easy to develop applications on the cloud. But various limitations sometimes make people feel really uncomfortable when their usual behavior being considered as illegal according to the Google Laws.

You are not allowed to use any library related to .net package which may possibly generate more threads. But in order to make any invocation of other RESTful web services, we need to do something to both satisfy the requirement of Google and us.

In the official document, GAE recommends to use the HttpURLConnection class, which is a little bit too simpler to use, which means it brings various problems when dealing with complicated data types.

Here I will invoke a remote RESTful web service which will accept the format of JSON. Here is the code:

try {
URL url = new URL("http://localhost:8183/users/" +user+"/content");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
String json = content.toJSON().toString();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(json);
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("sssssss");
} else {
System.out.println(connection.getResponseMessage());
System.out.println("ffffffff");
}
} catch (Exception e) {
e.printStackTrace();
}

Looks easy, ha, yep, quite straightforward. Remember to add those content-type in the connection for the receiver to recognize the format you are sending. While you are only allowed to send String or Char[], so hurry up and write your own parsing functions.