Java Code Examples for org.jboss.netty.channel.ChannelPipeline#addFirst()

The following examples show how to use org.jboss.netty.channel.ChannelPipeline#addFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSSLAwareChannelPipelineFactory.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline =  super.getPipeline();

    if (isSSLSocket()) {
        // We need to set clientMode to false.
        // See https://issues.apache.org/jira/browse/JAMES-1025
        SSLEngine engine = getSSLContext().createSSLEngine();
        engine.setUseClientMode(false);
        if (enabledCipherSuites != null && enabledCipherSuites.length > 0) {
            engine.setEnabledCipherSuites(enabledCipherSuites);
        }
        pipeline.addFirst(HandlerConstants.SSL_HANDLER, new SslHandler(engine));
    }
    return pipeline;
}
 
Example 2
Source File: AvroSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelPipeline getPipeline() throws Exception {
  ChannelPipeline pipeline = Channels.pipeline();
  if (enableCompression) {
    ZlibEncoder encoder = new ZlibEncoder(6);
    pipeline.addFirst("deflater", encoder);
    pipeline.addFirst("inflater", new ZlibDecoder());
  }
  if (enableSsl) {
    SSLEngine sslEngine = createServerSSLContext().createSSLEngine();
    sslEngine.setUseClientMode(false);
    // addFirst() will make SSL handling the first stage of decoding
    // and the last stage of encoding this must be added after
    // adding compression handling above
    pipeline.addFirst("ssl", new SslHandler(sslEngine));
  }
  return pipeline;
}
 
Example 3
Source File: TestAvroSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public SocketChannel newChannel(ChannelPipeline pipeline) {
  try {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[]{new PermissiveTrustManager()},
                    null);
    SSLEngine sslEngine = sslContext.createSSLEngine();
    sslEngine.setUseClientMode(true);
    // addFirst() will make SSL handling the first stage of decoding
    // and the last stage of encoding
    pipeline.addFirst("ssl", new SslHandler(sslEngine));
    return super.newChannel(pipeline);
  } catch (Exception ex) {
    throw new RuntimeException("Cannot create SSL channel", ex);
  }
}
 
Example 4
Source File: RpcTestUtils.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelPipeline getPipeline() throws Exception {
  ChannelPipeline pipeline = Channels.pipeline();
  ZlibEncoder encoder = new ZlibEncoder(6);
  pipeline.addFirst("deflater", encoder);
  pipeline.addFirst("inflater", new ZlibDecoder());
  return pipeline;
}
 
Example 5
Source File: TestAvroSource.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public SocketChannel newChannel(ChannelPipeline pipeline) {
  try {

    ZlibEncoder encoder = new ZlibEncoder(compressionLevel);
    pipeline.addFirst("deflater", encoder);
    pipeline.addFirst("inflater", new ZlibDecoder());
    return super.newChannel(pipeline);
  } catch (Exception ex) {
    throw new RuntimeException("Cannot create Compression channel", ex);
  }
}
 
Example 6
Source File: ManageSieveServer.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
protected ChannelPipelineFactory createPipelineFactory(final ChannelGroup group) {

    return new ChannelPipelineFactory() {

        private final ChannelGroupHandler groupHandler = new ChannelGroupHandler(group);

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = pipeline();
            Encryption secure = getEncryption();
            if (secure != null && !secure.isStartTLS()) {
                // We need to set clientMode to false.
                // See https://issues.apache.org/jira/browse/JAMES-1025
                SSLEngine engine = secure.getContext().createSSLEngine();
                engine.setUseClientMode(false);
                pipeline.addFirst(SSL_HANDLER, new SslHandler(engine));

            }
            pipeline.addLast(GROUP_HANDLER, groupHandler);
            pipeline.addLast(CONNECTION_LIMIT_HANDLER, new ConnectionLimitUpstreamHandler(ManageSieveServer.this.connectionLimit));
            pipeline.addLast(CONNECTION_LIMIT_PER_IP_HANDLER, new ConnectionPerIpLimitUpstreamHandler(ManageSieveServer.this.connPerIP));
            // Add the text line decoder which limit the max line length,
            // don't strip the delimiter and use CRLF as delimiter
            // Use a SwitchableDelimiterBasedFrameDecoder, see JAMES-1436
            pipeline.addLast(FRAMER, getFrameHandlerFactory().create(pipeline));
            pipeline.addLast(CONNECTION_COUNT_HANDLER, getConnectionCountHandler());
            pipeline.addLast(CHUNK_WRITE_HANDLER, new ChunkedWriteHandler());

            ExecutionHandler ehandler = getExecutionHandler();
            if (ehandler  != null) {
                pipeline.addLast(EXECUTION_HANDLER, ehandler);

            }
            pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(CORE_HANDLER, createCoreHandler());
            pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
            return pipeline;
        }

    };
}
 
