Wednesday, May 09, 2012

Extra S3 log parameters with AWS Java SDK

Amazon web services' S3 REST API supports custom access log information by adding custom query string parameters beginning with "x-" that S3 ignores but logs. Unfortunately custom query parameters are not directly supported in the AWS Java SDK, so we were planning to drop down to writing HTTP REST code for a recent project that required logging of some custom client-session parameters.

I hate working with HTTP, so I was glad when we figured out a workaround. The trick we came up with was to register a custom RequestHandler with our instance of the SDK's AmazonS3Client. The RequestHandler is an internal class in the API, so it's not included in the API documentation, but you can download the source code to see its simple interface:

/**
 * Interface for addition request handling in clients. A request handler is
 * executed on a request object before it is sent to the client runtime
 * to be executed.
 */
public interface RequestHandler {

    /**
     * Runs any additional processing logic on the specified request (before it
     * is executed by the client runtime).
     *
     * @param request
     *            The low level request being processed.
     */
    public void beforeRequest(Request request);

 /**
  * Runs any additional processing logic on the specified request (after is
  * has been executed by the client runtime).
  *
  * @param request
  *            The low level request being processed.
  * @param response
  *            The response generated from the specified request.
  * @param timingInfo
  *            Timing information on the request's processing.
  */
    public void afterResponse(Request request, Object response, TimingInfo timingInfo);

 /**
  * Runs any additional processing logic on a request after it has failed.
  *
  * @param request
  *            The request that generated an error.
  * @param e
  *            The error that resulted from executing the request.
  */
    public void afterError(Request request, Exception e);

}

So the RequestHandler's beforeRequest method can add parameters to a Request before it is processed by the underlying HTTP engine. The only trick is to determine which parameters to add to a particular Request, but it's easy to extend the SDK's API-level request objects (like S3's GetObjectRequest) with subtypes that add application-specific properties, then check for for those subtypes in the RequestHandler via the getOriginalRequest accessor.

Oracle Google Comments

For some reason I was inspired today to post a comment on the NYTimes Bits bLog's post on the Oracle-Google lawsuit. I thought I didn't make it through the Time's idiot filter, but they actually put my comment online. Crazy. Here it is again:


I think Google got itself into trouble, because rather than simply build on top of java (write code with the java language and libraries), and contribute to the java community; Google decided to "fork" java to make its own language that is nearly identical to java, but with various differences (security model, libraries, bytecode) at a time (pre-openJDK) that java was not open sourced. Often non-UI code (networking, database, ...) written in java can be easily recompiled for Android's "Dalvik" runtime, so Google gained instant access to the java developer community and programming tools; but the two systems are not the same.

There's something slimy about what Google did - forking java for Android without respecting the time and money Sun invested. Sun Micro sued Microsoft years ago when Microsoft released a version of java with incompatibilities to improve java on windows. Google was very careful to never call Android's runtime "java" to avoid the issues Microsoft ran into, but everybody thinks of Android as running java. Google should have just bought Sun Micro when it had the chance ...

Oracle is slimy too for trying to insinuate itself into the Android smart phone and tablet market. Oracle and Sun didn't have the vision of Apple, or the skill to imitate Apple quickly like Google; so now Oracle is trying to sue its way into the market.

Anyway - the best outcome when two corporate monsters fight is that they both get cut up and bleed ... ;-)