public ChannelPipeline getPipeline() throws Exception {
    log.debug("Creating client channel pipeline");

    ChannelPipeline pipeline = Channels.pipeline();

    SSLEngine engine = sslContext.createSSLEngine();
    engine.setUseClientMode(true);

    SslHandler sslHandler = new SslHandler(engine);
    sslHandler.setCloseOnSSLException(true);
    pipeline.addLast("ssl", sslHandler);

    pipeline.addLast("chunker", new ChunkedWriteHandler());

    pipeline.addLast(
        "framer",
        new DelimiterBasedFrameDecoder(protocol.maxHeaderLength(), protocol.headerDelimiter()));
    pipeline.addLast("stringDecoder", new StringDecoder(protocol.headerCharset()));
    pipeline.addLast("stringEncoder", new StringEncoder(protocol.headerCharset()));

    HeaderCodec headerCodec = new HeaderCodec(protocol);
    pipeline.addLast("headerDecoder", headerCodec.decoder());
    pipeline.addLast("headerEncoder", headerCodec.encoder());

    pipeline.addLast("client", new ClientHandler(store));

    return pipeline;
  }
  @SuppressWarnings("unchecked")
  @Override
  public SMTPClientFuture<FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>
      startTLS() {
    if (!isEncrypted()) {
      final SMTPClientFutureImpl<FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>
          future =
              new SMTPClientFutureImpl<
                  FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>(false);

      SslHandler sslHandler = new SslHandler(engine, false);
      channel.getPipeline().addFirst(SSL_HANDLER_KEY, sslHandler);
      sslHandler
          .handshake()
          .addListener(
              new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture cfuture) throws Exception {
                  if (cfuture.isSuccess()) {
                    future.setResult(FutureResult.createVoid());
                  } else {
                    future.setResult(FutureResult.create(cfuture.getCause()));
                  }
                }
              });

      return future;
    } else {
      return new ReadySMTPClientFuture<
          FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>(
          this, FutureResult.create(STARTTLS_EXCEPTION));
    }
  }
  @Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    // Get the SslHandler in the current pipeline.
    // We added it in SecureChatPipelineFactory.
    final SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);

    // Get notified when SSL handshake is done.
    ChannelFuture handshakeFuture = sslHandler.handshake();
    handshakeFuture.addListener(new SslLister(sslHandler));
  }
  @Override
  public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();

    pipeline.addLast("debug-client-head", new DebugHandler("CLIENT_HEAD"));
    if (this.clientContext.isSecure()) {
      SSLEngine sslEngine = this.clientContext.getSSLContext().createSSLEngine();
      sslEngine.setUseClientMode(true);
      SslHandler sslHandler = new SslHandler(sslEngine);
      sslHandler.setEnableRenegotiation(false);
      sslHandler.setIssueHandshake(true);
      pipeline.addLast("ssl", sslHandler);
      pipeline.addLast("client-post-ssl", new DebugHandler("SERVER-POST-SSL"));
    }
    if (this.handshake != null) {
      pipeline.addLast("http-encoder", new HttpRequestEncoder());
      pipeline.addLast("http-decoder", new WebSocketHttpResponseDecoder(this.handshake));
      pipeline.addLast(
          "websocket-connection-negotiator",
          new WebSocketConnectionNegotiator(
              this.clientContext.getWebSocketAddress(), this.handshake));
      pipeline.addLast("stomp-frame-decoder", new WebSocketStompFrameDecoder());
      pipeline.addLast("stomp-frame-encoder", new WebSocketStompFrameEncoder());
    } else {
      pipeline.addLast("stomp-frame-decoder", new StompFrameDecoder());
      pipeline.addLast("stomp-frame-encoder", new StompFrameEncoder());
    }

    pipeline.addLast("debug-client-mid", new DebugHandler("CLIENT_MID"));

    pipeline.addLast(
        "stomp-connection-negotiator", new StompConnectionNegotiator(clientContext, "localhost"));
    pipeline.addLast("stomp-client-receipt", new ClientReceiptHandler(clientContext));

    pipeline.addLast("stomp-message-encoder", new StompMessageEncoder());
    pipeline.addLast(
        "stomp-message-decoder",
        new StompMessageDecoder(new ClientStompMessageFactory(this.client)));

    pipeline.addLast("stomp-client-message-handler", new ClientMessageHandler(clientContext));
    pipeline.addLast("debug-client-tail", new DebugHandler("CLIENT_TAIL"));

    return pipeline;
  }
Exemple #5
0
 @Override
 public void handleUpstream(final ChannelHandlerContext ctx, final ChannelEvent e)
     throws Exception {
   Object o = null;
   if ((e instanceof MessageEvent)
       && this.first.compareAndSet(true, false)
       && ((o = ((MessageEvent) e).getMessage()) instanceof ChannelBuffer)
       && !maybeSsl((ChannelBuffer) o)) {
     ctx.getPipeline().removeFirst();
     ctx.sendUpstream(e);
   } else {
     super.handleUpstream(ctx, e);
   }
 }
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

      final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
      NioWorker worker = ch.getWorker();

      // Choose a handler
      final HandlerHolder handler = handlerManager.chooseHandler(worker);

      if (handler == null) {
        // Ignore
        return;
      }

      if (tcpHelper.isSSL()) {
        SslHandler sslHandler = (SslHandler) ch.getPipeline().get("ssl");

        ChannelFuture fut = sslHandler.handshake();
        fut.addListener(
            new ChannelFutureListener() {

              public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()) {
                  connected(ch, handler);
                } else {
                  log.error(
                      "Client from origin "
                          + ch.getRemoteAddress()
                          + " failed to connect over ssl");
                }
              }
            });

      } else {
        connected(ch, handler);
      }
    }