Class ALPN


  • public class ALPN
    extends java.lang.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:

     SSLSocket sslSocket = ...;
     ALPN.put(sslSocket, new ALPN.ClientProvider()
     {
         @Override
         public boolean supports()
         {
             return true;
         }
    
         @Override
         public List<String> protocols()
         {
             return Arrays.asList("spdy/3", "http/1.1");
         }
    
         @Override
         public void unsupported()
         {
         }
    
         @Override
         public void selected(String protocol)
         {
             System.out.println("Selected protocol: " + protocol);
         }
      });
     
    Server side typical usage:
     SSLSocket sslSocket = ...;
     ALPN.put(sslSocket, new ALPN.ServerProvider()
     {
         @Override
         public void unsupported()
         {
         }
    
         @Override
         public String select(List<String> protocols)
         {
             return protocols.get(0);
         }
      });
     
    Applications must ensure to deregister SSLSocket or SSLEngine instances, because they are kept in a 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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ALPN.ClientProvider
      The client-side provider interface that applications must implement to interact with the negotiation of the protocol.
      static interface  ALPN.Provider
      Base, empty, interface for providers.
      static interface  ALPN.ServerProvider
      The server-side provider interface that applications must implement to interact with the negotiation of the protocol.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean debug
      Flag that enables printing of debug statements to System.err.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static ALPN.Provider get​(javax.net.ssl.SSLEngine engine)  
      static ALPN.Provider get​(javax.net.ssl.SSLSocket socket)  
      static void put​(javax.net.ssl.SSLEngine engine, ALPN.Provider provider)
      Registers a SSLEngine with a provider.
      static void put​(javax.net.ssl.SSLSocket socket, ALPN.Provider provider)
      Registers a SSLSocket with a provider.
      static ALPN.Provider remove​(javax.net.ssl.SSLEngine engine)
      Unregisters the given SSLEngine.
      static ALPN.Provider remove​(javax.net.ssl.SSLSocket socket)
      Unregisters the given SSLSocket.
      • Methods inherited from class java.lang.Object

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

      • debug

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

      • put

        public static void put​(javax.net.ssl.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)
      • get

        public static ALPN.Provider get​(javax.net.ssl.SSLSocket socket)
        Parameters:
        socket - a socket registered with put(SSLSocket, Provider)
        Returns:
        the provider registered with the given socket
      • remove

        public static ALPN.Provider remove​(javax.net.ssl.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​(javax.net.ssl.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)
      • get

        public static ALPN.Provider get​(javax.net.ssl.SSLEngine engine)
        Parameters:
        engine - an engine registered with put(SSLEngine, Provider)
        Returns:
        the provider registered with the given engine
      • remove

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