improve protocol enabling based on socket object type

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2015-02-05 15:48:21 +05:30
parent fabab5460c
commit d62d511f47
3 changed files with 59 additions and 25 deletions

View File

@ -189,22 +189,28 @@ public class NeutronRestApi {
@Override
public Socket createSocket(final String host, final int port) throws IOException {
SSLSocket s = (SSLSocket) ssf.createSocket(host, port);
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = ssf.createSocket(host, port);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
}
@Override
public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
SSLSocket s = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = ssf.createSocket(address, port, localAddress, localPort);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
}
@Override
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
SSLSocket s = (SSLSocket) ssf.createSocket(socket, host, port, autoClose);
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = ssf.createSocket(socket, host, port, autoClose);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
}
@ -213,10 +219,16 @@ public class NeutronRestApi {
UnknownHostException, ConnectTimeoutException {
int timeout = params.getConnectionTimeout();
if (timeout == 0) {
return createSocket(host, port, localAddress, localPort);
Socket s = createSocket(host, port, localAddress, localPort);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
} else {
SSLSocket s = (SSLSocket) ssf.createSocket();
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = ssf.createSocket();
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
s.bind(new InetSocketAddress(localAddress, localPort));
s.connect(new InetSocketAddress(host, port), timeout);
return s;

View File

@ -356,15 +356,19 @@ public class RESTServiceConnector {
@Override
public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
SSLSocket socket = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
Socket socket = ssf.createSocket(address, port, localAddress, localPort);
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
return socket;
}
@Override
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
SSLSocket s = (SSLSocket) ssf.createSocket(socket, host, port, autoClose);
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = ssf.createSocket(socket, host, port, autoClose);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
}
@ -373,10 +377,16 @@ public class RESTServiceConnector {
throws IOException, UnknownHostException, ConnectTimeoutException {
final int timeout = params.getConnectionTimeout();
if (timeout == 0) {
return createSocket(host, port, localAddress, localPort);
Socket socket = createSocket(host, port, localAddress, localPort);
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
return socket;
} else {
final SSLSocket s = (SSLSocket) ssf.createSocket();
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
final Socket s = ssf.createSocket();
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
s.bind(new InetSocketAddress(localAddress, localPort));
s.connect(new InetSocketAddress(host, port), timeout);
return s;

View File

@ -122,8 +122,10 @@ public class EasySSLProtocolSocketFactory implements ProtocolSocketFactory {
*/
@Override
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException {
SSLSocket socket = (SSLSocket) getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
Socket socket = getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
return socket;
}
@ -157,10 +159,16 @@ public class EasySSLProtocolSocketFactory implements ProtocolSocketFactory {
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
Socket socket = socketfactory.createSocket(host, port, localAddress, localPort);
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
return socket;
} else {
SSLSocket socket = (SSLSocket) socketfactory.createSocket();
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
Socket socket = socketfactory.createSocket();
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
@ -174,14 +182,18 @@ public class EasySSLProtocolSocketFactory implements ProtocolSocketFactory {
*/
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
SSLSocket socket = (SSLSocket) getSSLContext().getSocketFactory().createSocket(host, port);
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
Socket socket = (Socket) getSSLContext().getSocketFactory().createSocket(host, port);
if (socket instanceof SSLSocket) {
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
}
return socket;
}
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
SSLSocket s= (SSLSocket) getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
Socket s = getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
if (s instanceof SSLSocket) {
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
}
return s;
}