Skip to content

Instantly share code, notes, and snippets.

@cloudbow
Created March 12, 2014 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudbow/9513536 to your computer and use it in GitHub Desktop.
Save cloudbow/9513536 to your computer and use it in GitHub Desktop.
initializer
package gcm.netty.handlers;
import gcm.netty.codec.GcmBatchMessageEncoder;
import gcm.netty.config.GcmConfig;
import gcm.netty.constants.GcmProtocolConstants;
import gcm.netty.constants.TraceLogs;
import gcm.netty.security.GcmSSLContextFactory;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.ssl.SslHandler;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLEngine;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* Handles a client-side channel.
*/
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class BatchGcmMessageInitializer extends ChannelInitializer<SocketChannel> {
/** The Constant logger. */
private static final Logger logger = Logger.getLogger(BatchGcmMessageInitializer.class);
/** The apns config. */
@Autowired
private GcmConfig gcmConfig;
/** The application context. */
@Autowired
private ApplicationContext applicationContext;
/** The apns ssl context factory. */
@Autowired
private GcmSSLContextFactory gcmSSLContextFactory;
/*
* (non-Javadoc)
* @see
* io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
*/
@Override
public void initChannel(final SocketChannel ch) throws Exception {
BatchGcmMessageInitializer.logger.trace(TraceLogs.INITING_CHANNEL);
final ChannelPipeline pipeline = ch.pipeline();
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
BatchGcmMessageInitializer.logger.trace(TraceLogs.GET_GCM_SSL_CONTEXT_FACTORY + getGcmSSLContextFactory());
BatchGcmMessageInitializer.logger.trace(TraceLogs.GET_GCM_SSL_CONTEXT_FACTORY_GET_SSL_CONTEXT
+ getGcmSSLContextFactory().getSSLContext());
final SSLEngine engine = getGcmSSLContextFactory().getSSLContext().createSSLEngine();
BatchGcmMessageInitializer.logger.trace(TraceLogs.SSL_ENGINE_IS + engine);
engine.setUseClientMode(true);
final SslHandler sslHandler = new SslHandler(engine);
sslHandler.setHandshakeTimeout(gcmConfig.getHandShakeTimeout(), TimeUnit.MILLISECONDS);
pipeline.addFirst(GcmProtocolConstants.SSL, sslHandler);
pipeline.addLast(GcmProtocolConstants.ENCODER1, new HttpRequestEncoder());
pipeline.addLast(GcmProtocolConstants.ENCODER2, applicationContext.getBean(GcmBatchMessageEncoder.class));
pipeline.addLast(GcmProtocolConstants.DECODER1, new HttpResponseDecoder());
pipeline.addLast(GcmProtocolConstants.INFLATOR, new HttpContentDecompressor());
// // Remove the following line if you don't want automatic content
// // decompression.
pipeline.addLast(GcmProtocolConstants.AGGREGATOR, new HttpObjectAggregator(512 * 1024));
// pipeline.addLast(BatchGcmMessageInitializer.CODEC,
// new HttpClientCodec());
//
// pipeline.addLast("encoder",
// applicationContext.getBean(GcmBatchMessageEncoder.class));
//
// // pipeline.addLast(BatchGcmMessageInitializer.CODEC,
// // new HttpClientCodec());
//
// // // Remove the following line if you don't want automatic content
// // // decompression.
// pipeline.addLast("inflator", new HttpContentDecompressor());
// pipeline.addLast(BatchGcmMessageInitializer.AGGEGATOR,
// new HttpObjectAggregator(512 * 1024));
// // to be used since huge file transfer
// pipeline.addLast(BatchGcmMessageInitializer.CHUNKED_WRITER,
// new ChunkedWriteHandler());
// // On top of the SSL handler, add the text line codec.
// pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
// Delimiters.lineDelimiter()));
// pipeline.addLast("decoder",
// applicationContext.getBean(BatchConnectionDecoder.class));
// pipeline.addLast("encoder",
// applicationContext.getBean(BatchConnectionEncoder.class));
// pipeline.addLast(BatchGcmMessageInitializer.CODEC,
// applicationContext.getBean(BatchMessageCodec.class));
// and then business logic.
pipeline.addLast(GcmProtocolConstants.HANDLER, applicationContext.getBean(BatchGcmMessagesHandler.class));
}
/**
* Gets the apns ssl context factory.
* @return the apns ssl context factory
*/
public GcmSSLContextFactory getGcmSSLContextFactory() {
return gcmSSLContextFactory;
}
/**
* Sets the apns ssl context factory.
* @param gcmSSLContextFactory
* the new apns ssl context factory
*/
public void setGcmSSLContextFactory(final GcmSSLContextFactory gcmSSLContextFactory) {
this.gcmSSLContextFactory = gcmSSLContextFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment