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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
20 package org.eclipse.jetty.server;
23 import javax.net.ssl.SSLEngine;
24 import javax.net.ssl.SSLSession;
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;
33 public class SslConnectionFactory extends AbstractConnectionFactory
35 private final SslContextFactory _sslContextFactory;
36 private final String _nextProtocol;
38 public SslConnectionFactory()
40 this(HttpVersion.HTTP_1_1.asString());
43 public SslConnectionFactory(@Name("next") String nextProtocol)
45 this(null,nextProtocol);
48 public SslConnectionFactory(@Name("sslContextFactory") SslContextFactory factory, @Name("next") String nextProtocol)
50 super("SSL-"+nextProtocol);
51 _sslContextFactory=factory==null?new SslContextFactory():factory;
52 _nextProtocol=nextProtocol;
53 addBean(_sslContextFactory);
56 public SslContextFactory getSslContextFactory()
58 return _sslContextFactory;
62 protected void doStart() throws Exception
66 SSLEngine engine = _sslContextFactory.newSSLEngine();
67 engine.setUseClientMode(false);
68 SSLSession session=engine.getSession();
70 if (session.getPacketBufferSize()>getInputBufferSize())
71 setInputBufferSize(session.getPacketBufferSize());
75 public Connection newConnection(Connector connector, EndPoint endPoint)
77 SSLEngine engine = _sslContextFactory.newSSLEngine(endPoint.getRemoteAddress());
78 engine.setUseClientMode(false);
80 SslConnection sslConnection = newSslConnection(connector, endPoint, engine);
81 sslConnection.setRenegotiationAllowed(_sslContextFactory.isRenegotiationAllowed());
82 configure(sslConnection, connector, endPoint);
84 ConnectionFactory next = connector.getConnectionFactory(_nextProtocol);
85 EndPoint decryptedEndPoint = sslConnection.getDecryptedEndPoint();
86 Connection connection = next.newConnection(connector, decryptedEndPoint);
87 decryptedEndPoint.setConnection(connection);
92 protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine)
94 return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine);
98 public String toString()
100 return String.format("%s@%x{%s}",this.getClass().getSimpleName(),hashCode(),getProtocol());