View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net.ssl;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.Socket;
19  import java.net.UnknownHostException;
20  
21  import javax.net.SocketFactory;
22  import javax.net.ssl.SSLParameters;
23  import javax.net.ssl.SSLSocket;
24  import javax.net.ssl.SSLSocketFactory;
25  
26  /**
27   * An {@link SSLSocketFactory} that configures SSL parameters 
28   * (those covered by {@link SSLParameters}) on each newly created socket. 
29   * <p>
30   * When any of this factory's {@code createSocket} methods are invoked, it
31   * calls on a {@link SSLSocketFactory} delegate to create the socket, and 
32   * then sets the SSL parameters of the socket (using the provided 
33   * configuration) before returning the socket to the caller.
34   *
35   * @author Carl Harris
36   */
37  public class ConfigurableSSLSocketFactory extends SocketFactory {
38  
39      private final SSLParametersConfiguration parameters;
40      private final SSLSocketFactory delegate;
41  
42      /**
43       * Creates a new factory.
44       * @param parameters parameters that will be configured on each
45       *    socket created by the factory
46       * @param delegate socket factory that will be called upon to create
47       *    sockets before configuration
48       */
49      public ConfigurableSSLSocketFactory(SSLParametersConfiguration parameters, SSLSocketFactory delegate) {
50          this.parameters = parameters;
51          this.delegate = delegate;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
59          SSLSocket socket = (SSLSocket) delegate.createSocket(address, port, localAddress, localPort);
60          parameters.configure(new SSLConfigurableSocket(socket));
61          return socket;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public Socket createSocket(InetAddress host, int port) throws IOException {
69          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port);
70          parameters.configure(new SSLConfigurableSocket(socket));
71          return socket;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
79          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port, localHost, localPort);
80          parameters.configure(new SSLConfigurableSocket(socket));
81          return socket;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
89          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port);
90          parameters.configure(new SSLConfigurableSocket(socket));
91          return socket;
92      }
93  
94  }