1 package com.foxinmy.weixin4j.socket;
2
3 import java.util.Map;
4
5 import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
6 import com.foxinmy.weixin4j.util.AesToken;
7
8 import io.netty.channel.ChannelInitializer;
9 import io.netty.channel.ChannelPipeline;
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.handler.codec.http.HttpObjectAggregator;
12 import io.netty.handler.codec.http.HttpServerCodec;
13
14
15
16
17
18
19
20
21
22
23 public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
24
25 private final WeixinMessageDispatcher messageDispatcher;
26 private final WeixinMessageDecoder messageDecoder;
27
28 public WeixinServerInitializer(Map<String, AesToken> aesTokenMap, WeixinMessageDispatcher messageDispatcher) {
29 this.messageDispatcher = messageDispatcher;
30 this.messageDecoder = new WeixinMessageDecoder(aesTokenMap);
31 }
32
33 public void addAesToken(AesToken asetoken) {
34 messageDecoder.addAesToken(asetoken);
35 }
36
37 @Override
38 protected void initChannel(SocketChannel channel) {
39 ChannelPipeline pipeline = channel.pipeline();
40 pipeline.addLast(new HttpServerCodec());
41 pipeline.addLast(new HttpObjectAggregator(65536));
42 pipeline.addLast(messageDecoder);
43 pipeline.addLast(new WeixinResponseEncoder());
44 pipeline.addLast(new SingleResponseEncoder());
45 pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
46 }
47 }