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 // ========================================================================
19 package org.eclipse.jetty.io.ssl;
21 import java.io.IOException;
23 import java.util.concurrent.Executor;
25 import javax.net.ssl.SSLEngine;
27 import org.eclipse.jetty.io.ByteBufferPool;
28 import org.eclipse.jetty.io.ClientConnectionFactory;
29 import org.eclipse.jetty.io.EndPoint;
30 import org.eclipse.jetty.util.ssl.SslContextFactory;
32 public class SslClientConnectionFactory implements ClientConnectionFactory
34 public static final String SSL_PEER_HOST_CONTEXT_KEY = "ssl.peer.host";
35 public static final String SSL_PEER_PORT_CONTEXT_KEY = "ssl.peer.port";
36 public static final String SSL_ENGINE_CONTEXT_KEY = "ssl.engine";
38 private final SslContextFactory sslContextFactory;
39 private final ByteBufferPool byteBufferPool;
40 private final Executor executor;
41 private final ClientConnectionFactory connectionFactory;
43 public SslClientConnectionFactory(SslContextFactory sslContextFactory, ByteBufferPool byteBufferPool, Executor executor, ClientConnectionFactory connectionFactory)
45 this.sslContextFactory = sslContextFactory;
46 this.byteBufferPool = byteBufferPool;
47 this.executor = executor;
48 this.connectionFactory = connectionFactory;
52 public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
54 String host = (String)context.get(SSL_PEER_HOST_CONTEXT_KEY);
55 int port = (Integer)context.get(SSL_PEER_PORT_CONTEXT_KEY);
56 SSLEngine engine = sslContextFactory.newSSLEngine(host, port);
57 engine.setUseClientMode(true);
58 context.put(SSL_ENGINE_CONTEXT_KEY, engine);
60 SslConnection sslConnection = newSslConnection(byteBufferPool, executor, endPoint, engine);
61 sslConnection.setRenegotiationAllowed(sslContextFactory.isRenegotiationAllowed());
62 endPoint.setConnection(sslConnection);
63 EndPoint appEndPoint = sslConnection.getDecryptedEndPoint();
64 appEndPoint.setConnection(connectionFactory.newConnection(appEndPoint, context));
69 protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine)
71 return new SslConnection(byteBufferPool, executor, endPoint, engine);