View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
20  package org.eclipse.jetty.spdy.server;
21  
22  import java.util.Arrays;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.net.ssl.SSLEngine;
27  
28  import org.eclipse.jetty.io.Connection;
29  import org.eclipse.jetty.io.EndPoint;
30  import org.eclipse.jetty.io.ssl.SslConnection;
31  import org.eclipse.jetty.npn.NextProtoNego;
32  import org.eclipse.jetty.server.AbstractConnectionFactory;
33  import org.eclipse.jetty.server.Connector;
34  import org.eclipse.jetty.util.annotation.Name;
35  import org.eclipse.jetty.util.log.Log;
36  import org.eclipse.jetty.util.log.Logger;
37  
38  public class NPNServerConnectionFactory extends AbstractConnectionFactory
39  {
40      private static final Logger LOG = Log.getLogger(NPNServerConnectionFactory.class);
41      private final List<String> _protocols;
42      private String _defaultProtocol;
43  
44      /* ------------------------------------------------------------ */
45      /**
46       * @param protocols List of supported protocols in priority order
47       */
48      public NPNServerConnectionFactory(@Name("protocols")String... protocols)
49      {
50          super("npn");
51          _protocols=Arrays.asList(protocols);
52  
53          try
54          {
55              if (NextProtoNego.class.getClassLoader()!=null)
56              {
57                  LOG.warn("NextProtoNego not from bootloader classloader: "+NextProtoNego.class.getClassLoader());
58                  throw new IllegalStateException("NextProtoNego not on bootloader");
59              }
60          }
61          catch(Throwable th)
62          {
63              LOG.warn("NextProtoNego not available: "+th);
64              throw new IllegalStateException("NextProtoNego not available",th);
65          }
66      }
67  
68      public String getDefaultProtocol()
69      {
70          return _defaultProtocol;
71      }
72  
73      public void setDefaultProtocol(String defaultProtocol)
74      {
75          _defaultProtocol = defaultProtocol;
76      }
77  
78      public List<String> getProtocols()
79      {
80          return _protocols;
81      }
82  
83      @Override
84      public Connection newConnection(Connector connector, EndPoint endPoint)
85      {
86          List<String> protocols=_protocols;
87          if (protocols==null || protocols.size()==0)
88          {
89              protocols=connector.getProtocols();
90              for (Iterator<String> i=protocols.iterator();i.hasNext();)
91              {
92                  String protocol=i.next();
93                  if (protocol.startsWith("SSL-")||protocol.equals("NPN"))
94                      i.remove();
95              }
96          }
97  
98          String dft=_defaultProtocol;
99          if (dft==null)
100             dft=_protocols.get(0);
101         
102         SSLEngine engine=null;
103         EndPoint ep=endPoint;
104         while(engine==null && ep!=null)
105         {
106             // TODO make more generic
107             if (ep instanceof SslConnection.DecryptedEndPoint)
108                 engine=((SslConnection.DecryptedEndPoint)ep).getSslConnection().getSSLEngine();
109             else
110                 ep=null;
111         }
112 
113         return configure(new NextProtoNegoServerConnection(endPoint, engine, connector,protocols,_defaultProtocol),connector,endPoint);
114     }
115 
116     @Override
117     public String toString()
118     {
119         return String.format("%s@%x{%s,%s,%s}",this.getClass().getSimpleName(),hashCode(),getProtocol(),getDefaultProtocol(),getProtocols());
120     }
121 }