Mytec
SSL client implementation in Grizzly.
When implemented SSL client support for Grizzly, it was first time I used SSLEngine in practice. Want to say that it took several
days until I had more or less complete understanding how it should be implemented - first of all handshake phase and the bunch of
related SSLEngine states.
I tried to hide all SSLEngine operations and propose developer to use just common Grizzly ConnectorHandler
methods like: connect, read, write, close. One highlevel method was added: handshake. All enlisted methods could be
used both in blocking/non-blocking manner.
SSLConnectorHandler requires special kind of CallbackHandler - SSLCallbackHandler, which additionally has
onHandshake(IOEvent) callback method. This method will be called when non-blocking handshake operation will be completed.
How to configure SSLConnectorHandler... SSLConnectorHandler could be used with default SSL configuration, it means
SSL artifacts (keystore, truststore) will be loaded according to System properties. However you can set custom SSL
configuration, using either SSLConnectorHandler.configure(SSLConfig) or SSLConnectorHandler.setSSLContext(SSLContext).
New SSL configuration will become active after SSLConnectorHandler will be (re)connected.
Here is small code fragment showing simple usecase for SSLConnectorHandler (blocking mode):
// create new standalone SSLConnectorHandler instance
final SSLConnectorHandler sslConnector = new SSLConnectorHandler();
// initialize buffers
final ByteBuffer sendBuffer = ByteBuffer.wrap("sending data".getBytes());
final ByteBuffer receiveBuffer = ByteBuffer.allocate(256);
try {
// Step #1: Connect
sslConnector.connect(new InetSocketAddress(HOST, PORT));
assert sslConnector.isConnected();
// Step #2: Handshake
assert sslConnector.handshake(receiveBuffer, true);
// Step #3: Write some data
sslConnector.write(sendBuffer, true);
// Step #4: Read response
sslConnector.read(receiveBuffer, true);
receiveBuffer.flip();
System.out.println("Response length: " + receiveBuffer.remaining());
} catch (IOException e) {
// log exception
}
Non blocking implementation is more difficult, but lets you build more scalable applications.
Here is the example of Grizzly non-blocking SSL client implementation.
PS: To be able to run samples - you need keystore and trust store files (info...)
Posted at 03:06PM Jul 16, 2007 by oleksiys in Grizzly | Comments[2]
Posted by Craig Baker on July 17, 2007 at 01:55 PM CEST #
I'm interested in streaming data to the client -- does this work for applets behind firewalls?
Posted by john on September 06, 2007 at 07:19 PM CEST #