View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.anyflow.lannister.http;
18  
19  import io.netty.channel.ChannelInitializer;
20  import io.netty.channel.socket.SocketChannel;
21  import io.netty.handler.codec.http.HttpContentCompressor;
22  import io.netty.handler.codec.http.HttpObjectAggregator;
23  import io.netty.handler.codec.http.HttpServerCodec;
24  import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
25  import io.netty.handler.logging.LogLevel;
26  import io.netty.handler.logging.LoggingHandler;
27  import io.netty.handler.ssl.SslContext;
28  import io.netty.handler.ssl.SslContextBuilder;
29  import net.anyflow.lannister.Settings;
30  
31  class WebServerChannelInitializer extends ChannelInitializer<SocketChannel> {
32  
33  	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(WebServerChannelInitializer.class);
34  
35  	private final boolean useSsl;
36  	private final Class<? extends WebsocketFrameHandler> websocketFrameHandlerClass;
37  
38  	public WebServerChannelInitializer(boolean useSsl,
39  			Class<? extends WebsocketFrameHandler> websocketFrameHandlerClass) {
40  		this.useSsl = useSsl;
41  		this.websocketFrameHandlerClass = websocketFrameHandlerClass;
42  	}
43  
44  	@Override
45  	protected void initChannel(SocketChannel ch) throws Exception {
46  		if ("true".equalsIgnoreCase(Settings.INSTANCE.getProperty("webserver.logging.writelogOfNettyLogger"))) {
47  			ch.pipeline().addLast("log", new LoggingHandler("lannister.web/server", LogLevel.DEBUG));
48  		}
49  
50  		if (useSsl) {
51  			SslContext sslCtx = SslContextBuilder
52  					.forServer(Settings.INSTANCE.certChainFile(), Settings.INSTANCE.privateKeyFile()).build();
53  
54  			logger.debug("SSL Provider : {}", SslContext.defaultServerProvider());
55  
56  			ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
57  		}
58  
59  		ch.pipeline().addLast(HttpServerCodec.class.getName(), new HttpServerCodec());
60  		ch.pipeline().addLast(HttpObjectAggregator.class.getName(), new HttpObjectAggregator(1048576));
61  		ch.pipeline().addLast(HttpContentCompressor.class.getName(), new HttpContentCompressor());
62  		ch.pipeline().addLast(HttpRequestRouter.class.getName(), new HttpRequestRouter());
63  
64  		if (websocketFrameHandlerClass != null) {
65  			WebsocketFrameHandler wsfh = websocketFrameHandlerClass.newInstance();
66  
67  			ch.pipeline().addLast(WebSocketServerProtocolHandler.class.getName(), new WebSocketServerProtocolHandler(
68  					wsfh.websocketPath(), wsfh.subprotocols(), wsfh.allowExtensions(), wsfh.maxFrameSize()));
69  
70  			ch.pipeline().addLast(wsfh);
71  		}
72  	}
73  }