Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 14th, 2012  |  syntax: None  |  size: 3.43 KB  |  views: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public ChannelPipeline getPipeline() throws Exception {
  2.     // Create a default pipeline implementation.
  3.     ChannelPipeline pipeline = pipeline();
  4.  
  5.     // Uncomment the following line if you want HTTPS
  6.     SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
  7.     engine.setUseClientMode(false);
  8.     engine.setNeedClientAuth(true);
  9.     pipeline.addLast("ssl", new SslHandler(engine));
  10.  
  11.     pipeline.addLast("decoder", new HttpRequestDecoder());
  12.     pipeline.addLast("logger", new RequestAuditLogger());
  13.     // Uncomment the following line if you don't want to handle HttpChunks.
  14.     pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
  15.     pipeline.addLast("outputLogger", new ResponseAuditLogger());
  16.     pipeline.addLast("encoder", new HttpResponseEncoder());
  17.     // Remove the following line if you don't want automatic content compression.
  18.     pipeline.addLast("deflater", new HttpContentCompressor());
  19.     pipeline.addLast("handler", new HttpSnoopServerHandler());
  20.     return pipeline;
  21. }
  22. }
  23.        
  24. public final class SecureChatSslContextFactory {
  25.  
  26.  
  27. private static Logger logger = LoggerFactory.getLogger(SecureChatSslContextFactory.class);
  28.  
  29. private static final String PROTOCOL = "TLS";
  30. private static final SSLContext SERVER_CONTEXT;
  31. private static final SSLContext CLIENT_CONTEXT;
  32.  
  33. static {
  34.  
  35.     SSLContext serverContext = null;
  36.     SSLContext clientContext = null;
  37.  
  38.         // get keystore and trustore locations and passwords
  39.     String keyStoreLocation = System.getProperty("javax.net.ssl.keyStore");
  40.     String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
  41.     String trustStoreLocation = System.getProperty("javax.net.ssl.trustStore");
  42.     String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
  43.     try {
  44.  
  45.         KeyStore ks = KeyStore.getInstance("JKS");
  46.         ks.load(KeyStoreStreamManager.asInputStream(keyStoreLocation),
  47.                 keyStorePassword.toCharArray());
  48.  
  49.         // Set up key manager factory to use our key store
  50.         KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  51.         kmf.init(ks, keyStorePassword.toCharArray());
  52.  
  53.           // truststore
  54.         KeyStore ts = KeyStore.getInstance("JKS");
  55.         ts.load(KeyStoreStreamManager.asInputStream(trustStoreLocation),
  56.                 trustStorePassword.toCharArray());
  57.  
  58.         // set up trust manager factory to use our trust store
  59.         TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  60.         tmf.init(ts);
  61.  
  62.         // Initialize the SSLContext to work with our key managers.
  63.         serverContext = SSLContext.getInstance(PROTOCOL);
  64.         serverContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  65.  
  66.     } catch (Exception e) {
  67.         throw new Error(
  68.                 "Failed to initialize the server-side SSLContext", e);
  69.     }
  70.  
  71.     try {
  72.         clientContext = SSLContext.getInstance(PROTOCOL);
  73.         clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
  74.     } catch (Exception e) {
  75.         throw new Error(
  76.                 "Failed to initialize the client-side SSLContext", e);
  77.     }
  78.  
  79.     SERVER_CONTEXT = serverContext;
  80.     CLIENT_CONTEXT = clientContext;
  81. }
  82.  
  83. public static SSLContext getServerContext() {
  84.     return SERVER_CONTEXT;
  85. }
  86.  
  87. public static SSLContext getClientContext() {
  88.     return CLIENT_CONTEXT;
  89. }
  90.  
  91. private SecureChatSslContextFactory() {
  92.     // Unused
  93. }
  94. }