Example 7
Source File: IMAPServer.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
protected ChannelPipelineFactory createPipelineFactory(final ChannelGroup group) {
    
    return new ChannelPipelineFactory() {
        
        private final ChannelGroupHandler groupHandler = new ChannelGroupHandler(group);
        private final HashedWheelTimer timer = new HashedWheelTimer();
        
        private final TimeUnit timeoutUnit = TimeUnit.SECONDS;

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = pipeline();
            pipeline.addLast(GROUP_HANDLER, groupHandler);
            pipeline.addLast("idleHandler", new IdleStateHandler(timer, 0, 0, timeout, timeoutUnit));
            pipeline.addLast(TIMEOUT_HANDLER, new ImapIdleStateHandler());
            pipeline.addLast(CONNECTION_LIMIT_HANDLER, new ConnectionLimitUpstreamHandler(IMAPServer.this.connectionLimit));

            pipeline.addLast(CONNECTION_LIMIT_PER_IP_HANDLER, new ConnectionPerIpLimitUpstreamHandler(IMAPServer.this.connPerIP));

            // Add the text line decoder which limit the max line length,
            // don't strip the delimiter and use CRLF as delimiter
            // Use a SwitchableDelimiterBasedFrameDecoder, see JAMES-1436
            pipeline.addLast(FRAMER, getFrameHandlerFactory().create(pipeline));
           
            Encryption secure = getEncryption();
            if (secure != null && !secure.isStartTLS()) {
                // We need to set clientMode to false.
                // See https://issues.apache.org/jira/browse/JAMES-1025
                SSLEngine engine = secure.getContext().createSSLEngine();
                engine.setUseClientMode(false);
                pipeline.addFirst(SSL_HANDLER, new SslHandler(engine));

            }
            pipeline.addLast(CONNECTION_COUNT_HANDLER, getConnectionCountHandler());

            pipeline.addLast(CHUNK_WRITE_HANDLER, new ChunkedWriteHandler());

            ExecutionHandler ehandler = getExecutionHandler();
            if (ehandler  != null) {
                pipeline.addLast(EXECUTION_HANDLER, ehandler);

            }
            pipeline.addLast(REQUEST_DECODER, new ImapRequestFrameDecoder(decoder, inMemorySizeLimit, literalSizeLimit));

            pipeline.addLast(CORE_HANDLER, createCoreHandler());
            return pipeline;
        }

    };
}
 
Example 8
Source File: NettyAvroRpcClient.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Override
public SocketChannel newChannel(ChannelPipeline pipeline) {
  TrustManager[] managers;
  try {
    if (enableCompression) {
      ZlibEncoder encoder = new ZlibEncoder(compressionLevel);
      pipeline.addFirst("deflater", encoder);
      pipeline.addFirst("inflater", new ZlibDecoder());
    }
    if (enableSsl) {
      if (trustAllCerts) {
        logger.warn("No truststore configured, setting TrustManager to accept"
            + " all server certificates");
        managers = new TrustManager[] { new PermissiveTrustManager() };
      } else {
        KeyStore keystore = null;

        if (truststore != null) {
          if (truststorePassword == null) {
            throw new NullPointerException("truststore password is null");
          }
          InputStream truststoreStream = new FileInputStream(truststore);
          keystore = KeyStore.getInstance(truststoreType);
          keystore.load(truststoreStream, truststorePassword.toCharArray());
        }

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        // null keystore is OK, with SunX509 it defaults to system CA Certs
        // see http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#X509TrustManager
        tmf.init(keystore);
        managers = tmf.getTrustManagers();
      }

      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, managers, null);
      SSLEngine sslEngine = sslContext.createSSLEngine();
      sslEngine.setUseClientMode(true);
      // addFirst() will make SSL handling the first stage of decoding
      // and the last stage of encoding this must be added after
      // adding compression handling above
      pipeline.addFirst("ssl", new SslHandler(sslEngine));
    }

    return super.newChannel(pipeline);
  } catch (Exception ex) {
    logger.error("Cannot create SSL channel", ex);
    throw new RuntimeException("Cannot create SSL channel", ex);
  }
}