Posts

A Simple Java Http Client

I recently needed to make an HTTP call from my AppEngine application, which uses the standard java.net classes for its urlFetch service.  Doing something as simple as an HTTP request in Java is a lot more complicated than one would hope.  Since I didn't find the process intuitive at all, I wrote this little Java HTTP client that works the way I think of HTTP.

Along the way I got to experiment with generics, which allowed me to make chained method calls so I can effectively make the whole HTTP request and get the response in one line of code (sort of).  The whole client consists of three classes and uses only core java classes, so no external libraries are needed (and it can run on AppEngine).

Message.java This class forms the basis for the Request and Response classes.  Message is an abstract class, and contains only those things that are common to both Requests and Responses, e.g. headers and a body.

Request.java This class does most of the heavy lifting.  It contains fields and …

Polymorphism in Objectify

I'm currently learning how to use Objectify with Google AppEngine (GAE).  I was tripped-up while trying to use the polymorphic capabilities of Objectify.  With the patient help of Jeff on the objectify-appengine group, I realized that I was confusing Objectify's polymorphic object hierarchy with my code's inheritance tree.

I put together a demo to illustrate the difference.  Consider an object hierarchy where Vehicle is extended by Car and Motorcycle:

Vehicle.java:@Entity public class Vehicle extends Foundation { @Id private String licensePlate; @Index int numWheels; @Index int numCylinders; public String getLicensePlate() { return licensePlate; } public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } }
Here are the meanings of the annotations on the Vehicle class:

@Entity - Makes the Vehicle class the root of a Objectify polymorphic hierarchy.  An application could have many roots …