View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.server;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import javax.net.ssl.SSLEngine;
24  
25  import org.eclipse.jetty.alpn.ALPN;
26  import org.eclipse.jetty.io.EndPoint;
27  import org.eclipse.jetty.server.Connector;
28  import org.eclipse.jetty.server.NegotiatingServerConnection;
29  import org.eclipse.jetty.util.log.Log;
30  import org.eclipse.jetty.util.log.Logger;
31  
32  public class ALPNServerConnection extends NegotiatingServerConnection implements ALPN.ServerProvider
33  {
34      private static final Logger LOG = Log.getLogger(ALPNServerConnection.class);
35  
36      public ALPNServerConnection(Connector connector, EndPoint endPoint, SSLEngine engine, List<String> protocols, String defaultProtocol)
37      {
38          super(connector, endPoint, engine, protocols, defaultProtocol);
39          ALPN.put(engine, this);
40      }
41  
42      @Override
43      public void unsupported()
44      {
45          select(Collections.<String>emptyList());
46      }
47  
48      @Override
49      public String select(List<String> clientProtocols)
50      {
51          List<String> serverProtocols = getProtocols();
52          String negotiated = null;
53  
54          // RFC 7301 states that the server picks the protocol
55          // that it prefers that is also supported by the client.
56          for (String serverProtocol : serverProtocols)
57          {
58              if (clientProtocols.contains(serverProtocol))
59              {
60                  negotiated = serverProtocol;
61                  break;
62              }
63          }
64  
65          if (negotiated == null)
66          {
67              negotiated = getDefaultProtocol();
68          }
69          if (LOG.isDebugEnabled())
70              LOG.debug("{} protocol selected {}", this, negotiated);
71          setProtocol(negotiated);
72          ALPN.remove(getSSLEngine());
73          return negotiated;
74      }
75  
76      @Override
77      public void close()
78      {
79          ALPN.remove(getSSLEngine());
80          super.close();
81      }
82  }