org.eclipse.jetty.alpn.ALPN Maven / Gradle / Ivy
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.alpn;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
/**
* {@link 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 {@link SSLSocket} or {@link SSLEngine} with a
* {@link ClientProvider} or with a {@link 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 {@link SSLSocket} or {@link 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 {@link ALPN#debug} field
* to {@code true} to have debug code printed to {@link System#err}.
*/
public class ALPN
{
/**
* Flag that enables printing of debug statements to {@link System#err}.
*/
public static boolean debug = false;
private static Map