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
.
Modifier and Type | Class and 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.
|
Modifier and Type | Field and Description |
---|---|
static boolean |
debug
Flag that enables printing of debug statements to
System.err . |
Modifier and Type | Method and Description |
---|---|
static ALPN.Provider |
get(SSLEngine engine) |
static ALPN.Provider |
get(SSLSocket socket) |
static void |
put(SSLEngine engine,
ALPN.Provider provider)
Registers a SSLEngine with a provider.
|
static void |
put(SSLSocket socket,
ALPN.Provider provider)
Registers a SSLSocket with a provider.
|
static ALPN.Provider |
remove(SSLEngine engine)
Unregisters the given SSLEngine.
|
static ALPN.Provider |
remove(SSLSocket socket)
Unregisters the given SSLSocket.
|
public static boolean debug
System.err
.public static void put(SSLSocket socket, ALPN.Provider provider)
socket
- the socket to register with the providerprovider
- the provider to register with the socketremove(SSLSocket)
public static ALPN.Provider get(SSLSocket socket)
socket
- a socket registered with put(SSLSocket, Provider)
public static ALPN.Provider remove(SSLSocket socket)
socket
- the socket to unregisterput(SSLSocket, Provider)
public static void put(SSLEngine engine, ALPN.Provider provider)
engine
- the engine to register with the providerprovider
- the provider to register with the engineremove(SSLEngine)
public static ALPN.Provider get(SSLEngine engine)
engine
- an engine registered with put(SSLEngine, Provider)
public static ALPN.Provider remove(SSLEngine engine)
engine
- the engine to unregisterput(SSLEngine, Provider)
Copyright © 1995–2021 Mort Bay Consulting. All rights reserved.