NextProtoNego provides an API to applications that want to make use of the
Next Protocol Negotiation.
The NPN 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 NPN implementation will invoke the provider callbacks to allow applications to interact with the negotiation of the next protocol.
Client side typical usage:
final SSLSocket sslSocket = ...;
NextProtoNego.put(sslSocket, new NextProtoNego.ClientProvider()
{
@Override
public boolean supports()
{
return true;
}
@Override
public void unsupported()
{
NextProtoNego.remove(sslSocket);
}
@Override
public String selectProtocol(List<String> protocols)
{
NextProtoNego.remove(sslSocket);
return protocols.get(0);
}
});
Server side typical usage:
final SSLSocket sslSocket = ...;
NextProtoNego.put(sslSocket, new NextProtoNego.ServerProvider()
{
@Override
public void unsupported()
{
NextProtoNego.remove(sslSocket);
}
@Override
public List<String> protocols()
{
return Arrays.asList("http/1.1");
}
@Override
public void protocolSelected(String protocol)
{
NextProtoNego.remove(sslSocket);
System.out.println("Protocol Selected is: " + protocol);
}
});
The implementation must make sure to remove the SSLSocket or SSLEngine
instances when the NPN interaction is completed.
In order to help application development, you can set the NextProtoNego#debug field
to true to have debug code printed to System#err.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | NextProtoNego.ClientProvider
The client-side provider interface that applications must implement to interact with the negotiation of the next protocol. |
| public static interface | NextProtoNego.Provider
Base, empty, interface for providers. |
| public static interface | NextProtoNego.ServerProvider
The server-side provider interface that applications must implement to interact with the negotiation of the next protocol. |
| Modifier and Type | Field and Description |
|---|---|
| public static boolean | debug
Enables debug logging on |
| private static final Map |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NextProtoNego. | Returns: the provider registered with the given socketa socket registered with socket)put(SSLSocket, Provider)
|
| public static NextProtoNego. | Returns: the provider registered with the given enginean engine registered with engine)put(SSLEngine, Provider)
|
| public static void | put(SSLSocket
the socket to register with the provider socket, NextProtoNego.the provider to register with the socket providerRegisters a SSLSocket with a provider. |
| public static void | put(SSLEngine
the engine to register with the provider engine, NextProtoNego.the provider to register with the engine providerRegisters a SSLEngine with a provider. |
| public static NextProtoNego. | |
| public static NextProtoNego. |
| debug | back to summary |
|---|---|
| public static boolean debug Enables debug logging on | |
| objects | back to summary |
|---|---|
| private static final Map<Object, NextProtoNego. | |
| NextProtoNego | back to summary |
|---|---|
| private NextProtoNego() | |
| get | back to summary |
|---|---|
public static NextProtoNego.
| |
| get | back to summary |
|---|---|
public static NextProtoNego.
| |
| put | back to summary |
|---|---|
| public static void put(SSLSocket socket, NextProtoNego. Registers a SSLSocket with a provider.
| |
| put | back to summary |
|---|---|
| public static void put(SSLEngine engine, NextProtoNego. Registers a SSLEngine with a provider.
| |
| remove | back to summary |
|---|---|
| public static NextProtoNego. Unregisters the given SSLSocket.
| |
| remove | back to summary |
|---|---|
| public static NextProtoNego. Unregisters the given SSLEngine.
| |
The client-side provider interface that applications must implement to interact with the negotiation of the next protocol.
| Modifier and Type | Method and Description |
|---|---|
| public String | Returns: the protocol selected by the application, ornull
to indicate to the server that it was not possible to negotiate
a protocolthe protocols sent by the server protocols)Callback invoked to let the application select a protocol among the ones sent by the server. |
| public boolean | Returns: true to add the NPN extension, false otherwiseCallback invoked to let the implementation know whether an empty NPN extension should be added to a ClientHello SSL message. |
| public void |
| selectProtocol | back to summary |
|---|---|
| public String selectProtocol(List<String> protocols) Callback invoked to let the application select a protocol among the ones sent by the server. The implementation may return null to indicate to the server that it was not possible to negotiate a protocol. The implementation may throw an exception to indicate that the TLS handshake must be aborted. | |
| supports | back to summary |
|---|---|
| public boolean supports() Callback invoked to let the implementation know whether an empty NPN extension should be added to a ClientHello SSL message.
| |
| unsupported | back to summary |
|---|---|
| public void unsupported() Callback invoked to let the application know that the server does not support NPN. | |
Base, empty, interface for providers.
The server-side provider interface that applications must implement to interact with the negotiation of the next protocol.
| Modifier and Type | Method and Description |
|---|---|
| public List | Returns: the list of protocols, or null if no NPN extension should be sent to the clientCallback invoked to let the implementation know the list of protocols that should be added to an NPN extension in a ServerHello SSL message. |
| public void | protocolSelected(String
the selected protocol, or protocol)null if the client
could not negotiate a protocolCallback invoked to let the application know the protocol selected by the client. |
| public void |
| protocols | back to summary |
|---|---|
| public List Callback invoked to let the implementation know the list of protocols that should be added to an NPN extension in a ServerHello SSL message. This callback is invoked only if the client sent a NPN extension. | |
| protocolSelected | back to summary |
|---|---|
| public void protocolSelected(String protocol) Callback invoked to let the application know the protocol selected by the client. The protocol selected may be The implementation may throw an exception to indicate that the TLS handshake must be aborted.
| |
| unsupported | back to summary |
|---|---|
| public void unsupported() Callback invoked to let the application know that the client does not support NPN. | |