Azure/azure-storage-java — pull request #28 by ngershaneck (2014-12-02 19:48:10+00:00)
Title: Changes to accomodate setting local outgoing address explicitly

This is a follow up an issue raised by a colleague. See the following:
[Unable to set local interface IP on outgoing Azure connections] (https://github.com/Azure/azure-storage-java/issues/27)
[Binding specific interface IP address to Azure Storage container connections] (http://stackoverflow.com/questions/26802636/binding-specific-interface-ip-address-to-azure-storage-container-connections)

We forked the azure-storage repository in order to incorporate the needed capability to set the local network interface when creating connections. The changes are described below.

New Classes

The internal mechanics of the URLStreamHandler for http and https connections were mirrored up to the point where the socket is created by effectively duplicating the functionality of the internal implementations of HttpURLConnection and HttpsURLConnection within OpenJDK Java 7.

AzureStreamHandlerFactory

Holds a cache of URLStreamHandler instances based on the combination of protocol and local address to be used.

AzureHttpHandler

Extends the internal implementation class sun.net.www.protocol.http.handler.

The modified behavior is to override the openConnection method to return an AzureHttpURLConnection instance.

AzureHttpsHandler

Extends the internal implementation class sun.net.www.protocol.https.handler.

The modified behavior is to override the openConnection method. It delegates to the super class, and then explicitly sets the SSLSocketFactory on the returned connection object before handing it back to the caller. The internal HttpsURLConnection implementation always invokes its SSLSocketFactory to create sockets, so replacing that with a factory that always binds the socket to the correct local address is sufficient to handle HTTPS connections.

AzureHttpURLConnection

Extends the internal implementation class sun.net.www.protocol.http.HttpURLConnection.

Overrides all methods which produce HttpClient instances to produce AzureHttpClient instances instead.

AzureHttpClient

Extends the internal implementation class sun.net.www.http.HttpClient.

Constructor logic was copied from the super class because the constructor calls openServer (which creates a socket), and the local address needs to be set first. Mirrors the functionality of the super class's New methods, except they produce AzureHttpClient instances. Overrides the keep alive cache mechanisms because the variable that drives it is private and cannot be accessed in the subclass. Overrides the createSocket method to look at the local address and bind it to the socket before returning.

Modified Classes

The following describes the changes to existing Azure SDK classes.

RequestOptions

Adds a new option to set the local IP address to be used for client connections. Follows the existing pattern the class uses.

BaseRequest

Modifies the createURLConnection method. When a local address has been set, instead of using the URI.toURL() method, the URL is formed by using the constructor that accepts a URLStreamHandler parameter and invoking the AzureStreamHandlerFactory to produce a handler instance.