View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.Arrays;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.concurrent.Executor;
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 String protocol;
39  
40      public ALPNClientConnection(EndPoint endPoint, Executor executor, ClientConnectionFactory connectionFactory, SSLEngine sslEngine, Map<String, Object> context, String protocol)
41      {
42          super(endPoint, executor, sslEngine, connectionFactory, context);
43          this.protocol = protocol;
44          ALPN.put(sslEngine, this);
45      }
46  
47      @Override
48      public boolean supports()
49      {
50          return true;
51      }
52  
53      @Override
54      public void unsupported()
55      {
56          ALPN.remove(getSSLEngine());
57          completed();
58      }
59  
60      @Override
61      public List<String> protocols()
62      {
63          return Arrays.asList(protocol);
64      }
65  
66      @Override
67      public void selected(String protocol)
68      {
69          if (this.protocol.equals(protocol))
70          {
71              ALPN.remove(getSSLEngine());
72              completed();
73          }
74          else
75          {
76              LOG.info("Could not negotiate protocol: server {} - client {}", protocol, this.protocol);
77              close();
78          }
79      }
80  
81      @Override
82      public void close()
83      {
84          ALPN.remove(getSSLEngine());
85          super.close();
86      }
87  }