PageRenderTime 116ms CodeModel.GetById 101ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/sitebricks-mail/src/main/java/com/google/sitebricks/mail/MailClientPipelineFactory.java

http://github.com/dhanji/sitebricks
Java | 46 lines | 32 code | 9 blank | 5 comment | 2 complexity | 4a6165e2a278f69683a8dc7d88de7cce MD5 | raw file
 1package com.google.sitebricks.mail;
 2
 3import com.google.sitebricks.mail.Mail.Auth;
 4import org.jboss.netty.channel.ChannelPipeline;
 5import org.jboss.netty.channel.ChannelPipelineFactory;
 6import org.jboss.netty.channel.Channels;
 7import org.jboss.netty.handler.codec.string.StringDecoder;
 8import org.jboss.netty.handler.codec.string.StringEncoder;
 9import org.jboss.netty.handler.ssl.SslHandler;
10
11import javax.net.ssl.SSLContext;
12import javax.net.ssl.SSLEngine;
13
14/**
15 * @author dhanji@gmail.com (Dhanji R. Prasanna)
16 */
17class MailClientPipelineFactory implements ChannelPipelineFactory {
18  private final MailClientHandler mailClientHandler;
19  private final MailClientConfig config;
20
21  public MailClientPipelineFactory(MailClientHandler mailClientHandler, MailClientConfig config) {
22    this.mailClientHandler = mailClientHandler;
23    this.config = config;
24  }
25
26  public ChannelPipeline getPipeline() throws Exception {
27    // Create a default pipeline implementation.
28    ChannelPipeline pipeline = Channels.pipeline();
29
30    if (config.getAuthType() != Auth.PLAIN) {
31      SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine();
32      sslEngine.setUseClientMode(true);
33      SslHandler sslHandler = new SslHandler(sslEngine);
34      sslHandler.setEnableRenegotiation(true);
35      pipeline.addLast("ssl", sslHandler);
36    }
37
38    pipeline.addLast("decoder", new StringDecoder());
39    pipeline.addLast("encoder", new StringEncoder());
40
41    // and then business logic.
42    pipeline.addLast("handler", mailClientHandler);
43
44    return pipeline;
45  }
46}