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.spdy.client;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.concurrent.Executor;
24  import javax.net.ssl.SSLEngine;
25  
26  import org.eclipse.jetty.io.ClientConnectionFactory;
27  import org.eclipse.jetty.io.EndPoint;
28  import org.eclipse.jetty.io.NegotiatingClientConnection;
29  import org.eclipse.jetty.npn.NextProtoNego;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  
33  public class NPNClientConnection extends NegotiatingClientConnection implements NextProtoNego.ClientProvider
34  {
35      private static final Logger LOG = Log.getLogger(NPNClientConnection.class);
36  
37      private final String protocol;
38  
39      public NPNClientConnection(EndPoint endPoint, Executor executor, ClientConnectionFactory connectionFactory, SSLEngine sslEngine, Map<String, Object> context, String protocol)
40      {
41          super(endPoint, executor, sslEngine, connectionFactory, context);
42          this.protocol = protocol;
43          NextProtoNego.put(sslEngine, this);
44      }
45  
46      @Override
47      public boolean supports()
48      {
49          return true;
50      }
51  
52      @Override
53      public void unsupported()
54      {
55          NextProtoNego.remove(getSSLEngine());
56          completed();
57      }
58  
59      @Override
60      public String selectProtocol(List<String> protocols)
61      {
62          if (protocols.contains(protocol))
63          {
64              NextProtoNego.remove(getSSLEngine());
65              completed();
66              return protocol;
67          }
68          else
69          {
70              LOG.info("Could not negotiate protocol: server {} - client {}", protocols, protocol);
71              close();
72              return null;
73          }
74      }
75  
76      @Override
77      public void close()
78      {
79          NextProtoNego.remove(getSSLEngine());
80          super.close();
81      }
82  }