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
ClientProvider or with a 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 ALPN#debug field
to true to have debug code printed to System#err.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ALPN.ClientProvider
The client-side provider interface that applications must implement to interact with the negotiation of the protocol. |
| public static interface | ALPN.Provider
Base, empty, interface for providers. |
| public 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 |
|---|---|
| public static boolean | debug
Flag that enables printing of debug statements to |
| private static Map |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ALPN. | Returns: the provider registered with the given socketa socket registered with socket)put(SSLSocket, Provider)
|
| public static ALPN. | Returns: the provider registered with the given enginean engine registered with engine)put(SSLEngine, Provider)
|
| public static void | |
| public static void | |
| public static ALPN. | |
| public static ALPN. |
| debug | back to summary |
|---|---|
| public static boolean debug Flag that enables printing of debug statements to | |
| objects | back to summary |
|---|---|
| private static Map<Object, ALPN. | |
| ALPN | back to summary |
|---|---|
| private ALPN() | |
| get | back to summary |
|---|---|
public static ALPN.
| |
| get | back to summary |
|---|---|
public static ALPN.
| |
| put | back to summary |
|---|---|
| public static void put(SSLSocket socket, ALPN. Registers a SSLSocket with a provider.
| |
| put | back to summary |
|---|---|
| public static void put(SSLEngine engine, ALPN. Registers a SSLEngine with a provider.
| |
| remove | back to summary |
|---|---|
| public static ALPN. Unregisters the given SSLSocket.
| |
| remove | back to summary |
|---|---|
| public static ALPN. Unregisters the given SSLEngine.
| |
| Modifier and Type | Method and Description |
|---|---|
| public List | Returns: the list of protocols supported by the client; ifnull or empty, the ALPN extension is not sentCallback invoked to let the implementation know the list of protocols that should be added to the ALPN extension in a ClientHello TLS message. |
| public void | |
| public void | unsupported()
Callback invoked to let the client application know that the server does not support ALPN. |
| protocols | back to summary |
|---|---|
| public List Callback invoked to let the implementation know the list of protocols that should be added to the ALPN extension in a ClientHello TLS message. | |
| selected | back to summary |
|---|---|
| public void selected(String protocol) throws SSLException Callback invoked to let the client application know the protocol chosen by the server. The implementation may throw an exception to indicate
that the protocol selected by the server is not acceptable,
causing the connection to be closed with a TLS alert of
type
| |
| unsupported | back to summary |
|---|---|
| public void unsupported() Callback invoked to let the client application know that the server does not support ALPN. | |
| Modifier and Type | Method and Description |
|---|---|
| public String | Returns: the protocol selected by the server application. Anull value will indicate the server will not
include the ALPN extension message in the ServerHello
message. This means the server appears as though it doesn't support
ALPN and lets the client decide how to handle the situation.the protocols sent by the client. protocols)Callback invoked to let the server application select a protocol among the ones sent by the client. |
| public void | unsupported()
Callback invoked to let the server application know that the client does not support ALPN. |
| select | back to summary |
|---|---|
| public String select(List<String> protocols) throws SSLException Callback invoked to let the server application select a protocol among the ones sent by the client. The implementation may throw an exception to indicate
that the protocol negotiation is not possible,
causing the connection to be closed with a TLS alert of
type
| |
| unsupported | back to summary |
|---|---|
| public void unsupported() Callback invoked to let the server application know that the client does not support ALPN. | |