@Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap bootstrap = new Bootstrap();
    bootstrap
        .group(inboundChannel.eventLoop())
        .channel(ctx.channel().getClass())
        .handler(
            new ChannelInitializer<SocketChannel>() {
              @Override
              public void initChannel(SocketChannel ch) throws Exception {
                // Create a default pipeline implementation.
                ChannelPipeline pipeline = ch.pipeline();

                // add logging
                if (logger.isDebugEnabled()) {
                  pipeline.addLast("logger", new LoggingHandler("                -->"));
                }

                // add HTTPS proxy -> server support
                if (secure) {
                  SSLEngine engine = SSLFactory.sslContext().createSSLEngine();
                  engine.setUseClientMode(true);
                  pipeline.addLast("proxy -> server ssl", new SslHandler(engine));
                }

                // add handler
                pipeline.addLast(
                    new ProxyRelayHandler(
                        inboundChannel,
                        bufferedCapacity,
                        new RequestInterceptor(),
                        "                -->"));
              }
            })
        .option(ChannelOption.AUTO_READ, false);
    ChannelFuture channelFuture = bootstrap.connect(remoteSocketAddress);
    outboundChannel = channelFuture.channel();
    channelFuture.addListener(
        new ChannelFutureListener() {
          @Override
          public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
              channelBuffer.clear();
              bufferedMode = bufferedCapacity > 0;
              flushedBuffer = false;
              // connection complete start to read first data
              inboundChannel.read();
            } else {
              // Close the connection if the connection attempt has failed.
              inboundChannel.close();
            }
          }
        });
  }
Example #2
0
 private void connected(final Channel ch, final Handler<ClientConnection> connectHandler) {
   actualCtx.execute(
       ch.eventLoop(),
       new Runnable() {
         public void run() {
           createConn(ch, connectHandler);
         }
       });
 }
Example #3
0
  private void failed(
      final Channel ch, final Handler<Throwable> connectionExceptionHandler, final Throwable t) {
    // If no specific exception handler is provided, fall back to the HttpClient's exception
    // handler.
    final Handler<Throwable> exHandler =
        connectionExceptionHandler == null ? exceptionHandler : connectionExceptionHandler;

    actualCtx.execute(
        ch.eventLoop(),
        new Runnable() {
          public void run() {
            pool.connectionClosed();
            try {
              ch.close();
            } catch (Exception ignore) {
            }
            if (exHandler != null) {
              exHandler.handle(t);
            } else {
              actualCtx.reportException(t);
            }
          }
        });
  }