2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.base;
24 import java.net.InetSocketAddress;
25 import java.security.cert.CertificateException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.net.ssl.SSLException;
31 import io.netty.bootstrap.ServerBootstrap;
32 import io.netty.channel.ChannelFuture;
33 import io.netty.channel.ChannelHandler;
34 import io.netty.channel.ChannelInitializer;
35 import io.netty.channel.ChannelPipeline;
36 import io.netty.channel.EventLoopGroup;
37 import io.netty.channel.nio.NioEventLoopGroup;
38 import io.netty.channel.socket.SocketChannel;
39 import io.netty.channel.socket.nio.NioServerSocketChannel;
40 import io.netty.handler.codec.http.HttpRequestDecoder;
41 import io.netty.handler.codec.http.HttpResponseEncoder;
42 import io.netty.handler.logging.LogLevel;
43 import io.netty.handler.logging.LoggingHandler;
44 import io.netty.util.concurrent.GenericFutureListener;
46 public class HttpServer {
48 private static class HttpServerInitializer
49 extends ChannelInitializer<SocketChannel> {
51 private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
53 public HttpServerInitializer() {
56 public void addHandler(ChannelHandler handler) {
57 additionalHandlers.add(handler);
61 public void initChannel(SocketChannel ch) {
62 ChannelPipeline p = ch.pipeline();
64 * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
66 p.addLast(new HttpRequestDecoder());
67 // Uncomment the following line if you don't want to handle
69 // p.addLast(new HttpObjectAggregator(1048576));
70 p.addLast(new HttpResponseEncoder());
71 // Remove the following line if you don't want automatic content
73 // p.addLast(new HttpContentCompressor());
74 for (ChannelHandler handler : additionalHandlers) {
81 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
83 EventLoopGroup workerGroup = new NioEventLoopGroup();
85 HttpServerInitializer initializer = new HttpServerInitializer();
87 public void addHandler(ChannelHandler handler) {
88 initializer.addHandler(handler);
91 public void startServer(InetSocketAddress inetSocketAddress)
92 throws CertificateException, SSLException, InterruptedException {
95 ServerBootstrap b = new ServerBootstrap();
96 b.group(bossGroup, workerGroup);
97 b.channel(NioServerSocketChannel.class);
98 b.handler(new LoggingHandler(LogLevel.INFO));
100 b.childHandler(initializer);
102 ChannelFuture ch = b.bind(inetSocketAddress).sync();
103 ch.addListener(new GenericFutureListener<ChannelFuture>() {
106 public void operationComplete(ChannelFuture future)
108 // TODO Auto-generated method stub
110 .println("Connection status of TCP Http SERVER : "
111 + future.isSuccess());
120 public void stopServer() {
121 // shut down all event loops
122 if (bossGroup != null) {
123 bossGroup.shutdownGracefully();
126 bossGroup.terminationFuture().sync();
127 } catch (InterruptedException e) {
128 // TODO Auto-generated catch block
133 if (workerGroup != null) {
134 workerGroup.shutdownGracefully();
137 workerGroup.terminationFuture().sync();
138 } catch (InterruptedException e) {
139 // TODO Auto-generated catch block