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 (those covered by
28   * {@link SSLParameters}) on each newly created socket.
29   * <p>
30   * When any of this factory's {@code createSocket} methods are invoked, it calls
31   * on a {@link SSLSocketFactory} delegate to create the socket, and then sets
32   * the SSL parameters of the socket (using the provided configuration) before
33   * 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       * 
45       * @param parameters parameters that will be configured on each socket created
46       *                   by the factory
47       * @param delegate   socket factory that will be called upon to create sockets
48       *                   before configuration
49       */
50      public ConfigurableSSLSocketFactory(SSLParametersConfiguration parameters, SSLSocketFactory delegate) {
51          this.parameters = parameters;
52          this.delegate = delegate;
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
60              throws IOException {
61          SSLSocket socket = (SSLSocket) delegate.createSocket(address, port, localAddress, localPort);
62          parameters.configure(new SSLConfigurableSocket(socket));
63          return socket;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public Socket createSocket(InetAddress host, int port) throws IOException {
71          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port);
72          parameters.configure(new SSLConfigurableSocket(socket));
73          return socket;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
81              throws IOException, UnknownHostException {
82          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port, localHost, localPort);
83          parameters.configure(new SSLConfigurableSocket(socket));
84          return socket;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
92          SSLSocket socket = (SSLSocket) delegate.createSocket(host, port);
93          parameters.configure(new SSLConfigurableSocket(socket));
94          return socket;
95      }
96  
97  }