View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.alpn.client;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.concurrent.Executor;
24  
25  import javax.net.ssl.SSLEngine;
26  
27  import org.eclipse.jetty.alpn.ALPN;
28  import org.eclipse.jetty.io.ClientConnectionFactory;
29  import org.eclipse.jetty.io.EndPoint;
30  import org.eclipse.jetty.io.NegotiatingClientConnection;
31  import org.eclipse.jetty.util.log.Log;
32  import org.eclipse.jetty.util.log.Logger;
33  
34  public class ALPNClientConnection extends NegotiatingClientConnection implements ALPN.ClientProvider
35  {
36      private static final Logger LOG = Log.getLogger(ALPNClientConnection.class);
37  
38      private final List<String> protocols;
39  
40      public ALPNClientConnection(EndPoint endPoint, Executor executor, ClientConnectionFactory connectionFactory, SSLEngine sslEngine, Map<String, Object> context, List<String> protocols)
41      {
42          super(endPoint, executor, sslEngine, connectionFactory, context);
43          this.protocols = protocols;
44          ALPN.put(sslEngine, this);
45      }
46  
47      @Override
48      public void unsupported()
49      {
50          ALPN.remove(getSSLEngine());
51          completed();
52      }
53  
54      @Override
55      public List<String> protocols()
56      {
57          return protocols;
58      }
59  
60      @Override
61      public void selected(String protocol)
62      {
63          if (protocols.contains(protocol))
64          {
65              ALPN.remove(getSSLEngine());
66              completed();
67          }
68          else
69          {
70              LOG.info("Could not negotiate protocol: server [{}] - client {}", protocol, protocols);
71              close();
72          }
73      }
74  
75      @Override
76      public void close()
77      {
78          ALPN.remove(getSSLEngine());
79          super.close();
80      }
81  }