Package org.apache.hc.core5.reactor.ssl
Class SSLIOSession
- java.lang.Object
-
- org.apache.hc.core5.reactor.ssl.SSLIOSession
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.WritableByteChannel
,SocketModalCloseable
,ModalCloseable
,IOSession
,Identifiable
@Contract(threading=SAFE_CONDITIONAL) @Internal public class SSLIOSession extends java.lang.Object implements IOSession
SSLIOSession
is a decorator class intended to transparently extend anIOSession
with transport layer security capabilities based on the SSL/TLS protocol.- Since:
- 4.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
SSLIOSession.TLSHandShakeState
-
Nested classes/interfaces inherited from interface org.apache.hc.core5.reactor.IOSession
IOSession.Status
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description SSLIOSession(NamedEndpoint targetEndpoint, IOSession session, SSLMode sslMode, javax.net.ssl.SSLContext sslContext, SSLBufferMode sslBufferMode, SSLSessionInitializer initializer, SSLSessionVerifier verifier, Callback<SSLIOSession> sessionStartCallback, Callback<SSLIOSession> sessionEndCallback, Timeout connectTimeout)
Creates new instance ofSSLIOSession
class.SSLIOSession(NamedEndpoint targetEndpoint, IOSession session, SSLMode sslMode, javax.net.ssl.SSLContext sslContext, SSLBufferMode sslBufferMode, SSLSessionInitializer initializer, SSLSessionVerifier verifier, Timeout handshakeTimeout, Callback<SSLIOSession> sessionStartCallback, Callback<SSLIOSession> sessionEndCallback, FutureCallback<javax.net.ssl.SSLSession> resultCallback)
Creates new instance ofSSLIOSession
class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginHandshake(IOSession protocolSession)
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.void
close(CloseMode closeMode)
Closes this process or endpoint and releases any system resources associated with it.private javax.net.ssl.SSLException
convert(java.lang.RuntimeException ex)
private void
decryptData(IOSession protocolSession)
private void
doHandshake(IOSession protocolSession)
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 void
encryptData(IOSession protocolSession)
void
enqueue(Command command, Command.Priority priority)
InsertsCommand
at the end of the command queue.private IOEventHandler
ensureHandler()
private static void
formatOps(java.lang.StringBuilder buffer, int ops)
int
getEventMask()
Returns mask of I/O evens this session declared interest in.IOEventHandler
getHandler()
Returns event handler associated with the session.java.lang.String
getId()
long
getLastEventTime()
Returns timestamp of the last I/O event including socket timeout reset.long
getLastReadTime()
Returns timestamp of the last read event.long
getLastWriteTime()
Returns timestamp of the last write event.java.net.SocketAddress
getLocalAddress()
Returns local address.java.util.concurrent.locks.Lock
getLock()
Returns session lock that should be used by I/O event handlers to synchronize access to the session.java.net.SocketAddress
getRemoteAddress()
Returns address of the remote peer.Timeout
getSocketTimeout()
Returns value of the socket timeout in milliseconds.IOSession.Status
getStatus()
Returns status of the session:TlsDetails
getTlsDetails()
boolean
hasCommands()
Tests if there enqueued commands pending execution.private void
initialize(IOSession protocolSession)
boolean
isOpen()
Command
poll()
Removes firstCommand
from the command queue if available.int
read(java.nio.ByteBuffer dst)
private int
receiveEncryptedData()
private int
sendEncryptedData()
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 sessionvoid
setSocketTimeout(Timeout timeout)
Sets value of the socket timeout in milliseconds.java.lang.String
toString()
private void
updateEventMask()
void
updateReadTime()
Updates the timestamp of the last read eventvoid
updateWriteTime()
Updates the timestamp of the last write eventvoid
upgrade(IOEventHandler handler)
Upgrades event handler associated with the session.int
write(java.nio.ByteBuffer src)
-
-
-
Field Detail
-
EMPTY_BUFFER
private static final java.nio.ByteBuffer EMPTY_BUFFER
-
targetEndpoint
private final NamedEndpoint targetEndpoint
-
session
private final IOSession session
-
sslEngine
private final javax.net.ssl.SSLEngine sslEngine
-
inEncrypted
private final SSLManagedBuffer inEncrypted
-
outEncrypted
private final SSLManagedBuffer outEncrypted
-
inPlain
private final SSLManagedBuffer inPlain
-
initializer
private final SSLSessionInitializer initializer
-
verifier
private final SSLSessionVerifier verifier
-
sessionStartCallback
private final Callback<SSLIOSession> sessionStartCallback
-
sessionEndCallback
private final Callback<SSLIOSession> sessionEndCallback
-
handshakeCallbackRef
private final java.util.concurrent.atomic.AtomicReference<FutureCallback<javax.net.ssl.SSLSession>> handshakeCallbackRef
-
handshakeTimeout
private final Timeout handshakeTimeout
-
sslMode
private final SSLMode sslMode
-
outboundClosedCount
private final java.util.concurrent.atomic.AtomicInteger outboundClosedCount
-
handshakeStateRef
private final java.util.concurrent.atomic.AtomicReference<SSLIOSession.TLSHandShakeState> handshakeStateRef
-
internalEventHandler
private final IOEventHandler internalEventHandler
-
appEventMask
private int appEventMask
-
endOfStream
private volatile boolean endOfStream
-
status
private volatile IOSession.Status status
-
socketTimeout
private volatile Timeout socketTimeout
-
tlsDetails
private volatile TlsDetails tlsDetails
-
-
Constructor Detail
-
SSLIOSession
public SSLIOSession(NamedEndpoint targetEndpoint, IOSession session, SSLMode sslMode, javax.net.ssl.SSLContext sslContext, SSLBufferMode sslBufferMode, SSLSessionInitializer initializer, SSLSessionVerifier verifier, Callback<SSLIOSession> sessionStartCallback, Callback<SSLIOSession> sessionEndCallback, Timeout connectTimeout)
Creates new instance ofSSLIOSession
class.- Parameters:
session
- I/O session to be decorated with the TLS/SSL capabilities.sslMode
- SSL mode (client or server)targetEndpoint
- target endpoint (applicable in client mode only). May benull
.sslContext
- SSL context to use for this I/O session.sslBufferMode
- buffer management modeinitializer
- optional SSL session initializer. May benull
.verifier
- optional SSL session verifier. May benull
.connectTimeout
- timeout to apply for the TLS/SSL handshake. May benull
.- Since:
- 5.0
-
SSLIOSession
public SSLIOSession(NamedEndpoint targetEndpoint, IOSession session, SSLMode sslMode, javax.net.ssl.SSLContext sslContext, SSLBufferMode sslBufferMode, SSLSessionInitializer initializer, SSLSessionVerifier verifier, Timeout handshakeTimeout, Callback<SSLIOSession> sessionStartCallback, Callback<SSLIOSession> sessionEndCallback, FutureCallback<javax.net.ssl.SSLSession> resultCallback)
Creates new instance ofSSLIOSession
class.- Parameters:
session
- I/O session to be decorated with the TLS/SSL capabilities.sslMode
- SSL mode (client or server)targetEndpoint
- target endpoint (applicable in client mode only). May benull
.sslContext
- SSL context to use for this I/O session.sslBufferMode
- buffer management modeinitializer
- optional SSL session initializer. May benull
.verifier
- optional SSL session verifier. May benull
.handshakeTimeout
- timeout to apply for the TLS/SSL handshake. May benull
.resultCallback
- result callback. May benull
.- Since:
- 5.2
-
-
Method Detail
-
ensureHandler
private IOEventHandler ensureHandler()
-
getHandler
public IOEventHandler getHandler()
Description copied from interface:IOSession
Returns event handler associated with the session.- Specified by:
getHandler
in interfaceIOSession
-
beginHandshake
public void beginHandshake(IOSession protocolSession) throws java.io.IOException
- Throws:
java.io.IOException
-
initialize
private void initialize(IOSession protocolSession) throws java.io.IOException
- Throws:
java.io.IOException
-
convert
private javax.net.ssl.SSLException convert(java.lang.RuntimeException ex)
-
doWrap
private javax.net.ssl.SSLEngineResult doWrap(java.nio.ByteBuffer src, java.nio.ByteBuffer dst) throws javax.net.ssl.SSLException
- Throws:
javax.net.ssl.SSLException
-
doUnwrap
private javax.net.ssl.SSLEngineResult doUnwrap(java.nio.ByteBuffer src, java.nio.ByteBuffer dst) throws javax.net.ssl.SSLException
- Throws:
javax.net.ssl.SSLException
-
doRunTask
private void doRunTask()
-
doHandshake
private void doHandshake(IOSession protocolSession) throws java.io.IOException
- Throws:
java.io.IOException
-
updateEventMask
private void updateEventMask()
-
sendEncryptedData
private int sendEncryptedData() throws java.io.IOException
- Throws:
java.io.IOException
-
receiveEncryptedData
private int receiveEncryptedData() throws java.io.IOException
- Throws:
java.io.IOException
-
decryptData
private void decryptData(IOSession protocolSession) throws java.io.IOException
- Throws:
java.io.IOException
-
encryptData
private void encryptData(IOSession protocolSession) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst)
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
-
getId
public java.lang.String getId()
- Specified by:
getId
in interfaceIdentifiable
-
getLock
public java.util.concurrent.locks.Lock getLock()
Description copied from interface:IOSession
Returns session lock that should be used by I/O event handlers to synchronize access to the session.
-
upgrade
public void upgrade(IOEventHandler handler)
Description copied from interface:IOSession
Upgrades event handler associated with the session.
-
getTlsDetails
public TlsDetails getTlsDetails()
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
close
public void close()
Description copied from interface:IOSession
Terminates the session gracefully and closes the underlying I/O channel. This method ensures that session termination handshake, such as the one used by the SSL/TLS protocol, is correctly carried out.
-
close
public void close(CloseMode closeMode)
Description copied from interface:ModalCloseable
Closes this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.- Specified by:
close
in interfaceModalCloseable
- Parameters:
closeMode
- How to close the receiver.
-
getStatus
public IOSession.Status getStatus()
Description copied from interface:IOSession
Returns status of the session:IOSession.Status.ACTIVE
: session is active.IOSession.Status.CLOSING
: session is being closed.IOSession.Status.CLOSED
: session has been terminated.
-
enqueue
public void enqueue(Command command, Command.Priority priority)
Description copied from interface:IOSession
InsertsCommand
at the end of the command queue.
-
hasCommands
public boolean hasCommands()
Description copied from interface:IOSession
Tests if there enqueued commands pending execution.- Specified by:
hasCommands
in interfaceIOSession
-
poll
public Command poll()
Description copied from interface:IOSession
Removes firstCommand
from the command queue if available.
-
channel
public java.nio.channels.ByteChannel channel()
Description copied from interface:IOSession
Returns the underlying I/O channel associated with this session.
-
getLocalAddress
public java.net.SocketAddress getLocalAddress()
Description copied from interface:IOSession
Returns local address.- Specified by:
getLocalAddress
in interfaceIOSession
- Returns:
- socket address.
-
getRemoteAddress
public java.net.SocketAddress getRemoteAddress()
Description copied from interface:IOSession
Returns address of the remote peer.- Specified by:
getRemoteAddress
in interfaceIOSession
- Returns:
- socket address.
-
getEventMask
public int getEventMask()
Description copied from interface:IOSession
Returns mask of I/O evens this session declared interest in.- Specified by:
getEventMask
in interfaceIOSession
- Returns:
- I/O event mask.
-
setEventMask
public void setEventMask(int ops)
Description copied from interface:IOSession
Declares interest in I/O event notifications by setting the event mask associated with the session- Specified by:
setEventMask
in interfaceIOSession
- Parameters:
ops
- new I/O event mask.
-
setEvent
public void setEvent(int op)
Description copied from interface:IOSession
Declares interest in a particular I/O event type by updating the event mask associated with the session.
-
clearEvent
public void clearEvent(int op)
Description copied from interface:IOSession
Clears interest in a particular I/O event type by updating the event mask associated with the session.- Specified by:
clearEvent
in interfaceIOSession
- Parameters:
op
- I/O event type.
-
getSocketTimeout
public Timeout getSocketTimeout()
Description copied from interface:IOSession
Returns value of the socket timeout in milliseconds. The value of0
signifies the session cannot time out.- Specified by:
getSocketTimeout
in interfaceIOSession
- Specified by:
getSocketTimeout
in interfaceSocketModalCloseable
- Returns:
- socket timeout.
-
setSocketTimeout
public void setSocketTimeout(Timeout timeout)
Description copied from interface:IOSession
Sets value of the socket timeout in milliseconds. The value of0
signifies the session cannot time out.Please note this operation may affect the last event time.
- Specified by:
setSocketTimeout
in interfaceIOSession
- Specified by:
setSocketTimeout
in interfaceSocketModalCloseable
- Parameters:
timeout
- socket timeout.- See Also:
IOSession.getLastEventTime()
-
updateReadTime
public void updateReadTime()
Description copied from interface:IOSession
Updates the timestamp of the last read event- Specified by:
updateReadTime
in interfaceIOSession
-
updateWriteTime
public void updateWriteTime()
Description copied from interface:IOSession
Updates the timestamp of the last write event- Specified by:
updateWriteTime
in interfaceIOSession
-
getLastReadTime
public long getLastReadTime()
Description copied from interface:IOSession
Returns timestamp of the last read event.- Specified by:
getLastReadTime
in interfaceIOSession
- Returns:
- timestamp.
-
getLastWriteTime
public long getLastWriteTime()
Description copied from interface:IOSession
Returns timestamp of the last write event.- Specified by:
getLastWriteTime
in interfaceIOSession
- Returns:
- timestamp.
-
getLastEventTime
public long getLastEventTime()
Description copied from interface:IOSession
Returns timestamp of the last I/O event including socket timeout reset.- Specified by:
getLastEventTime
in interfaceIOSession
- Returns:
- timestamp.
- See Also:
IOSession.getSocketTimeout()
-
formatOps
private static void formatOps(java.lang.StringBuilder buffer, int ops)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-