Class SocketFetcher


  • public class SocketFetcher
    extends java.lang.Object
    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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static MailLogger logger  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SocketFetcher()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private static void checkServerIdentity​(java.lang.String server, javax.net.ssl.SSLSocket sslSocket)
      Check the server from the Socket connection against the server name(s) as expressed in the server certificate (RFC 2595 check).
      private static java.io.IOException cleanupAndThrow​(java.net.Socket socket, java.io.IOException ife)  
      private static void configureSSLSocket​(java.net.Socket socket, java.lang.String host, java.util.Properties props, java.lang.String prefix, javax.net.SocketFactory sf)
      Configure the SSL options for the socket (if it's an SSL socket), based on the mail..ssl.protocols and mail..ssl.ciphersuites properties.
      private static java.net.Socket createSocket​(java.net.InetAddress localaddr, int localport, java.lang.String host, int port, int cto, int to, java.util.Properties props, java.lang.String prefix, javax.net.SocketFactory sf, boolean useSSL)
      Create a socket with the given local address and connected to the given host and port.
      private static java.lang.ClassLoader getContextClassLoader()
      Convenience method to get our context class loader.
      static java.net.Socket getSocket​(java.lang.String host, int port, java.util.Properties props, java.lang.String prefix)  
      static java.net.Socket getSocket​(java.lang.String host, int port, java.util.Properties props, java.lang.String prefix, boolean useSSL)
      This method returns a Socket.
      private static javax.net.SocketFactory getSocketFactory​(java.lang.String sfClass)
      Return a socket factory of the specified class.
      private static boolean isRecoverable​(java.lang.Throwable t)  
      private static boolean matchCert​(java.lang.String server, java.security.cert.X509Certificate cert)
      Do any of the names in the cert match the server name?
      private static boolean matchServer​(java.lang.String server, java.lang.String name)
      Does the server we're expecting to connect to match the given name from the server's certificate?
      private static void proxyConnect​(java.net.Socket socket, java.lang.String proxyHost, int proxyPort, java.lang.String proxyUser, java.lang.String proxyPassword, java.lang.String host, int port, int cto)
      Use the HTTP CONNECT protocol to connect to a site through an HTTP proxy server.
      static java.net.Socket startTLS​(java.net.Socket socket)
      Deprecated. 
      static java.net.Socket startTLS​(java.net.Socket socket, java.lang.String host, java.util.Properties props, java.lang.String prefix)
      Start TLS on an existing socket.
      static java.net.Socket startTLS​(java.net.Socket socket, java.util.Properties props, java.lang.String prefix)
      Deprecated. 
      private static java.lang.String[] stringArray​(java.lang.String s)
      Parse a string into whitespace separated tokens and return the tokens in an array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SocketFetcher

        private SocketFetcher()
    • Method Detail

      • getSocket

        public static java.net.Socket getSocket​(java.lang.String host,
                                                int port,
                                                java.util.Properties props,
                                                java.lang.String prefix,
                                                boolean useSSL)
                                         throws java.io.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 - The host to connect to
        port - The port to connect to at the host
        props - Properties object containing socket properties
        prefix - Property name prefix, e.g., "mail.imap"
        useSSL - use the SSL socket factory as the default
        Returns:
        the Socket
        Throws:
        java.io.IOException - for I/O errors
      • getSocket

        public static java.net.Socket getSocket​(java.lang.String host,
                                                int port,
                                                java.util.Properties props,
                                                java.lang.String prefix)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • createSocket

        private static java.net.Socket createSocket​(java.net.InetAddress localaddr,
                                                    int localport,
                                                    java.lang.String host,
                                                    int port,
                                                    int cto,
                                                    int to,
                                                    java.util.Properties props,
                                                    java.lang.String prefix,
                                                    javax.net.SocketFactory sf,
                                                    boolean useSSL)
                                             throws java.io.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.
        Throws:
        java.io.IOException
      • getSocketFactory

        private static javax.net.SocketFactory getSocketFactory​(java.lang.String sfClass)
                                                         throws java.lang.ClassNotFoundException,
                                                                java.lang.NoSuchMethodException,
                                                                java.lang.IllegalAccessException,
                                                                java.lang.reflect.InvocationTargetException
        Return a socket factory of the specified class.
        Throws:
        java.lang.ClassNotFoundException
        java.lang.NoSuchMethodException
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
      • startTLS

        @Deprecated
        public static java.net.Socket startTLS​(java.net.Socket socket)
                                        throws java.io.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 - the existing socket
        Returns:
        the wrapped Socket
        Throws:
        java.io.IOException - for I/O errors
      • startTLS

        @Deprecated
        public static java.net.Socket startTLS​(java.net.Socket socket,
                                               java.util.Properties props,
                                               java.lang.String prefix)
                                        throws java.io.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 - the existing socket
        props - the properties
        prefix - the property prefix
        Returns:
        the wrapped Socket
        Throws:
        java.io.IOException - for I/O errors
      • startTLS

        public static java.net.Socket startTLS​(java.net.Socket socket,
                                               java.lang.String host,
                                               java.util.Properties props,
                                               java.lang.String prefix)
                                        throws java.io.IOException
        Start TLS on an existing socket. Supports the "STARTTLS" command in many protocols.
        Parameters:
        socket - the existing socket
        host - the host the socket is connected to
        props - the properties
        prefix - the property prefix
        Returns:
        the wrapped Socket
        Throws:
        java.io.IOException - for I/O errors
      • configureSSLSocket

        private static void configureSSLSocket​(java.net.Socket socket,
                                               java.lang.String host,
                                               java.util.Properties props,
                                               java.lang.String prefix,
                                               javax.net.SocketFactory sf)
                                        throws java.io.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.
        Throws:
        java.io.IOException
      • cleanupAndThrow

        private static java.io.IOException cleanupAndThrow​(java.net.Socket socket,
                                                           java.io.IOException ife)
      • isRecoverable

        private static boolean isRecoverable​(java.lang.Throwable t)
      • checkServerIdentity

        private static void checkServerIdentity​(java.lang.String server,
                                                javax.net.ssl.SSLSocket sslSocket)
                                         throws java.io.IOException
        Check the server from the Socket connection against the server name(s) as expressed in the server certificate (RFC 2595 check).
        Parameters:
        server - name of the server expected
        sslSocket - SSLSocket connected to the server
        Throws:
        java.io.IOException - if we can't verify identity of server
      • matchCert

        private static boolean matchCert​(java.lang.String server,
                                         java.security.cert.X509Certificate cert)
        Do any of the names in the cert match the server name?
        Parameters:
        server - name of the server expected
        cert - X509Certificate to get the subject's name from
        Returns:
        true if it matches
      • matchServer

        private static boolean matchServer​(java.lang.String server,
                                           java.lang.String name)
        Does the server we're expecting to connect to match the given name from the server's certificate?
        Parameters:
        server - name of the server expected
        name - name from the server's certificate
      • proxyConnect

        private static void proxyConnect​(java.net.Socket socket,
                                         java.lang.String proxyHost,
                                         int proxyPort,
                                         java.lang.String proxyUser,
                                         java.lang.String proxyPassword,
                                         java.lang.String host,
                                         int port,
                                         int cto)
                                  throws java.io.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
         
         
         
        Throws:
        java.io.IOException
      • stringArray

        private static java.lang.String[] stringArray​(java.lang.String s)
        Parse a string into whitespace separated tokens and return the tokens in an array.
      • getContextClassLoader

        private static java.lang.ClassLoader getContextClassLoader()
        Convenience method to get our context class loader. Assert any privileges we might have and then call the Thread.getContextClassLoader method.