// Path: src/main/java/org/jboss/netty/example/securechat/SecureChatSslContextFactory.java
// public class SecureChatSslContextFactory {
// 
//     private static final String PROTOCOL = "TLS";
//     private static final SSLContext SERVER_CONTEXT;
//     private static final SSLContext CLIENT_CONTEXT;
// 
//     static {
//         String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
//         if (algorithm == null) {
//             algorithm = "SunX509";
//         }
// 
//         SSLContext serverContext;
//         SSLContext clientContext;
//         try {
//             KeyStore ks = KeyStore.getInstance("JKS");
//             ks.load(SecureChatKeyStore.asInputStream(), SecureChatKeyStore.getKeyStorePassword());
// 
//             // Set up key manager factory to use our key store
//             KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
//             kmf.init(ks, SecureChatKeyStore.getCertificatePassword());
// 
//             // Initialize the SSLContext to work with our key managers.
//             serverContext = SSLContext.getInstance(PROTOCOL);
//             serverContext.init(kmf.getKeyManagers(), SecureChatTrustManagerFactory.getTrustManagers(), null);
//         } catch (Exception e) {
//             throw new Error("Failed to initialize the server-side SSLContext", e);
//         }
// 
//         try {
//             clientContext = SSLContext.getInstance(PROTOCOL);
//             clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
//         } catch (Exception e) {
//             throw new Error("Failed to initialize the client-side SSLContext", e);
//         }
// 
//         SERVER_CONTEXT = serverContext;
//         CLIENT_CONTEXT = clientContext;
//     }
// 
//     public static SSLContext getServerContext() {
//         return SERVER_CONTEXT;
//     }
// 
//     public static SSLContext getClientContext() {
//         return CLIENT_CONTEXT;
//     }
// }


import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory;
import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.ssl.SslHandler;
import org.jboss.netty.util.CharsetUtil;
import javax.net.ssl.SSLEngine;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

        this.useSsl = USE_SSL;
        this.failureProbability = FAILURE_PROBABILITY;
        this.responseLatency = RESPONSE_LATENCY;
        this.content = CONTENT;
        this.useOldIo = USE_OLD_IO;
    }

    public DummyHttpServer(int port) {
        this(null, port);
    }

    // public methods -------------------------------------------------------------------------------------------------

    public boolean init() {
        if (this.useOldIo) {
            this.bootstrap = new ServerBootstrap(new OioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                                                                                   Executors.newCachedThreadPool()));
        } else {
            this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                                                                                   Executors.newCachedThreadPool()));
        }

        this.bootstrap.setOption("child.tcpNoDelay", true);
        this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();

                if (useSsl) {
// Path: src/main/java/org/jboss/netty/example/securechat/SecureChatSslContextFactory.java
// public class SecureChatSslContextFactory {
// 
//     private static final String PROTOCOL = "TLS";
//     private static final SSLContext SERVER_CONTEXT;
//     private static final SSLContext CLIENT_CONTEXT;
// 
//     static {
//         String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
//         if (algorithm == null) {
//             algorithm = "SunX509";
//         }
// 
//         SSLContext serverContext;
//         SSLContext clientContext;
//         try {
//             KeyStore ks = KeyStore.getInstance("JKS");
//             ks.load(SecureChatKeyStore.asInputStream(), SecureChatKeyStore.getKeyStorePassword());
// 
//             // Set up key manager factory to use our key store
//             KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
//             kmf.init(ks, SecureChatKeyStore.getCertificatePassword());
// 
//             // Initialize the SSLContext to work with our key managers.
//             serverContext = SSLContext.getInstance(PROTOCOL);
//             serverContext.init(kmf.getKeyManagers(), SecureChatTrustManagerFactory.getTrustManagers(), null);
//         } catch (Exception e) {
//             throw new Error("Failed to initialize the server-side SSLContext", e);
//         }
// 
//         try {
//             clientContext = SSLContext.getInstance(PROTOCOL);
//             clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
//         } catch (Exception e) {
//             throw new Error("Failed to initialize the client-side SSLContext", e);
//         }
// 
//         SERVER_CONTEXT = serverContext;
//         CLIENT_CONTEXT = clientContext;
//     }
// 
//     public static SSLContext getServerContext() {
//         return SERVER_CONTEXT;
//     }
// 
//     public static SSLContext getClientContext() {
//         return CLIENT_CONTEXT;
//     }
// }


// Path: src/main/java/com/biasedbit/hotpotato/util/DummyHttpServer.java
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory;
import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.ssl.SslHandler;
import org.jboss.netty.util.CharsetUtil;
import javax.net.ssl.SSLEngine;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;


        this.useSsl = USE_SSL;
        this.failureProbability = FAILURE_PROBABILITY;
        this.responseLatency = RESPONSE_LATENCY;
        this.content = CONTENT;
        this.useOldIo = USE_OLD_IO;
    }

    public DummyHttpServer(int port) {
        this(null, port);
    }

    // public methods -------------------------------------------------------------------------------------------------

    public boolean init() {
        if (this.useOldIo) {
            this.bootstrap = new ServerBootstrap(new OioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                                                                                   Executors.newCachedThreadPool()));
        } else {
            this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                                                                                   Executors.newCachedThreadPool()));
        }

        this.bootstrap.setOption("child.tcpNoDelay", true);
        this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();

                if (useSsl) {
                    SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();