Class ALPN


  • public class ALPN
    extends Object

    ALPN provides an API to applications that want to make use of the Application Layer Protocol Negotiation.

    The ALPN extension is only available when using the TLS protocol, therefore applications must ensure that the TLS protocol is used:

     SSLContext context = SSLContext.getInstance("TLSv1");
     

    Refer to the list of standard SSLContext protocol names for further information on TLS protocol versions supported.

    Applications must register instances of either SSLSocket or SSLEngine with a ALPN.ClientProvider or with a ALPN.ServerProvider, depending whether they are on client or server side.

    The ALPN implementation will invoke the provider callbacks to allow applications to interact with the negotiation of the protocol.

    Client side typical usage:

     final SSLSocket sslSocket = ...;
     ALPN.put(sslSocket, new ALPN.ClientProvider()
     {
         @Override
         public List<String> protocols()
         {
             return Arrays.asList("spdy/3", "http/1.1");
         }
    
         @Override
         public void unsupported()
         {
             ALPN.remove(sslSocket);
         }
    
         @Override
         public void selected(String protocol) throws SSLException
         {
             System.out.println("Selected protocol: " + protocol);
             ALPN.remove(sslSocket);
         }
      });
     

    Server side typical usage:

     final SSLSocket sslSocket = ...;
     ALPN.put(sslSocket, new ALPN.ServerProvider()
     {
         @Override
         public void unsupported()
         {
             ALPN.remove(sslSocket);
         }
    
         @Override
         public String select(List<String> protocols) throws SSLException
         {
             ALPN.remove(sslSocket);
             return protocols.get(0);
         }
      });
     

    Applications must ensure to deregister SSLSocket or SSLEngine instances, because they are kept in a JVM global map. Deregistration should typically happen when the application detects the end of the protocol negotiation, and/or when the associated socket connection is closed.

    In order to help application development, you can set the debug field to true to have debug code printed to System.err.

    • Field Detail

      • debug

        public static boolean debug
        Flag that enables printing of debug statements to System.err.
    • Method Detail

      • put

        public static void put​(SSLSocket socket,
                               ALPN.Provider provider)
        Registers a SSLSocket with a provider.
        Parameters:
        socket - the socket to register with the provider
        provider - the provider to register with the socket
        See Also:
        remove(SSLSocket)
      • remove

        public static ALPN.Provider remove​(SSLSocket socket)
        Unregisters the given SSLSocket.
        Parameters:
        socket - the socket to unregister
        Returns:
        the provider registered with the socket
        See Also:
        put(SSLSocket, Provider)
      • put

        public static void put​(SSLEngine engine,
                               ALPN.Provider provider)
        Registers a SSLEngine with a provider.
        Parameters:
        engine - the engine to register with the provider
        provider - the provider to register with the engine
        See Also:
        remove(SSLEngine)
      • remove

        public static ALPN.Provider remove​(SSLEngine engine)
        Unregisters the given SSLEngine.
        Parameters:
        engine - the engine to unregister
        Returns:
        the provider registered with the engine
        See Also:
        put(SSLEngine, Provider)