/*
 * Copyright 2019 Mitsunori Komatsu (komamitsu)
 *
 * 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
 *
 *      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.komamitsu.fluency.fluentd.ingester.sender;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class SSLSocketBuilder
{
    private static final String SSL_PROTOCOL = "TLSv1.2";
    private final String host;
    private final int port;
    private final int connectionTimeoutMilli;
    private final int readTimeoutMilli;

    public SSLSocketBuilder(String host, Integer port, int connectionTimeoutMilli, int readTimeoutMilli)
    {
        this.host = host;
        this.port = port;
        this.connectionTimeoutMilli = connectionTimeoutMilli;
        this.readTimeoutMilli = readTimeoutMilli;
    }

    public SSLSocket build()
            throws IOException
    {
        try {
            SSLContext sslContext = SSLContext.getInstance(SSL_PROTOCOL);
            sslContext.init(null, null, new SecureRandom());
            javax.net.ssl.SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            Socket socket = new Socket();
            try {
                socket.connect(new InetSocketAddress(host, port), connectionTimeoutMilli);
                socket.setTcpNoDelay(true);
                socket.setSoTimeout(readTimeoutMilli);

                return (SSLSocket) socketFactory.createSocket(socket, host, port, true);
            }
            catch (Throwable e) {
                socket.close();
                throw e;
            }
        }
        catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("Failed to get SSLContext", e);
        }
        catch (KeyManagementException e) {
            throw new IllegalStateException("Failed to init SSLContext", e);
        }
    }

    @Override
    public String toString()
    {
        return "SSLSocketBuilder{" +
                "host='" + host + '\'' +
                ", port=" + port +
                '}';
    }
}

@@ -688,22 +688,21 @@
 .ssl.SSL
-Contex
+Socke
 t;%0Aimpor
@@ -726,16 +726,23 @@
 SLSocket
+Factory
 ;%0A%0Aimpor
@@ -827,225 +827,40 @@
 et;%0A
-import java.security.KeyManagementException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.SecureRandom;%0A%0Apublic class SSLSocketBuilder%0A%7B%0A    private static final String SSL_PROTOCOL = %22TLSv1.2%22;
+%0Apublic class SSLSocketBuilder%0A%7B
 %0A   
@@ -1362,271 +1362,60 @@
     
-try %7B%0A            SSLContext sslContext = SSLContext.getInstance(SSL_PROTOCOL);%0A            sslContext.init(null, null, new SecureRandom());%0A            javax.net.ssl.SSLSocketFactory socketFactory = sslContext.getSocketFactory();%0A            Socket socket = new 
+Socket socket = SSLSocketFactory.getDefault().create
 Sock
@@ -1432,22 +1432,14 @@
     
-  
-  
 try %7B%0A
-    
     
@@ -1525,36 +1525,32 @@
 i);%0A            
-    
 socket.setTcpNoD
@@ -1565,36 +1565,32 @@
 e);%0A            
-  
-  
 socket.setSoTime
@@ -1616,102 +1616,8 @@
 i);%0A
-%0A                return (SSLSocket) socketFactory.createSocket(socket, host, port, true);%0A    
     
@@ -1618,28 +1618,24 @@
 ;%0A        %7D%0A
-    
         catc
@@ -1648,28 +1648,24 @@
 owable e) %7B%0A
-    
             
@@ -1688,28 +1688,24 @@
             
-    
 throw e;%0A   
@@ -1701,20 +1701,16 @@
 hrow e;%0A
-    
         
@@ -1723,270 +1723,34 @@
     
-%7D%0A        catch (NoSuchAlgorithmException e) %7B%0A            throw new IllegalStateException(%22Failed to get SSLContext%22, e);%0A        %7D%0A        catch (KeyManagementException e) %7B%0A            throw new IllegalStateException(%22Failed to init SSLContext%22, e);%0A        %7D
+return (SSLSocket) socket;
 %0A   

Use simpler impl to support mutual TLS