1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.io.ssl;
20
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.concurrent.Executor;
24
25 import javax.net.ssl.SSLEngine;
26
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;
31
32 public class SslClientConnectionFactory implements ClientConnectionFactory
33 {
34 public static final String SSL_CONTEXT_FACTORY_CONTEXT_KEY = "ssl.context.factory";
35 public static final String SSL_PEER_HOST_CONTEXT_KEY = "ssl.peer.host";
36 public static final String SSL_PEER_PORT_CONTEXT_KEY = "ssl.peer.port";
37 public static final String SSL_ENGINE_CONTEXT_KEY = "ssl.engine";
38
39 private final SslContextFactory sslContextFactory;
40 private final ByteBufferPool byteBufferPool;
41 private final Executor executor;
42 private final ClientConnectionFactory connectionFactory;
43
44 public SslClientConnectionFactory(SslContextFactory sslContextFactory, ByteBufferPool byteBufferPool, Executor executor, ClientConnectionFactory connectionFactory)
45 {
46 this.sslContextFactory = sslContextFactory;
47 this.byteBufferPool = byteBufferPool;
48 this.executor = executor;
49 this.connectionFactory = connectionFactory;
50 }
51
52 @Override
53 public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
54 {
55 String host = (String)context.get(SSL_PEER_HOST_CONTEXT_KEY);
56 int port = (Integer)context.get(SSL_PEER_PORT_CONTEXT_KEY);
57 SSLEngine engine = sslContextFactory.newSSLEngine(host, port);
58 engine.setUseClientMode(true);
59 context.put(SSL_ENGINE_CONTEXT_KEY, engine);
60
61 SslConnection sslConnection = newSslConnection(byteBufferPool, executor, endPoint, engine);
62 sslConnection.setRenegotiationAllowed(sslContextFactory.isRenegotiationAllowed());
63 endPoint.setConnection(sslConnection);
64 EndPoint appEndPoint = sslConnection.getDecryptedEndPoint();
65 appEndPoint.setConnection(connectionFactory.newConnection(appEndPoint, context));
66
67 return sslConnection;
68 }
69
70 protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine)
71 {
72 return new SslConnection(byteBufferPool, executor, endPoint, engine);
73 }
74 }