Skip to content

Instantly share code, notes, and snippets.

@serac
Created May 22, 2012 18:28
Show Gist options
  • Select an option

  • Save serac/2770776 to your computer and use it in GitHub Desktop.

Select an option

Save serac/2770776 to your computer and use it in GitHub Desktop.
One Strategy for Exposing Socket Configuration in Jasig Java CAS Client
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you 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:
*
* http://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 org.jasig.cas.client.ssl;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.*;
import java.util.Map;
/**
* Wraps a {@link SSLContext} and configures {@link SSLSocket}s produced by
* {@link javax.net.ssl.SSLContext#getSocketFactory()}.
*
* @author Marvin S. Addison
* @version $Revision: $
*/
public class WrappedSSLSocketFactory extends SSLSocketFactory {
private final SSLContext sslContext;
private SSLParameters sslParameters;
private Map<Integer, Object> socketOptions;
public WrappedSSLSocketFactory(final SSLContext context) {
if (context == null) {
throw new IllegalArgumentException("SSLContext cannot be null");
}
this.sslContext = context;
}
public void setSSLParameters(final SSLParameters params) {
this.sslParameters = params;
}
public void setSocketOptions(final Map<Integer, Object> options) {
this.socketOptions = options;
}
@Override
public String[] getDefaultCipherSuites() {
return sslContext.getSocketFactory().getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return sslContext.getSocketFactory().getSupportedCipherSuites();
}
@Override
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoclose) throws IOException {
return configureSocket((SSLSocket) sslContext.getSocketFactory().createSocket(socket, host, port, autoclose));
}
@Override
public Socket createSocket(final String host, final int port) throws IOException, UnknownHostException {
return configureSocket((SSLSocket) sslContext.getSocketFactory().createSocket(host, port));
}
@Override
public Socket createSocket(final InetAddress address, final int port) throws IOException {
return configureSocket((SSLSocket) sslContext.getSocketFactory().createSocket(address, port));
}
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
return configureSocket((SSLSocket) sslContext.getSocketFactory().createSocket(host, port, localAddress, localPort));
}
@Override
public Socket createSocket(final InetAddress address, final int port, final InetAddress remoteAddress, final int remotePort) throws IOException {
return configureSocket((SSLSocket) sslContext.getSocketFactory().createSocket(address, port, remoteAddress, remotePort));
}
protected SSLSocket configureSocket(final SSLSocket socket) throws SocketException {
if (sslParameters != null) {
socket.setSSLParameters(sslParameters);
}
Object value;
if (socketOptions != null) {
for (int optId : socketOptions.keySet()) {
value = socketOptions.get(optId);
switch (optId) {
case SocketOptions.IP_TOS:
socket.setTrafficClass((Integer) value);
break;
case SocketOptions.SO_KEEPALIVE:
socket.setKeepAlive((Boolean) value);
break;
case SocketOptions.SO_LINGER:
socket.setSoLinger(true, (Integer) value);
break;
case SocketOptions.SO_OOBINLINE:
socket.setOOBInline((Boolean) value);
break;
case SocketOptions.SO_RCVBUF:
socket.setReceiveBufferSize((Integer) value);
break;
case SocketOptions.SO_REUSEADDR:
socket.setReuseAddress((Boolean) value);
break;
case SocketOptions.SO_SNDBUF:
socket.setSendBufferSize((Integer) value);
break;
case SocketOptions.SO_TIMEOUT:
socket.setSoTimeout((Integer) value);
break;
case SocketOptions.TCP_NODELAY:
socket.setTcpNoDelay((Boolean) value);
break;
}
}
}
return socket;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment