Class WebSocketFactory
WebSocket
instances.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private int
private DualStackMode
private final ProxySettings
private String[]
private final SocketFactorySettings
private int
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate SocketConnector
createDirectRawSocket
(String host, int port, boolean secure, int timeout) private SocketConnector
createProxiedRawSocket
(String host, int port, boolean secure, int timeout) private SocketConnector
createRawSocket
(String host, int port, boolean secure, int timeout) createSocket
(String uri) Create a WebSocket.createSocket
(String uri, int timeout) Create a WebSocket.private WebSocket
createSocket
(String scheme, String userInfo, String host, int port, String path, String query, int timeout) createSocket
(URI uri) Create a WebSocket.createSocket
(URI uri, int timeout) Create a WebSocket.createSocket
(URL url) Create a WebSocket.createSocket
(URL url, int timeout) Create a WebSocket.private WebSocket
createWebSocket
(boolean secure, String userInfo, String host, int port, String path, String query, SocketConnector connector) private static String
determinePath
(String path) private static int
determinePort
(int port, boolean secure) int
Get the timeout value in milliseconds for socket connection.int
Get the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.Get the dual stack mode that will be applied when establishing a socket connection.Get the proxy settings.String[]
Get server names for SNI (Server Name Indication).Get the socket factory that has been set bysetSocketFactory(SocketFactory)
.int
Get the timeout value in milliseconds for socket read and write operations.Get the SSL context that has been set bysetSSLContext(SSLContext)
.Get the SSL socket factory that has been set bysetSSLSocketFactory(SSLSocketFactory)
.boolean
Get the flag which indicates whether the hostname in the server's certificate should be verified or not.private static boolean
isSecureConnectionRequired
(String scheme) setConnectionTimeout
(int timeout) Set the timeout value in milliseconds for socket connection.setDualStackFallbackDelay
(int delay) Set the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.Set the dual stack mode that will be applied when establishing a socket connection.setServerName
(String serverName) Set a server name for SNI (Server Name Indication).setServerNames
(String[] serverNames) Set server names for SNI (Server Name Indication).setSocketFactory
(SocketFactory factory) Set a socket factory.setSocketTimeout
(int timeout) Set the timeout value in milliseconds for socket read and write operations.setSSLContext
(SSLContext context) Set an SSL context to get a socket factory.setSSLSocketFactory
(SSLSocketFactory factory) Set an SSL socket factory.setVerifyHostname
(boolean verifyHostname) Set the flag which indicates whether the hostname in the server's certificate should be verified or not.
-
Field Details
-
mSocketFactorySettings
-
mProxySettings
-
mConnectionTimeout
private int mConnectionTimeout -
mSocketTimeout
private int mSocketTimeout -
mDualStackMode
-
mDualStackFallbackDelay
private int mDualStackFallbackDelay -
mVerifyHostname
private boolean mVerifyHostname -
mServerNames
-
-
Constructor Details
-
WebSocketFactory
public WebSocketFactory()Constructor. -
WebSocketFactory
Copy constructor.- Parameters:
other
- AWebSocketFactory
instance to copy.- Throws:
IllegalArgumentException
- If the givenWebSocketFactory
instance is null.- Since:
- 2.10
-
-
Method Details
-
getSocketFactory
Get the socket factory that has been set bysetSocketFactory(SocketFactory)
.- Returns:
- The socket factory.
-
setSocketFactory
Set a socket factory. SeecreateSocket(URI)
for details.- Parameters:
factory
- A socket factory.- Returns:
this
instance.
-
getSSLSocketFactory
Get the SSL socket factory that has been set bysetSSLSocketFactory(SSLSocketFactory)
.- Returns:
- The SSL socket factory.
-
setSSLSocketFactory
Set an SSL socket factory. SeecreateSocket(URI)
for details.- Parameters:
factory
- An SSL socket factory.- Returns:
this
instance.
-
getSSLContext
Get the SSL context that has been set bysetSSLContext(SSLContext)
.- Returns:
- The SSL context.
-
setSSLContext
Set an SSL context to get a socket factory. SeecreateSocket(URI)
for details.- Parameters:
context
- An SSL context.- Returns:
this
instance.
-
getProxySettings
Get the proxy settings.- Returns:
- The proxy settings.
- Since:
- 1.3
- See Also:
-
getConnectionTimeout
public int getConnectionTimeout()Get the timeout value in milliseconds for socket connection. The default value is 0 and it means an infinite timeout.When a
createSocket
method which does not havetimeout
argument is called, the value returned by this method is used as a timeout value for socket connection.- Returns:
- The connection timeout value in milliseconds.
- Since:
- 1.10
-
setConnectionTimeout
Set the timeout value in milliseconds for socket connection. A timeout of zero is interpreted as an infinite timeout.- Parameters:
timeout
- The connection timeout value in milliseconds.- Returns:
this
object.- Throws:
IllegalArgumentException
- The given timeout value is negative.- Since:
- 1.10
-
getSocketTimeout
public int getSocketTimeout()Get the timeout value in milliseconds for socket read and write operations. The default value is 0 and it means an infinite timeout.This can be changed later with
getSocket().setSoTimeout(int)
.- Returns:
- The socket timeout value in milliseconds.
- Since:
- 2.14
- See Also:
-
setSocketTimeout
Set the timeout value in milliseconds for socket read and write operations. A timeout of zero is interpreted as an infinite timeout.This can be changed later with
getSocket().setSoTimeout(int)
.- Parameters:
timeout
- The socket timeout value in milliseconds.- Returns:
this
object.- Throws:
IllegalArgumentException
- The given timeout value is negative.- Since:
- 2.14
- See Also:
-
getDualStackMode
Get the dual stack mode that will be applied when establishing a socket connection. The default value isDualStackMode.BOTH
.A hostname may resolve to an arbitrary amount of IPv4 and IPv6 addresses. This controls which IP address families will be used when establishing a connection. Note that IPv6 will be preferred, if activated.
- Returns:
- The dual stack mode.
-
setDualStackMode
Set the dual stack mode that will be applied when establishing a socket connection.- Parameters:
mode
- The dual stack mode to be applied.- Returns:
this
object.
-
getDualStackFallbackDelay
public int getDualStackFallbackDelay()Get the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.A hostname may resolve to an arbitrary amount of IPv4 and IPv6 addresses. This controls the maximum amount of time that may pass between attempts to establish a socket connection to an IP addresses before trying the next one. Note that the previous attempt will not be aborted. The connections will race until one has been established.
- Returns:
- The dual stack fallback delay in milliseconds.
-
setDualStackFallbackDelay
Set the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.- Parameters:
delay
- The dual stack fallback delay in milliseconds.- Returns:
this
object.
-
getVerifyHostname
public boolean getVerifyHostname()Get the flag which indicates whether the hostname in the server's certificate should be verified or not. The default value istrue
. See the description ofsetVerifyHostname(boolean)
to understand what this boolean flag means.- Returns:
true
if hostname verification is enabled.- Since:
- 2.3
-
setVerifyHostname
Set the flag which indicates whether the hostname in the server's certificate should be verified or not. The default value istrue
.Manual hostname verification has been enabled since the version 2.1. Because the verification is executed manually after
Socket.
connect(SocketAddress, int)
succeeds, the hostname verification is always executed even if you has passed anSSLContext
which naively accepts any server certificate (e.g.NaiveSSLContext
). However, this behavior is not desirable in some cases and you may want to disable the hostname verification. This setter method exists for the purpose and you can disable hostname verification by passingfalse
to this method.- Parameters:
verifyHostname
-true
to enable hostname verification.false
to disable hostname verification.- Returns:
this
object.- Since:
- 2.3
-
getServerNames
Get server names for SNI (Server Name Indication).- Returns:
- List of host names.
- Since:
- 2.4
-
setServerNames
Set server names for SNI (Server Name Indication). IfsetServerNames(List<SNIServerName>)
method ofSSLParameters
class is available in the underlying system, the method is called to set up server names for SNI (Server Name Indication).- Parameters:
serverNames
- List of host names.- Returns:
this
object.- Since:
- 2.4
-
setServerName
Set a server name for SNI (Server Name Indication). This method internally creates a String array of size 1 which contains the givenserverName
and callssetServerNames(String[])
.- Parameters:
serverName
- A host name.- Returns:
this
object.- Since:
- 2.4
-
createSocket
Create a WebSocket.This method is an alias of
createSocket
(uri,
getConnectionTimeout()
)
.- Parameters:
uri
- The URI of the WebSocket endpoint on the server side.- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URI isnull
or violates RFC 2396.IOException
- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.
-
createSocket
Create a WebSocket.This method is an alias of
createSocket
(
URI.create
(uri), timeout)
.- Parameters:
uri
- The URI of the WebSocket endpoint on the server side.timeout
- The timeout value in milliseconds for socket connection. A timeout of zero is interpreted as an infinite timeout.- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URI isnull
or violates RFC 2396, or the given timeout value is negative.IOException
- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.- Since:
- 1.10
-
createSocket
Create a WebSocket.This method is an alias of
createSocket
(url,
getConnectionTimeout()
)
.- Parameters:
url
- The URL of the WebSocket endpoint on the server side.- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URL isnull
or failed to be converted into a URI.IOException
- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.
-
createSocket
Create a WebSocket.This method is an alias of
createSocket
(url.
toURI()
, timeout)
.- Parameters:
url
- The URL of the WebSocket endpoint on the server side.timeout
- The timeout value in milliseconds for socket connection.- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URL isnull
or failed to be converted into a URI, or the given timeout value is negative.IOException
- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.- Since:
- 1.10
-
createSocket
Create a WebSocket. This method is an alias ofcreateSocket
(uri,
getConnectionTimeout()
)
.A socket factory (= a
SocketFactory
instance) to create a raw socket (= aSocket
instance) is determined as described below.-
If the scheme of the URI is either
wss
orhttps
,-
If an
SSLContext
instance has been set bysetSSLContext(SSLContext)
, the value returned fromSSLContext.getSocketFactory()
method of the instance is used. -
Otherwise, if an
SSLSocketFactory
instance has been set bysetSSLSocketFactory(SSLSocketFactory)
, the instance is used. -
Otherwise, the value returned from
SSLSocketFactory.getDefault()
is used.
-
If an
-
Otherwise (= the scheme of the URI is either
ws
orhttp
),-
If a
SocketFactory
instance has been set bysetSocketFactory(SocketFactory)
, the instance is used. -
Otherwise, the value returned from
SocketFactory.getDefault()
is used.
-
If a
- Parameters:
uri
- The URI of the WebSocket endpoint on the server side. The scheme part of the URI must be one ofws
,wss
,http
andhttps
(case-insensitive).- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URI isnull
or violates RFC 2396.IOException
- Failed to create a socket.
-
If the scheme of the URI is either
-
createSocket
Create a WebSocket.A socket factory (= a
SocketFactory
instance) to create a raw socket (= aSocket
instance) is determined as described below.-
If the scheme of the URI is either
wss
orhttps
,-
If an
SSLContext
instance has been set bysetSSLContext(SSLContext)
, the value returned fromSSLContext.getSocketFactory()
method of the instance is used. -
Otherwise, if an
SSLSocketFactory
instance has been set bysetSSLSocketFactory(SSLSocketFactory)
, the instance is used. -
Otherwise, the value returned from
SSLSocketFactory.getDefault()
is used.
-
If an
-
Otherwise (= the scheme of the URI is either
ws
orhttp
),-
If a
SocketFactory
instance has been set bysetSocketFactory(SocketFactory)
, the instance is used. -
Otherwise, the value returned from
SocketFactory.getDefault()
is used.
-
If a
- Parameters:
uri
- The URI of the WebSocket endpoint on the server side. The scheme part of the URI must be one ofws
,wss
,http
andhttps
(case-insensitive).timeout
- The timeout value in milliseconds for socket connection.- Returns:
- A WebSocket.
- Throws:
IllegalArgumentException
- The given URI isnull
or violates RFC 2396, or the given timeout value is negative.IOException
- Failed to create a socket.- Since:
- 1.10
-
If the scheme of the URI is either
-
createSocket
private WebSocket createSocket(String scheme, String userInfo, String host, int port, String path, String query, int timeout) throws IOException - Throws:
IOException
-
isSecureConnectionRequired
-
determinePath
-
createRawSocket
private SocketConnector createRawSocket(String host, int port, boolean secure, int timeout) throws IOException - Throws:
IOException
-
createProxiedRawSocket
-
createDirectRawSocket
-
determinePort
private static int determinePort(int port, boolean secure) -
createWebSocket
-