CompositeSSLServerSocketFactory.java
/*
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.altindag.ssl.socket;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Objects;
/**
* <strong>NOTE:</strong>
* Please don't use this class directly as it is part of the internal API. Class name and methods can be changed any time.
* Instead use the {@link nl.altindag.ssl.util.SSLSocketUtils SSLSocketUtils} which provides the same functionality
* while it has a stable API because it is part of the public API.
*
* @author Hakan Altindag
*/
public final class CompositeSSLServerSocketFactory extends SSLServerSocketFactory {
private final SSLServerSocketFactory sslServerSocketFactory;
private final SSLParameters sslParameters;
public CompositeSSLServerSocketFactory(SSLServerSocketFactory sslServerSocketFactory, SSLParameters sslParameters) {
this.sslServerSocketFactory = Objects.requireNonNull(sslServerSocketFactory);
this.sslParameters = Objects.requireNonNull(sslParameters);
}
@Override
public String[] getDefaultCipherSuites() {
return sslParameters.getCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return sslParameters.getCipherSuites();
}
@Override
public ServerSocket createServerSocket() throws IOException {
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket();
return withSslParameters(serverSocket);
}
@Override
public ServerSocket createServerSocket(int port) throws IOException {
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(port);
return withSslParameters(serverSocket);
}
@Override
public ServerSocket createServerSocket(int port, int backlog) throws IOException {
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(port, backlog);
return withSslParameters(serverSocket);
}
@Override
public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(port, backlog, ifAddress);
return withSslParameters(serverSocket);
}
private ServerSocket withSslParameters(ServerSocket socket) {
if (socket instanceof SSLServerSocket) {
SSLServerSocket sslSocket = (SSLServerSocket) socket;
sslSocket.setSSLParameters(sslParameters);
}
return socket;
}
}