Using the Facebook Graph API from your GWT application

It might happen that you have a GWT application and you want to use the Graph API from Facebook.
As the Graph API requires an authentication with OAuth2 this task can be easily accomplished using the GWT-OAuth2 library.
To simplify things even more I build the following utility class for you:

After setting the unique ID of your facebook app in the constant FACEBOOK_CLIENT_ID, you can easily use the Graph API by calling the doGraph method.
Here is an example that posts a message on the feed of the app user by sending some data to the me/feed path:

As you notice the variables message and url need to be defined with values of your choice before. Now have fun trying other methods of the Graph API.
If you need further permissions, you have to add them as parameters to the call of withScopes inside of the doAuth method.

Using Google App Engine as Backend for Android

If you’re looking for a way to create a backend for your Android application, Google App Engine looks like the perfect choice: You can use Java as you can do for Android and you don’t need to think too much about hosting, as it is all stored in the cloud.

Another benefit is that you can reuse your transfer objects on the client and on server side. But as it is often there are some problems doing this in practice. So you don’t have the same ones I had, I am glad to share my experiences with you.

So first question is what libraries are to use for the client/server communication. At start I tried Restlet 2.0. Looked like a great choice as there are special editions for App Engine and for Android available. In practice it is not very useful as the libraries are to big for Android and I also very much disliked that fully serialized java objects are transfered.

Best approach I found so far is to use Jersey 1.6: It is easy to use and implements the JAX-RS (JSR 311) standard. To set it up on the App Engine, please consult these blog posts from me: Using real POJOs (without JAXB Annotations) as transfer objects with JAX-RS and Storing large images RESTful in the cloud using Google App Engine.

Ok, so far about the server side. To keep things small and simple on the Android side, I mainly created the following wrapper class for the HttpClient to handle the HTTP requests:

This implementation is far from perfect, especially exception handling and passing parameters need to be improved, but it works so far :)

Using this class it is easy to store a file using the FileServerResource from my former blog post. Just call:

Also it is easy to store a transfer object using the doPut method. Note that is is using the ObjectMapper class from Jackson, the same JSON processor that is also used by Jersey.
Jackson is therefore the only additional library that you need on the Android side which keeps the executable small. If you use the same version of Jackson on the client side as on the server side you’re also ensured that the (un-)marshalling process of your transfer objects works flawlessly on both sides.

Hope you liked this approach – feel free to discuss here further ideas.

Storing large images RESTful in the cloud using Google App Engine

In my last article I showed how to store files in the cloud using Google app engine.
Problem there was that the maximum size of the files was 1MB. Not that much for images.

To improve the situation, we just shrink the images with this very simple algorithm by factor 0.9 until the size is less than 1MB:

This FileTransformer class is then just called in the storeFile method of the FileServerResource in the case the file size is large than 1MB:

Remark: So far this only works with JPEG media types, otherwise an exception is raised. Feel free to add different compression cases for other media types (e.g. using GZIP on texts). As I said, it is a good idea to store
the media type 😉

Storing files RESTful in the cloud using Google App Engine

Do you want to store files RESTful in the cloud? Why not use the Google App Engine for it?

Firstly you will need a entity class that is storing the file in the data store:

Beside the actual content of the file, this entity also stores the media type (you never know, it might be good to know…).
As I just like standards, you also might mention that I used JPA as much as possible (Blob is a Google specific thing, as unfortunately byte[] can not store large amounts. BTW: Blob is also limited by 1MB, so you can not store larger files than 1MB using this approach).

Secondly you will need a JAX-RS resource class that is handling the GET and PUT requests from clients:

At last you just need a small helper class ( that is handling the transactions and of course an JAX-RS implementation (I used Jersey 1.6 – works perfectly with App Engine).

To test the file store, just call:
curl --upload-file mypic.jpg -H "Content-Type: image/jpeg" http://localhost:8888/rest/file/store

This request returns the URL of the file that is stored. Just request this URL to retrieve your file back from the storage.

Have fun and let me know, if you are using this code for some of your projects.

Using real POJOs (without JAXB Annotations) as transfer objects with JAX-RS

Are you annoyed that you have to annotate your POJOs with @XmlRootElement, so they can be used with JAX-RS? If your using Jersey as JAX-RS implementation your lucky: Just add to the <servlet> tag in your web.xml the following snippet:

After restarting your servlet, your POJOs are marshalled to JSON as a charme. Enjoy!