001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.net.ssl; 015 016import java.io.IOException; 017import java.net.InetAddress; 018import java.net.Socket; 019import java.net.UnknownHostException; 020 021import javax.net.SocketFactory; 022import javax.net.ssl.SSLParameters; 023import javax.net.ssl.SSLSocket; 024import javax.net.ssl.SSLSocketFactory; 025 026/** 027 * An {@link SSLSocketFactory} that configures SSL parameters (those covered by 028 * {@link SSLParameters}) on each newly created socket. 029 * <p> 030 * When any of this factory's {@code createSocket} methods are invoked, it calls 031 * on a {@link SSLSocketFactory} delegate to create the socket, and then sets 032 * the SSL parameters of the socket (using the provided configuration) before 033 * returning the socket to the caller. 034 * 035 * @author Carl Harris 036 */ 037public class ConfigurableSSLSocketFactory extends SocketFactory { 038 039 private final SSLParametersConfiguration parameters; 040 private final SSLSocketFactory delegate; 041 042 /** 043 * Creates a new factory. 044 * 045 * @param parameters parameters that will be configured on each socket created 046 * by the factory 047 * @param delegate socket factory that will be called upon to create sockets 048 * before configuration 049 */ 050 public ConfigurableSSLSocketFactory(SSLParametersConfiguration parameters, SSLSocketFactory delegate) { 051 this.parameters = parameters; 052 this.delegate = delegate; 053 } 054 055 /** 056 * {@inheritDoc} 057 */ 058 @Override 059 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) 060 throws IOException { 061 SSLSocket socket = (SSLSocket) delegate.createSocket(address, port, localAddress, localPort); 062 parameters.configure(new SSLConfigurableSocket(socket)); 063 return socket; 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public Socket createSocket(InetAddress host, int port) throws IOException { 071 SSLSocket socket = (SSLSocket) delegate.createSocket(host, port); 072 parameters.configure(new SSLConfigurableSocket(socket)); 073 return socket; 074 } 075 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) 081 throws IOException, UnknownHostException { 082 SSLSocket socket = (SSLSocket) delegate.createSocket(host, port, localHost, localPort); 083 parameters.configure(new SSLConfigurableSocket(socket)); 084 return socket; 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 @Override 091 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 092 SSLSocket socket = (SSLSocket) delegate.createSocket(host, port); 093 parameters.configure(new SSLConfigurableSocket(socket)); 094 return socket; 095 } 096 097}