View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.server;
21  
22  
23  import javax.net.ssl.SSLEngine;
24  import javax.net.ssl.SSLSession;
25  
26  import org.eclipse.jetty.http.HttpVersion;
27  import org.eclipse.jetty.io.Connection;
28  import org.eclipse.jetty.io.EndPoint;
29  import org.eclipse.jetty.io.ssl.SslConnection;
30  import org.eclipse.jetty.util.annotation.Name;
31  import org.eclipse.jetty.util.ssl.SslContextFactory;
32  
33  public class SslConnectionFactory extends AbstractConnectionFactory
34  {
35      private final SslContextFactory _sslContextFactory;
36      private final String _nextProtocol;
37  
38      public SslConnectionFactory()
39      {
40          this(HttpVersion.HTTP_1_1.asString());
41      }
42  
43      public SslConnectionFactory(@Name("next") String nextProtocol)
44      {
45          this(null,nextProtocol);
46      }
47  
48      public SslConnectionFactory(@Name("sslContextFactory") SslContextFactory factory, @Name("next") String nextProtocol)
49      {
50          super("SSL");
51          _sslContextFactory=factory==null?new SslContextFactory():factory;
52          _nextProtocol=nextProtocol;
53          addBean(_sslContextFactory);
54      }
55  
56      public SslContextFactory getSslContextFactory()
57      {
58          return _sslContextFactory;
59      }
60  
61      @Override
62      protected void doStart() throws Exception
63      {
64          super.doStart();
65  
66          SSLEngine engine = _sslContextFactory.newSSLEngine();
67          engine.setUseClientMode(false);
68          SSLSession session=engine.getSession();
69  
70          if (session.getPacketBufferSize()>getInputBufferSize())
71              setInputBufferSize(session.getPacketBufferSize());
72      }
73  
74      @Override
75      public Connection newConnection(Connector connector, EndPoint endPoint)
76      {
77          SSLEngine engine = _sslContextFactory.newSSLEngine(endPoint.getRemoteAddress());
78          engine.setUseClientMode(false);
79  
80          SslConnection sslConnection = newSslConnection(connector, endPoint, engine);
81          sslConnection.setRenegotiationAllowed(_sslContextFactory.isRenegotiationAllowed());
82          configure(sslConnection, connector, endPoint);
83  
84          ConnectionFactory next = connector.getConnectionFactory(_nextProtocol);
85          EndPoint decryptedEndPoint = sslConnection.getDecryptedEndPoint();
86          Connection connection = next.newConnection(connector, decryptedEndPoint);
87          decryptedEndPoint.setConnection(connection);
88  
89          return sslConnection;
90      }
91  
92      protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine)
93      {
94          return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine);
95      }
96  
97      @Override
98      public String toString()
99      {
100         return String.format("%s@%x{%s->%s}",this.getClass().getSimpleName(),hashCode(),getProtocol(),_nextProtocol);
101     }
102 
103 }