Sunday, July 3, 2011

Exploring Google App Engine with Restlet and Objectify

Warning: this post is excessively geeky for my usual audience. Non-programmers, feel free to skip.

I've been doing a little more exploration with Google App Engine. I've used it before, first for my Weasley Clock, and also for one of my classes, Object-Oriented Analysis and Design. In that OOAD class, my team and I had a short amount of time to build an Android app backed by a RESTful web service on GAE. We ended up using JAX-RS/Jersey as a RESTful framework, JAX-B for server-side XML serialization, and JDO for the datastore API. Then on the Android client, we used the Apache HTTP client and SAX parsers. That worked out alright, but recently I've wanted to explore what other options are out there.

I've been interested in Restlet for a while; in OOAD, I explored that first before moving to JAX-RS. It seems to be a pretty comprehensive solution. They provide both server and client libraries for many platforms, with flexible configuration for many needs, such as filters. What turned me off last time, and almost again this time, was that the source code for the "first application" example didn't work out of the box. I had to decide which version of the library I needed, download four different editions, and figure out which .jars to put where. Then for a long while, I was stuck on an interesting bug. Apparently, GAE doesn't support chunked encoding (whatever that means), so their current stable 2.0.8 libraries won't work for GAE/Android communication. Their 2.1m5 libraries have a workaround, but one must manually specify that entities must be buffered (once again, what?). I also paired their 2.0 "first application" example with 2.1m5 libraries without realizing that there was a difference, because their wiki wasn't clear about which version of the sample application you were looking at.

So far, I would say that Restlet is a well-built library for RESTful communication, both server and client-side, but their documentation and examples could really use some organization help. The answers are on their site, but sometimes they seem to be hidden away in dark corners of the wiki.

After I got the Restlet "first application" working, I wanted to bolt on datastore support. I wanted to try something other than JDO, and so looked at JPA. However, these both seem to be cumbersome and ill-fit when it comes to the GAE datastore. Then somehow I stumbled across Objectify, a relatively thin library that works on top of the datastore low-level API. Some of the attractive features were a less-cranky Key interface, using POJOs instead of GAE-specific Entities, and GWT compatibility without DTO's. I did have trouble with that GWT part for a while, because I thought that my POJOs still needed to be annotated with java.persistence.Entity. I think that was causing problems when the DataNucleus Enhancer went and did its magic; the server-side class looked different than what GWT was expecting on the client side, which ended up throwing a SerializationException. However, I finally figured out that stored entities don't need the Entity annotation. The only thing that the POJOs need is a java.persistence.Id annotation on the primary key field.

I have yet to really explore this thoroughly, but so far both libraries seem to be doing their job and playing nicely. The best part was not having to write DTOs! I did have to fight with both libraries for a while, but hopefully this post will help somebody else avoid those pitfalls.

No comments:

Post a Comment