java.lang.Object
org.newsclub.net.unix.ssl.SNIHostnameCapture
Helper class to simplify retrieving the requested SNI hostname sent from an SSL client to an SSL
server.
- Author:
- Christian Kohlschütter
-
Field Summary
Modifier and TypeFieldDescriptionstatic final SNIMatcher
SNIMatcher
that accepts and matches "any hostname". -
Method Summary
Modifier and TypeMethodDescriptionstatic SNIHostnameCapture
configure
(SSLSocket sslSocket, SNIMatcher hostnameMatcher) Configures the given serverSSLSocket
to match the given hostname pattern.static SNIHostnameCapture
configure
(SSLSocket sslSocket, SNIMatcher hostnameMatcher, Supplier<String> defaultHostnameSupplier) Configures the given serverSSLSocket
to match the given hostname pattern.Returns the hostname (either the retrieved one, or a fallback), which could also benull
.boolean
Checks if a hostname can be returned by callinggetHostname()
(which most likely means the handshake is complete).boolean
isComplete
(long timeout, TimeUnit unit) Checks if a hostname can be returned by callinggetHostname()
(which most likely means the handshake is complete), optionally waiting up to the given time interval if not yet complete.
-
Field Details
-
ACCEPT_ANY_HOSTNAME
SNIMatcher
that accepts and matches "any hostname".
-
-
Method Details
-
configure
Configures the given serverSSLSocket
to match the given hostname pattern.The matched hostname is then accessible after the handshake is completed, via
getHostname()
. If no hostname was retrieved,null
is assumed.- Parameters:
sslSocket
- The socket to configure.hostnameMatcher
- The matcher to use.- Returns:
- The
SNIHostnameCapture
instance. - See Also:
-
configure
public static SNIHostnameCapture configure(SSLSocket sslSocket, SNIMatcher hostnameMatcher, Supplier<String> defaultHostnameSupplier) Configures the given serverSSLSocket
to match the given hostname pattern.The matched hostname is then accessible after the handshake is completed, via
getHostname()
. If no hostname was retrieved, a fallback is retrieved via the given supplier.- Parameters:
sslSocket
- The socket to configure.hostnameMatcher
- The matcher to use.defaultHostnameSupplier
- The supplier for a default hostname, ornull
.- Returns:
- The
SNIHostnameCapture
instance. - See Also:
-
isComplete
Checks if a hostname can be returned by callinggetHostname()
(which most likely means the handshake is complete), optionally waiting up to the given time interval if not yet complete.- Parameters:
timeout
- The maximum timeout value to wait for.unit
- Timeout unit.- Returns:
true
ifgetHostname()
will not throw anIllegalStateException
.- Throws:
InterruptedException
- if the current thread was interrupted while waiting.
-
isComplete
public boolean isComplete()Checks if a hostname can be returned by callinggetHostname()
(which most likely means the handshake is complete).- Returns:
true
ifgetHostname()
will not throw anIllegalStateException
.
-
getHostname
Returns the hostname (either the retrieved one, or a fallback), which could also benull
.If the method is called before the handshake is complete (check with
isComplete()
or simply call afterSSLSocket.startHandshake()
), anIllegalStateException
is thrown.- Returns:
- The hostname, or
null
. - Throws:
IllegalStateException
- if the method was called before a hostname could be retrieved.
-