Class ALPN

java.lang.Object
org.eclipse.jetty.alpn.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:

 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.

  • Field Details

    • debug

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

    • 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:
    • get

      public static ALPN.Provider get(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(SSLSocket socket)
      Unregisters the given SSLSocket.
      Parameters:
      socket - the socket to unregister
      Returns:
      the provider registered with the socket
      See Also:
    • 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:
    • get

      public static ALPN.Provider get(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(SSLEngine engine)
      Unregisters the given SSLEngine.
      Parameters:
      engine - the engine to unregister
      Returns:
      the provider registered with the engine
      See Also: