Top Description Fields Constructors Methods
com.sun.mail.util

public Class SocketFetcher

extends Object
Class Inheritance
Imports
java.security.*, java.net.*, java.io.*, java.nio.channels.SocketChannel, java.nio.charset.StandardCharsets, java.lang.reflect.*, java.util.*, java.util.regex.*, java.util.logging.Level, java.security.cert.*, javax.net.*, javax.net.ssl.*

This class is used to get Sockets. Depending on the arguments passed it will either return a plain java.net.Socket or dynamically load the SocketFactory class specified in the classname param and return a socket created by that SocketFactory.
Authors
Max Spivak, Bill Shannon

Field Summary

Modifier and TypeField and Description
private static MailLogger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
checkServerIdentity(String
name of the server expected
server
,
SSLSocket
SSLSocket connected to the server
sslSocket
)

Check the server from the Socket connection against the server name(s) as expressed in the server certificate (RFC 2595 check).

private static IOException
private static void
configureSSLSocket(Socket socket, String host, Properties props, String prefix, SocketFactory sf)

Configure the SSL options for the socket (if it's an SSL socket), based on the mail.

private static Socket
createSocket(InetAddress localaddr, int localport, String host, int port, int cto, int to, Properties props, String prefix, SocketFactory sf, boolean useSSL)

Create a socket with the given local address and connected to the given host and port.

private static ClassLoader
getContextClassLoader()

Convenience method to get our context class loader.

public static Socket

Returns:

the Socket
getSocket
(String
The host to connect to
host
,
int
The port to connect to at the host
port
,
Properties
Properties object containing socket properties
props
,
String
Property name prefix, e.g., "mail.imap"
prefix
,
boolean
use the SSL socket factory as the default
useSSL
)

This method returns a Socket.

public static Socket
getSocket(String host, int port, Properties props, String prefix)

private static SocketFactory
getSocketFactory(String sfClass)

Return a socket factory of the specified class.

private static boolean
private static boolean

Returns:

true if it matches
matchCert
(String
name of the server expected
server
,
X509Certificate
X509Certificate to get the subject's name from
cert
)

Do any of the names in the cert match the server name?

private static boolean
matchServer(String
name of the server expected
server
,
String
name from the server's certificate
name
)

Does the server we're expecting to connect to match the given name from the server's certificate?

private static void
proxyConnect(Socket socket, String proxyHost, int proxyPort, String proxyUser, String proxyPassword, String host, int port, int cto)

Use the HTTP CONNECT protocol to connect to a site through an HTTP proxy server.

public static Socket

Returns:

the wrapped Socket
startTLS
(Socket
the existing socket
socket
)

Deprecated
Start TLS on an existing socket.
public static Socket

Returns:

the wrapped Socket
startTLS
(Socket
the existing socket
socket
,
Properties
the properties
props
,
String
the property prefix
prefix
)

Deprecated
Start TLS on an existing socket.
public static Socket

Returns:

the wrapped Socket
startTLS
(Socket
the existing socket
socket
,
String
the host the socket is connected to
host
,
Properties
the properties
props
,
String
the property prefix
prefix
)

Start TLS on an existing socket.

private static String[]
stringArray(String s)

Parse a string into whitespace separated tokens and return the tokens in an array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static MailLogger logger

Constructor Detail

SocketFetcherback to summary
private SocketFetcher()

Method Detail

checkServerIdentityback to summary
private static void checkServerIdentity(String server, SSLSocket sslSocket) throws IOException

Check the server from the Socket connection against the server name(s) as expressed in the server certificate (RFC 2595 check).

Parameters
server:String

name of the server expected

sslSocket:SSLSocket

SSLSocket connected to the server

Exceptions
IOException:
if we can't verify identity of server
cleanupAndThrowback to summary
private static IOException cleanupAndThrow(Socket socket, IOException ife)
configureSSLSocketback to summary
private static void configureSSLSocket(Socket socket, String host, Properties props, String prefix, SocketFactory sf) throws IOException

Configure the SSL options for the socket (if it's an SSL socket), based on the mail..ssl.protocols and mail..ssl.ciphersuites properties. Check the identity of the server as specified by the mail..ssl.checkserveridentity property.

createSocketback to summary
private static Socket createSocket(InetAddress localaddr, int localport, String host, int port, int cto, int to, Properties props, String prefix, SocketFactory sf, boolean useSSL) throws IOException

Create a socket with the given local address and connected to the given host and port. Use the specified connection timeout and read timeout. If a socket factory is specified, use it. Otherwise, use the SSLSocketFactory if useSSL is true.

getContextClassLoaderback to summary
private static ClassLoader getContextClassLoader()

Convenience method to get our context class loader. Assert any privileges we might have and then call the Thread.getContextClassLoader method.

getSocketback to summary
public static Socket getSocket(String host, int port, Properties props, String prefix, boolean useSSL) throws IOException

This method returns a Socket. Properties control the use of socket factories and other socket characteristics. The properties used are:

  • prefix.socketFactory
  • prefix.socketFactory.class
  • prefix.socketFactory.fallback
  • prefix.socketFactory.port
  • prefix.ssl.socketFactory
  • prefix.ssl.socketFactory.class
  • prefix.ssl.socketFactory.port
  • prefix.timeout
  • prefix.connectiontimeout
  • prefix.localaddress
  • prefix.localport
  • prefix.usesocketchannels

If we're making an SSL connection, the ssl.socketFactory properties are used first, if set.

If the socketFactory property is set, the value is an instance of a SocketFactory class, not a string. The instance is used directly. If the socketFactory property is not set, the socketFactory.class property is considered. (Note that the SocketFactory property must be set using the put method, not the setProperty method.)

If the socketFactory.class property isn't set, the socket returned is an instance of java.net.Socket connected to the given host and port. If the socketFactory.class property is set, it is expected to contain a fully qualified classname of a javax.net.SocketFactory subclass. In this case, the class is dynamically instantiated and a socket created by that SocketFactory is returned.

If the socketFactory.fallback property is set to false, don't fall back to using regular sockets if the socket factory fails.

The socketFactory.port specifies a port to use when connecting through the socket factory. If unset, the port argument will be used.

If the connectiontimeout property is set, the timeout is passed to the socket connect method.

If the timeout property is set, it is used to set the socket timeout.

If the localaddress property is set, it's used as the local address to bind to. If the localport property is also set, it's used as the local port number to bind to.

If the usesocketchannels property is set, and we create the Socket ourself, and the selection of other properties allows, create a SocketChannel and get the Socket from it. This allows us to later retrieve the SocketChannel from the Socket and use it with Select.

Parameters
host:String

The host to connect to

port:int

The port to connect to at the host

props:Properties

Properties object containing socket properties

prefix:String

Property name prefix, e.g., "mail.imap"

useSSL:boolean

use the SSL socket factory as the default

Returns:Socket

the Socket

Exceptions
IOException:
for I/O errors
getSocketback to summary
public static Socket getSocket(String host, int port, Properties props, String prefix) throws IOException
getSocketFactoryback to summary
private static SocketFactory getSocketFactory(String sfClass) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException

Return a socket factory of the specified class.

isRecoverableback to summary
private static boolean isRecoverable(Throwable t)
matchCertback to summary
private static boolean matchCert(String server, X509Certificate cert)

Do any of the names in the cert match the server name?

Parameters
server:String

name of the server expected

cert:X509Certificate

X509Certificate to get the subject's name from

Returns:boolean

true if it matches

matchServerback to summary
private static boolean matchServer(String server, String name)

Does the server we're expecting to connect to match the given name from the server's certificate?

Parameters
server:String

name of the server expected

name:String

name from the server's certificate

proxyConnectback to summary
private static void proxyConnect(Socket socket, String proxyHost, int proxyPort, String proxyUser, String proxyPassword, String host, int port, int cto) throws IOException

Use the HTTP CONNECT protocol to connect to a site through an HTTP proxy server.

Protocol is roughly:

CONNECT : HTTP/1.1
Host: :


HTTP/1.1 200 Connect established


startTLSback to summary
public static Socket startTLS(Socket socket) throws IOException

Deprecated

Start TLS on an existing socket. Supports the "STARTTLS" command in many protocols. This version for compatibility with possible third party code that might've used this API even though it shouldn't.

Parameters
socket:Socket

the existing socket

Returns:Socket

the wrapped Socket

Annotations
@Deprecated
Exceptions
IOException:
for I/O errors
startTLSback to summary
public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException

Deprecated

Start TLS on an existing socket. Supports the "STARTTLS" command in many protocols. This version for compatibility with possible third party code that might've used this API even though it shouldn't.

Parameters
socket:Socket

the existing socket

props:Properties

the properties

prefix:String

the property prefix

Returns:Socket

the wrapped Socket

Annotations
@Deprecated
Exceptions
IOException:
for I/O errors
startTLSback to summary
public static Socket startTLS(Socket socket, String host, Properties props, String prefix) throws IOException

Start TLS on an existing socket. Supports the "STARTTLS" command in many protocols.

Parameters
socket:Socket

the existing socket

host:String

the host the socket is connected to

props:Properties

the properties

prefix:String

the property prefix

Returns:Socket

the wrapped Socket

Exceptions
IOException:
for I/O errors
stringArrayback to summary
private static String[] stringArray(String s)

Parse a string into whitespace separated tokens and return the tokens in an array.