package com.redant.core.handler.ssl;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

/**
 * @author gris.wang
 * @since 2017/11/19
 **/
public class SslContextHelper {

    private static final String KEY_STORE_JKS = "JKS";

    private static final String ALGORITHM = "SunX509";

    /**
     * 获取SslContext
     * @param keyPath
     * @param keyPassword
     * @return
     */
    public static SslContext getSslContext(String keyPath,String keyPassword){
        if(keyPath==null || keyPath.trim().length()==0 || keyPassword==null || keyPassword.trim().length()==0){
            return null;
        }
        SslContext sslContext = null;
        try {
            KeyStore keyStore = KeyStore.getInstance(KEY_STORE_JKS);
            keyStore.load(new FileInputStream(keyPath), keyPassword.toCharArray());
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(ALGORITHM);
            keyManagerFactory.init(keyStore,keyPassword.toCharArray());
            sslContext = SslContextBuilder.forServer(keyManagerFactory).build();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        } catch (SSLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sslContext;
    }

}

@@ -31,16 +31,63 @@
 r.ssl;%0A%0A
+import com.xiaoleilu.hutool.crypto.SecureUtil;%0A
 import i
@@ -118,16 +118,16 @@
 ontext;%0A
-
 import i
@@ -552,16 +552,85 @@
 ception;
+%0Aimport java.util.Map;%0Aimport java.util.concurrent.ConcurrentHashMap;
 %0A%0A/**%0A *
@@ -815,24 +815,433 @@
 %22SunX509%22;%0A%0A
+    private static Map%3CString,SslContext%3E contents = new ConcurrentHashMap%3CString,SslContext%3E();%0A%0A    private static String getKey(String keyPath,String keyPassword)%7B%0A        if(keyPath==null %7C%7C keyPath.trim().length()==0 %7C%7C keyPassword==null %7C%7C keyPassword.trim().length()==0)%7B%0A            return null;%0A        %7D%0A        String keyStr = keyPath+%22&%22+keyPassword;%0A        return SecureUtil.md5(keyStr);%0A    %7D%0A%0A
     /**%0A    
@@ -1605,16 +1605,198 @@
   try %7B%0A
+            String key = getKey(keyPath,keyPassword);%0A            sslContext = contents.get(key);%0A            if(sslContext!=null)%7B%0A                return sslContext;%0A            %7D%0A%0A
         
@@ -1932,32 +1932,33 @@
 toCharArray());%0A
+%0A
             KeyM
@@ -2105,16 +2105,17 @@
 ray());%0A
+%0A
         
@@ -2183,24 +2183,118 @@
 y).build();%0A
+            if(sslContext!=null)%7B%0A                contents.put(key,sslContext);%0A            %7D%0A
         %7D ca

add cache when getSslContext