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.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          for (String clientProtocol : clientProtocols)
54          {
55              if (serverProtocols.contains(clientProtocol))
56              {
57                  negotiated = clientProtocol;
58                  break;
59              }
60          }
61          if (negotiated == null)
62          {
63              negotiated = getDefaultProtocol();
64          }
65          if (LOG.isDebugEnabled())
66              LOG.debug("{} protocol selected {}", this, negotiated);
67          setProtocol(negotiated);
68          ALPN.remove(getSSLEngine());
69          return negotiated;
70      }
71  
72      @Override
73      public void close()
74      {
75          ALPN.remove(getSSLEngine());
76          super.close();
77      }
78  }