@Contract(threading=SAFE_CONDITIONAL) public class SSLIOSession extends java.lang.Object implements IOSession, SessionBufferStatus, SocketAccessor
SSLIOSession is a decorator class intended to transparently extend
an IOSession with transport layer security capabilities based on
the SSL/TLS protocol.
The resultant instance of SSLIOSession must be added to the original
I/O session as an attribute with the SESSION_KEY key.
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, null, null);
SSLIOSession sslsession = new SSLIOSession(
iosession, SSLMode.CLIENT, sslContext, null);
iosession.setAttribute(SSLIOSession.SESSION_KEY, sslsession);
| Modifier and Type | Class and Description |
|---|---|
private class |
SSLIOSession.InternalByteChannel |
| Modifier and Type | Field and Description |
|---|---|
private SessionBufferStatus |
appBufferStatus |
private int |
appEventMask |
private SSLIOSession.InternalByteChannel |
channel |
private static java.nio.ByteBuffer |
EMPTY_BUFFER |
private boolean |
endOfStream |
private SSLSetupHandler |
handler |
private SSLBuffer |
inEncrypted |
private boolean |
initialized |
private SSLBuffer |
inPlain |
private SSLBuffer |
outEncrypted |
private IOSession |
session |
static java.lang.String |
SESSION_KEY
Name of the context attribute key, which can be used to obtain the
SSL session.
|
private javax.net.ssl.SSLEngine |
sslEngine |
private SSLMode |
sslMode |
private int |
status |
ACTIVE, ATTACHMENT_KEY, CLOSED, CLOSING| Constructor and Description |
|---|
SSLIOSession(IOSession session,
SSLMode sslMode,
HttpHost host,
javax.net.ssl.SSLContext sslContext,
SSLSetupHandler handler)
Creates new instance of
SSLIOSession class. |
SSLIOSession(IOSession session,
SSLMode sslMode,
HttpHost host,
javax.net.ssl.SSLContext sslContext,
SSLSetupHandler handler,
SSLBufferManagementStrategy bufferManagementStrategy)
Creates new instance of
SSLIOSession class. |
SSLIOSession(IOSession session,
SSLMode sslMode,
javax.net.ssl.SSLContext sslContext,
SSLSetupHandler handler)
Creates new instance of
SSLIOSession class. |
| Modifier and Type | Method and Description |
|---|---|
java.nio.channels.ByteChannel |
channel()
Returns the underlying I/O channel associated with this session.
|
void |
clearEvent(int op)
Clears interest in a particular I/O event type by updating the event
mask associated with the session.
|
void |
close()
Terminates the session gracefully and closes the underlying I/O channel.
|
private javax.net.ssl.SSLException |
convert(java.lang.RuntimeException ex) |
private boolean |
decryptData() |
private void |
doHandshake() |
private void |
doRunTask() |
private javax.net.ssl.SSLEngineResult |
doUnwrap(java.nio.ByteBuffer src,
java.nio.ByteBuffer dst) |
private javax.net.ssl.SSLEngineResult |
doWrap(java.nio.ByteBuffer src,
java.nio.ByteBuffer dst) |
private static void |
formatOps(java.lang.StringBuilder buffer,
int ops) |
java.lang.Object |
getAttribute(java.lang.String name)
Returns the value of the attribute with the given name.
|
int |
getEventMask()
Returns mask of I/O evens this session declared interest in.
|
java.net.SocketAddress |
getLocalAddress()
Returns local address.
|
java.net.SocketAddress |
getRemoteAddress()
Returns address of the remote peer.
|
java.net.Socket |
getSocket()
Return the underlying socket
|
int |
getSocketTimeout()
Returns value of the socket timeout in milliseconds.
|
javax.net.ssl.SSLSession |
getSSLSession() |
protected SSLSetupHandler |
getSSLSetupHandler() |
int |
getStatus()
Returns status of the session:
|
boolean |
hasBufferedInput()
Determines if the input buffer associated with the session contains data.
|
boolean |
hasBufferedOutput()
Determines if the output buffer associated with the session contains
data.
|
void |
inboundTransport()
Executes inbound SSL transport operations.
|
void |
initialize()
Initializes the session.
|
void |
initialize(SSLMode sslMode)
Deprecated.
(4.3) SSL mode must be set at construction time.
|
boolean |
isAppInputReady()
Reads encrypted data and returns whether the channel associated with
this session has any decrypted inbound data available for reading.
|
boolean |
isAppOutputReady()
Returns whether the channel associated with this session is ready to
accept outbound unecrypted data for writing.
|
boolean |
isClosed()
Determines if the session has been terminated.
|
boolean |
isInboundDone()
Returns whether the session will produce any more inbound data.
|
boolean |
isInitialized()
Returns
true is the session has been fully initialized,
false otherwise. |
boolean |
isOutboundDone()
Returns whether the session will accept any more outbound data.
|
void |
outboundTransport()
Sends encrypted data and executes outbound SSL transport operations.
|
private int |
readPlain(java.nio.ByteBuffer dst) |
private int |
receiveEncryptedData() |
java.lang.Object |
removeAttribute(java.lang.String name)
Removes attribute with the given name.
|
private int |
sendEncryptedData() |
void |
setAttribute(java.lang.String name,
java.lang.Object obj)
This method can be used to associate a particular object with the
session by the given attribute name.
|
void |
setBufferStatus(SessionBufferStatus status)
Quite often I/O sessions need to maintain internal I/O buffers in order
to transform input / output data prior to returning it to the consumer or
writing it to the underlying channel.
|
void |
setEvent(int op)
Declares interest in a particular I/O event type by updating the event
mask associated with the session.
|
void |
setEventMask(int ops)
Declares interest in I/O event notifications by setting the event mask
associated with the session
|
void |
setSocketTimeout(int timeout)
Sets value of the socket timeout in milliseconds.
|
void |
shutdown()
Terminates the session by shutting down the underlying I/O channel.
|
java.lang.String |
toString() |
private void |
updateEventMask() |
private int |
writePlain(java.nio.ByteBuffer src) |
public static final java.lang.String SESSION_KEY
private static final java.nio.ByteBuffer EMPTY_BUFFER
private final IOSession session
private final javax.net.ssl.SSLEngine sslEngine
private final SSLBuffer inEncrypted
private final SSLBuffer outEncrypted
private final SSLBuffer inPlain
private final SSLIOSession.InternalByteChannel channel
private final SSLSetupHandler handler
private int appEventMask
private SessionBufferStatus appBufferStatus
private boolean endOfStream
private volatile SSLMode sslMode
private volatile int status
private volatile boolean initialized
public SSLIOSession(IOSession session, SSLMode sslMode, HttpHost host, javax.net.ssl.SSLContext sslContext, SSLSetupHandler handler)
SSLIOSession class. The instances created uses a
PermanentSSLBufferManagementStrategy to manage its buffers.session - I/O session to be decorated with the TLS/SSL capabilities.sslMode - SSL mode (client or server)host - original host (applicable in client mode only)sslContext - SSL context to use for this I/O session.handler - optional SSL setup handler. May be null.public SSLIOSession(IOSession session, SSLMode sslMode, HttpHost host, javax.net.ssl.SSLContext sslContext, SSLSetupHandler handler, SSLBufferManagementStrategy bufferManagementStrategy)
SSLIOSession class.session - I/O session to be decorated with the TLS/SSL capabilities.sslMode - SSL mode (client or server)host - original host (applicable in client mode only)sslContext - SSL context to use for this I/O session.handler - optional SSL setup handler. May be null.bufferManagementStrategy - buffer management strategypublic SSLIOSession(IOSession session, SSLMode sslMode, javax.net.ssl.SSLContext sslContext, SSLSetupHandler handler)
SSLIOSession class.session - I/O session to be decorated with the TLS/SSL capabilities.sslMode - SSL mode (client or server)sslContext - SSL context to use for this I/O session.handler - optional SSL setup handler. May be null.protected SSLSetupHandler getSSLSetupHandler()
public boolean isInitialized()
true is the session has been fully initialized,
false otherwise.@Deprecated public void initialize(SSLMode sslMode) throws javax.net.ssl.SSLException
SSLMode. This method
invokes the SSLSetupHandler.initalize(SSLEngine) callback
if an instance of SSLSetupHandler was specified at
the construction time.javax.net.ssl.SSLExceptionpublic void initialize()
throws javax.net.ssl.SSLException
SSLSetupHandler.initalize(SSLEngine) callback if an instance of
SSLSetupHandler was specified at the construction time.javax.net.ssl.SSLException - in case of a SSL protocol exception.java.lang.IllegalStateException - if the session has already been initialized.public javax.net.ssl.SSLSession getSSLSession()
private javax.net.ssl.SSLException convert(java.lang.RuntimeException ex)
private javax.net.ssl.SSLEngineResult doWrap(java.nio.ByteBuffer src,
java.nio.ByteBuffer dst)
throws javax.net.ssl.SSLException
javax.net.ssl.SSLExceptionprivate javax.net.ssl.SSLEngineResult doUnwrap(java.nio.ByteBuffer src,
java.nio.ByteBuffer dst)
throws javax.net.ssl.SSLException
javax.net.ssl.SSLExceptionprivate void doRunTask()
throws javax.net.ssl.SSLException
javax.net.ssl.SSLExceptionprivate void doHandshake()
throws javax.net.ssl.SSLException
javax.net.ssl.SSLExceptionprivate void updateEventMask()
private int sendEncryptedData()
throws java.io.IOException
java.io.IOExceptionprivate int receiveEncryptedData()
throws java.io.IOException
java.io.IOExceptionprivate boolean decryptData()
throws javax.net.ssl.SSLException
javax.net.ssl.SSLExceptionpublic boolean isAppInputReady()
throws java.io.IOException
java.io.IOException - in case of an I/O error.public boolean isAppOutputReady()
throws java.io.IOException
java.io.IOException - - not thrown currentlypublic void inboundTransport()
throws java.io.IOException
java.io.IOException - - not thrown currentlypublic void outboundTransport()
throws java.io.IOException
java.io.IOException - in case of an I/O error.public boolean isInboundDone()
public boolean isOutboundDone()
private int writePlain(java.nio.ByteBuffer src)
throws java.io.IOException
java.io.IOExceptionprivate int readPlain(java.nio.ByteBuffer dst)
public void close()
IOSessionpublic void shutdown()
IOSessionpublic int getStatus()
IOSession
IOSession.ACTIVE: session is active.
IOSession.CLOSING: session is being closed.
IOSession.CLOSED: session has been terminated.
public boolean isClosed()
IOSessionpublic java.nio.channels.ByteChannel channel()
IOSessionpublic java.net.SocketAddress getLocalAddress()
IOSessiongetLocalAddress in interface IOSessionpublic java.net.SocketAddress getRemoteAddress()
IOSessiongetRemoteAddress in interface IOSessionpublic int getEventMask()
IOSessiongetEventMask in interface IOSessionpublic void setEventMask(int ops)
IOSessionsetEventMask in interface IOSessionops - new I/O event mask.public void setEvent(int op)
IOSessionpublic void clearEvent(int op)
IOSessionclearEvent in interface IOSessionop - I/O event type.public int getSocketTimeout()
IOSession0 signifies the session cannot time out.getSocketTimeout in interface IOSessionpublic void setSocketTimeout(int timeout)
IOSession0 signifies the session cannot time out.setSocketTimeout in interface IOSessiontimeout - socket timeout.public boolean hasBufferedInput()
IOSessionhasBufferedInput in interface IOSessionhasBufferedInput in interface SessionBufferStatustrue if the session input buffer contains data,
false otherwise.public boolean hasBufferedOutput()
IOSessionhasBufferedOutput in interface IOSessionhasBufferedOutput in interface SessionBufferStatustrue if the session output buffer contains data,
false otherwise.public void setBufferStatus(SessionBufferStatus status)
IOSession
I/O sessions can be made aware of the status of external session buffers
using the SessionBufferStatus interface.
setBufferStatus in interface IOSessionpublic java.lang.Object getAttribute(java.lang.String name)
IOSessionnull if not set.
The value of the session attachment object can be obtained using
IOSession.ATTACHMENT_KEY name.
getAttribute in interface IOSessionname - name of the attribute.IOSession.setAttribute(String, Object)public java.lang.Object removeAttribute(java.lang.String name)
IOSessionremoveAttribute in interface IOSessionname - name of the attribute to be removed.IOSession.setAttribute(String, Object)public void setAttribute(java.lang.String name,
java.lang.Object obj)
IOSessionI/O sessions are not bound to an execution thread, therefore one cannot use the context of the thread to store a session's state. All details about a particular session must be stored within the session itself.
setAttribute in interface IOSessionname - name of the attribute.obj - value of the attribute.private static void formatOps(java.lang.StringBuilder buffer,
int ops)
public java.lang.String toString()
toString in class java.lang.Objectpublic java.net.Socket getSocket()
SocketAccessorgetSocket in interface SocketAccessornull.