org.norther.tammi.acorn.nio
Class SSLSocketChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.SelectableChannel
          extended by java.nio.channels.spi.AbstractSelectableChannel
              extended by java.nio.channels.SocketChannel
                  extended by org.norther.tammi.acorn.nio.SSLSocketChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel, AdaptableChannel, SecureChannel

public class SSLSocketChannel
extends SocketChannel
implements AdaptableChannel, SecureChannel

A secure socket channel implementation adding SSL engine based cryprography to an adapted non-secure concrete SocketChannel implementation.

This implementation extends abstract SocketChannel and forwards applicable calls to methods of the adapted concrete implementation. It also implements AdaptableChannel as selectors typically don't accept channel implementations from other vendors, so the selector registration must be done with the adaptee channel.

The additional SecureChannel methods help handshake and shutdown even though they can also be handled by the read, write and close methods. Note that the handshake method performs only one such channel read or write operation during each call that is enabled by the ready set parameter. Any other way of action seems to cause browsers to block occasionally.

Version:
$Id: SSLSocketChannel.java,v 1.9 2010-07-26 19:31:45 cvsimp Exp $
Author:
Ilkka Priha

Constructor Summary
SSLSocketChannel(SocketChannel channel, SSLEngine engine)
          Construct a new channel.
 
Method Summary
 boolean connect(SocketAddress remote)
           
 int decrypted()
          Returns the number of decrypted bytes not yet read.
 int encrypted()
          Returns the number of encrypted bytes not yet flushed.
 boolean finishConnect()
           
 boolean finished()
          Returns true if this channel is finished with handshaking.
 void flush()
          Flushes remaining encrypted bytes if any.
 Channel getAdapteeChannel()
          Gets the adaptee of this adaptable channel.
 SSLSession getSession()
          Gets the SSL session.
 int handshake(int ops)
          Starts or continues handshaking with the specified operations.
protected  void implCloseSelectableChannel()
           
protected  void implConfigureBlocking(boolean block)
           
 boolean isConnected()
           
 boolean isConnectionPending()
           
 int read(ByteBuffer dst)
           
 long read(ByteBuffer[] dsts, int offset, int length)
           
 boolean shutdown()
          Starts the shutdown sequence but does not close the channel.
 Socket socket()
           
 String toString()
           
 int write(ByteBuffer src)
           
 long write(ByteBuffer[] srcs, int offset, int length)
           
 
Methods inherited from class java.nio.channels.SocketChannel
open, open, read, validOps, write
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel
blockingLock, configureBlocking, implCloseChannel, isBlocking, isRegistered, keyFor, provider, register
 
Methods inherited from class java.nio.channels.SelectableChannel
register
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Constructor Detail

SSLSocketChannel

public SSLSocketChannel(SocketChannel channel,
                        SSLEngine engine)
Construct a new channel.

Parameters:
channel - the unsecure socket channel.
engine - the SSL engine.
Method Detail

socket

public Socket socket()
Specified by:
socket in class SocketChannel

isConnected

public boolean isConnected()
Specified by:
isConnected in class SocketChannel

isConnectionPending

public boolean isConnectionPending()
Specified by:
isConnectionPending in class SocketChannel

connect

public boolean connect(SocketAddress remote)
                throws IOException
Specified by:
connect in class SocketChannel
Throws:
IOException

finishConnect

public boolean finishConnect()
                      throws IOException
Specified by:
finishConnect in class SocketChannel
Throws:
IOException

read

public int read(ByteBuffer dst)
         throws IOException
Specified by:
read in interface ReadableByteChannel
Specified by:
read in class SocketChannel
Throws:
IOException

read

public long read(ByteBuffer[] dsts,
                 int offset,
                 int length)
          throws IOException
Specified by:
read in interface ScatteringByteChannel
Specified by:
read in class SocketChannel
Throws:
IOException

write

public int write(ByteBuffer src)
          throws IOException
Specified by:
write in interface WritableByteChannel
Specified by:
write in class SocketChannel
Throws:
IOException

write

public long write(ByteBuffer[] srcs,
                  int offset,
                  int length)
           throws IOException
Specified by:
write in interface GatheringByteChannel
Specified by:
write in class SocketChannel
Throws:
IOException

getAdapteeChannel

public Channel getAdapteeChannel()
Description copied from interface: AdaptableChannel
Gets the adaptee of this adaptable channel.

Specified by:
getAdapteeChannel in interface AdaptableChannel
Returns:
the adaptee channel.

finished

public boolean finished()
Description copied from interface: SecureChannel
Returns true if this channel is finished with handshaking.

Specified by:
finished in interface SecureChannel
Returns:
true if finished, false otherwise.

encrypted

public int encrypted()
Description copied from interface: SecureChannel
Returns the number of encrypted bytes not yet flushed.

Specified by:
encrypted in interface SecureChannel
Returns:
the number of encrypted bytes.

decrypted

public int decrypted()
Description copied from interface: SecureChannel
Returns the number of decrypted bytes not yet read.

Specified by:
decrypted in interface SecureChannel
Returns:
the number of decrypted bytes.

handshake

public int handshake(int ops)
              throws IOException
Description copied from interface: SecureChannel
Starts or continues handshaking with the specified operations.

Specified by:
handshake in interface SecureChannel
Parameters:
ops - the current ready operations set.
Returns:
the interest set to continue or 0 if finished.
Throws:
IOException - on I/O errors.

shutdown

public boolean shutdown()
                 throws IOException
Description copied from interface: SecureChannel
Starts the shutdown sequence but does not close the channel.

Specified by:
shutdown in interface SecureChannel
Returns:
true if finished, false otherwise.
Throws:
IOException - on I/O errors.

flush

public void flush()
           throws IOException
Description copied from interface: SecureChannel
Flushes remaining encrypted bytes if any.

Specified by:
flush in interface SecureChannel
Throws:
IOException - on I/O errors.

toString

public String toString()
Overrides:
toString in class Object

getSession

public SSLSession getSession()
Gets the SSL session.

Returns:
the session.

implCloseSelectableChannel

protected void implCloseSelectableChannel()
                                   throws IOException
Specified by:
implCloseSelectableChannel in class AbstractSelectableChannel
Throws:
IOException

implConfigureBlocking

protected void implConfigureBlocking(boolean block)
                              throws IOException
Specified by:
implConfigureBlocking in class AbstractSelectableChannel
Throws:
IOException


Copyright © 2004 The Norther Organization. All rights reserved.