/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.shield.transport.netty;

import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.netty.channel.ChannelFuture;
import org.elasticsearch.common.netty.channel.ChannelFutureListener;
import org.elasticsearch.common.netty.channel.ChannelHandlerContext;
import org.elasticsearch.common.netty.channel.ChannelStateEvent;
import org.elasticsearch.common.netty.handler.ssl.SslHandler;
import org.elasticsearch.shield.transport.ssl.ElasticsearchSSLException;
import org.elasticsearch.transport.netty.MessageChannelHandler;

public class SecuredMessageChannelHandler extends MessageChannelHandler {

    public SecuredMessageChannelHandler(org.elasticsearch.transport.netty.NettyTransport transport, ESLogger logger) {
        super(transport, logger);
    }

    @Override
    public void channelConnected(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception {
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);

        // Get notified when SSL handshake is done.
        final ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    logger.debug("SSL / TLS handshake completed for channel", ctx.getName());
                    ctx.sendUpstream(e);
                } else {
                    logger.error("SSL / TLS handshake failed, closing channel", ctx.getName());
                    future.getChannel().close();
                    throw new ElasticsearchSSLException("SSL / TLS handshake failed, closing the channel", future.getCause());
                }
            }
        });
    }
}

@@ -1646,34 +1646,19 @@
 channel%22
-, ctx.getName()
 );%0A
+
         
@@ -1715,16 +1715,71 @@
  else %7B%0A
+                    if (logger.isDebugEnabled()) %7B%0A    
         
@@ -1851,131 +1851,132 @@
 nnel
-%22, ctx.getName());%0A                    future.getChannel().close();%0A                    throw new ElasticsearchSSLException
+: %7B%7D%22, future.getCause(), future.getCause().getMessage());%0A                    %7D else %7B%0A                        logger.error
 (%22SS
@@ -2013,19 +2013,19 @@
 ing 
-the 
 channel
+: %7B%7D
 %22, f
@@ -2040,16 +2040,100 @@
 tCause()
+.getMessage());%0A                    %7D%0A                    future.getChannel().close(
 );%0A     

Stop logging closed channel exceptions by default