fernandezpablo85/scribe-java — pull request #283 by brettwooldridge (2012-07-06 09:07:17+00:00)
Title: Added asynchronous (NIO) based authentication support.

Hi Fernandez,

First thanks for the excellent library. The code was very easy to understand and work with. I believe my changes adhere to your style correctly.

Let me summarize what I've done. Basically, to increase scalability I added support for asynchronous authentication using the Apache HTTP Components NIO library. This allows a single thread on the server to have hundreds or thousands of in-flight authentications.

You'll notice in the pom.xml there is a new entry:

org.apache.httpcomponents
httpcore-nio
4.2.1
compile

This is a compile-time dependency, but not a run-time dependency. The library can be used as is, without asynchronous support. However, if the Apache HTTP Components (httpcore-nio) library is available, the user has the option of using a new Async flavor of the API. The original API is unchanged, and uses the existing code (HttpURLConnection), even in the presence of the Apache components. It is only if the developer explicitly uses the Async flavor of the API that they will get asynchronous behavior.

I have added a TwitterAsyncExample and FacebookAsyncExample where you can see what it looks like from the end-user perspective. I think the usage is fairly crisp.

Now to the code. I know it looks like I changed a lot of files, but 90% was just moving a few things around to allow for code-sharing. Basically this is the broad strokes:

Request was changed to an interface. The majority of the code (+80%) in the old Request class was pulled into RequestBase, with the remainder moved to RequestSync. Essentially, wrt the original Request that is all that was done. RequestSync and RequestAsync extend RequestBase. By changing Request to an interface, with the same signature as the original class, it was possible to add the two implementation classes without affecting any client code.

OAuthRequest has a new constructor:

public OAuthRequest(Verb verb, String url, boolean async)

which allows creating an asynchronous flavor of an OAuthRequest. OAuthRequest now holds a Request (interface) reference as a member and delegates to it. The Request can obviously be either a RequestSync or RequestAsync implementation. There is some minor reflection to ensure there is no hard dependency on the RequestAsync class (which would pull in Apache requirements).

The ServiceBuilder has a new method:

public OAuthServiceAsync buildAsync();

which returns a asynchronous flavor of the OAuthService.

My suggestion is, before looking at the diffs, just make a local clone of my repository and take a look at the code. I find looking at diffs where classes where split (such as Request into RequestBase and RequestSync) confusing.

I welcome your feedback and suggestions.