package com.manning.nettyinaction.chapter8;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
/**
* Listing 8.5 of <i>Netty in Action</i>
*
* @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
*/
public class HttpsCodecInitializer extends ChannelInitializer<Channel> {
private final SSLContext context;
private final boolean client;
public HttpsCodecInitializer(SSLContext context, boolean client) {
this.context = context;
this.client = client;
}
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SSLEngine engine = context.createSSLEngine();
engine.setUseClientMode(client);
pipeline.addFirst("ssl", new SslHandler(engine));
if (client) {
pipeline.addLast("codec", new HttpClientCodec());
} else {
pipeline.addLast("codec", new HttpServerCodec());
}
}
}