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  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  }