View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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_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";
37  
38      private final SslContextFactory sslContextFactory;
39      private final ByteBufferPool byteBufferPool;
40      private final Executor executor;
41      private final ClientConnectionFactory connectionFactory;
42  
43      public SslClientConnectionFactory(SslContextFactory sslContextFactory, ByteBufferPool byteBufferPool, Executor executor, ClientConnectionFactory connectionFactory)
44      {
45          this.sslContextFactory = sslContextFactory;
46          this.byteBufferPool = byteBufferPool;
47          this.executor = executor;
48          this.connectionFactory = connectionFactory;
49      }
50  
51      @Override
52      public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
53      {
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);
59  
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));
65  
66          return sslConnection;
67      }
68  
69      protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine)
70      {
71          return new SslConnection(byteBufferPool, executor, endPoint, engine);
72      }
73  }