package io.reactivex.netty.pipeline.ssl;

import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import java.security.cert.CertificateException;

/**
 * @author Tomasz Bak
 */
public final class DefaultFactories {

    public static SSLEngineFactory SELF_SIGNED = new SelfSignedSSLEngineFactory();

    public static SSLEngineFactory TRUST_ALL = new TrustAllSSLEngineFactory();

    private DefaultFactories() {
    }

    private static class TrustAllSSLEngineFactory implements SSLEngineFactory {

        private final SslContext sslCtx;

        private TrustAllSSLEngineFactory() {
            try {
                sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
            } catch (SSLException e) {
                throw new IllegalStateException("Failed to create Netty's Ssl context with InsecureTrustManagerFactory", e);
            }
        }

        @Override
        public SSLEngine createSSLEngine(ByteBufAllocator allocator) {
            return sslCtx.newEngine(allocator);
        }
    }

    private static class SelfSignedSSLEngineFactory implements SSLEngineFactory {

        private final SslContext sslCtx;

        private SelfSignedSSLEngineFactory() {
            SelfSignedCertificate ssc;
            try {
                ssc = new SelfSignedCertificate();
            } catch (CertificateException e) {
                throw new IllegalStateException("Self signed certificate creation error", e);
            }
            try {
                sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
            } catch (SSLException e) {
                throw new IllegalStateException("Failed to create Netty's Ssl context with self signed certificate", e);
            }
        }

        @Override
        public SSLEngine createSSLEngine(ByteBufAllocator allocator) {
            return sslCtx.newEngine(allocator);
        }
    }
}

@@ -235,16 +235,49 @@
 icate;%0A%0A
+import javax.net.ssl.SSLContext;%0A
 import j
@@ -649,32 +649,540 @@
 ries() %7B%0A    %7D%0A%0A
+    public static SSLEngineFactory fromSSLContext(SSLContext sslContext) %7B%0A        return new FromSSLContext(sslContext);%0A    %7D%0A%0A    public static class FromSSLContext implements SSLEngineFactory %7B%0A%0A        private final SSLContext sslContext;%0A%0A        public FromSSLContext(SSLContext sslContext) %7B%0A            this.sslContext = sslContext;%0A        %7D%0A%0A        @Override%0A        public SSLEngine createSSLEngine(ByteBufAllocator allocator) %7B%0A            return sslContext.createSSLEngine();%0A        %7D%0A    %7D%0A%0A
     private stat

Add default implementation of SSLEngineFactory for javax.net.ssl.SSLContext.