Example usage for javax.net.ssl SSLSocket setEnabledCipherSuites

List of usage examples for javax.net.ssl SSLSocket setEnabledCipherSuites

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket setEnabledCipherSuites.

Prototype

public abstract void setEnabledCipherSuites(String suites[]);

Source Link

Document

Sets the cipher suites enabled for use on this connection.

Usage

From source file:org.wso2.carbon.databridge.agent.endpoint.binary.BinarySecureClientPoolFactory.java

@Override
public Object createClient(String protocol, String hostName, int port)
        throws DataEndpointException, DataEndpointSecurityException, DataEndpointAgentConfigurationException {
    if (protocol.equalsIgnoreCase(DataEndpointConfiguration.Protocol.SSL.toString())) {
        int timeout = AgentHolder.getInstance()
                .getDataEndpointAgent(DataEndpointConstants.BINARY_DATA_AGENT_TYPE).getAgentConfiguration()
                .getSocketTimeoutMS();// w  ww .j av a 2  s  .  c  om
        String sslProtocols = AgentHolder.getInstance()
                .getDataEndpointAgent(DataEndpointConstants.BINARY_DATA_AGENT_TYPE).getAgentConfiguration()
                .getSslEnabledProtocols();
        String ciphers = AgentHolder.getInstance()
                .getDataEndpointAgent(DataEndpointConstants.BINARY_DATA_AGENT_TYPE).getAgentConfiguration()
                .getCiphers();

        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslSocket = (SSLSocket) sslsocketfactory.createSocket(hostName, port);
            sslSocket.setSoTimeout(timeout);

            if (sslProtocols != null && sslProtocols.length() != 0) {
                String[] sslProtocolsArray = sslProtocols.split(",");
                sslSocket.setEnabledProtocols(sslProtocolsArray);
            }

            if (ciphers != null && ciphers.length() != 0) {
                String[] ciphersArray = ciphers.split(",");
                sslSocket.setEnabledCipherSuites(ciphersArray);
            } else {
                sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
            }
            return sslSocket;
        } catch (IOException e) {
            throw new DataEndpointException(
                    "Error while opening socket to " + hostName + ":" + port + ". " + e.getMessage(), e);
        }
    } else {
        throw new DataEndpointException("Unsupported protocol: " + protocol + ". Currently only "
                + DataEndpointConfiguration.Protocol.SSL.toString() + " supported.");
    }
}

From source file:processing.app.debug.EasySSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>/*from   w  ww.  ja v  a  2  s  .c  o  m*/
 * To circumvent the limitations of older JREs that do not support connect timeout a
 * controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *
 * @param host       the host name/IP
 * @param port       the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params     {@link HttpConnectionParams Http connection parameters}
 * @return Socket a new socket
 * @throws IOException          if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 *                              determined
 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    Socket socket;
    if (timeout == 0) {
        socket = socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }

    SSLSocket sslSocket = (SSLSocket) socket;
    sslSocket.setEnabledProtocols(SSL_PROTOCOLS);
    sslSocket.setEnabledCipherSuites(SSL_CYPHER_SUITES);

    return socket;
}