[Resource-container] Backported init fix to 1.1-rel
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / HttpClient.java
1 /*
2  * //****************************************************************** // //
3  * Copyright 2016 Samsung Electronics All Rights Reserved. //
4  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // //
5  * Licensed under the Apache License, Version 2.0 (the "License"); // you may
6  * not use this file except in compliance with the License. // You may obtain a
7  * copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // //
8  * Unless required by applicable law or agreed to in writing, software //
9  * distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT
10  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the
11  * License for the specific language governing permissions and // limitations
12  * under the License. //
13  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
14  */
15 // package org.iotivity.cloud.base;
16 //
17 // import java.net.URI;
18 // import java.net.URISyntaxException;
19 //
20 // import javax.net.ssl.SSLException;
21 //
22 // import io.netty.bootstrap.Bootstrap;
23 // import io.netty.channel.Channel;
24 // import io.netty.channel.ChannelHandlerContext;
25 // import io.netty.channel.ChannelInitializer;
26 // import io.netty.channel.ChannelPipeline;
27 // import io.netty.channel.EventLoopGroup;
28 // import io.netty.channel.SimpleChannelInboundHandler;
29 // import io.netty.channel.nio.NioEventLoopGroup;
30 // import io.netty.channel.socket.SocketChannel;
31 // import io.netty.channel.socket.nio.NioSocketChannel;
32 // import io.netty.handler.codec.http.ClientCookieEncoder;
33 // import io.netty.handler.codec.http.DefaultCookie;
34 // import io.netty.handler.codec.http.DefaultFullHttpRequest;
35 // import io.netty.handler.codec.http.HttpClientCodec;
36 // import io.netty.handler.codec.http.HttpContent;
37 // import io.netty.handler.codec.http.HttpContentDecompressor;
38 // import io.netty.handler.codec.http.HttpHeaders;
39 // import io.netty.handler.codec.http.HttpMethod;
40 // import io.netty.handler.codec.http.HttpObject;
41 // import io.netty.handler.codec.http.HttpRequest;
42 // import io.netty.handler.codec.http.HttpResponse;
43 // import io.netty.handler.codec.http.HttpVersion;
44 // import io.netty.handler.codec.http.LastHttpContent;
45 // import io.netty.handler.ssl.SslContext;
46 // import io.netty.handler.ssl.SslContextBuilder;
47 // import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
48 // import io.netty.util.CharsetUtil;
49 //
50 // public class HttpClient {
51 //
52 // private static class HttpClientInitializer
53 // extends ChannelInitializer<SocketChannel> {
54 //
55 // public static class HttpSnoopClientHandler
56 // extends SimpleChannelInboundHandler<HttpObject> {
57 //
58 // @Override
59 // public void channelRead0(ChannelHandlerContext ctx,
60 // HttpObject msg) {
61 // if (msg instanceof HttpResponse) {
62 // HttpResponse response = (HttpResponse) msg;
63 //
64 // System.err.println("STATUS: " + response.getStatus());
65 // System.err.println(
66 // "VERSION: " + response.getProtocolVersion());
67 // System.err.println();
68 //
69 // if (!response.headers().isEmpty()) {
70 // for (String name : response.headers().names()) {
71 // for (String value : response.headers()
72 // .getAll(name)) {
73 // System.err.println(
74 // "HEADER: " + name + " = " + value);
75 // }
76 // }
77 // System.err.println();
78 // }
79 //
80 // if (HttpHeaders.isTransferEncodingChunked(response)) {
81 // System.err.println("CHUNKED CONTENT {");
82 // } else {
83 // System.err.println("CONTENT {");
84 // }
85 // }
86 // if (msg instanceof HttpContent) {
87 // HttpContent content = (HttpContent) msg;
88 //
89 // System.err.print(
90 // content.content().toString(CharsetUtil.UTF_8));
91 // System.err.flush();
92 //
93 // if (content instanceof LastHttpContent) {
94 // System.err.println("} END OF CONTENT");
95 // ctx.close();
96 // }
97 // }
98 // }
99 //
100 // @Override
101 // public void exceptionCaught(ChannelHandlerContext ctx,
102 // Throwable cause) {
103 // cause.printStackTrace();
104 // ctx.close();
105 // }
106 // }
107 //
108 // private final SslContext sslCtx;
109 //
110 // public HttpClientInitializer(SslContext sslCtx) {
111 // this.sslCtx = sslCtx;
112 // }
113 //
114 // @Override
115 // public void initChannel(SocketChannel ch) {
116 // ChannelPipeline p = ch.pipeline();
117 //
118 // // Enable HTTPS if necessary.
119 // if (sslCtx != null) {
120 // p.addLast(sslCtx.newHandler(ch.alloc()));
121 // }
122 //
123 // p.addLast(new HttpClientCodec());
124 //
125 // // Remove the following line if you don't want automatic content
126 // // decompression.
127 // p.addLast(new HttpContentDecompressor());
128 //
129 // // Uncomment the following line if you don't want to handle
130 // // HttpContents.
131 // // p.addLast(new HttpObjectAggregator(1048576));
132 //
133 // p.addLast(new HttpSnoopClientHandler());
134 // }
135 // }
136 //
137 // public void connect(String strUrl)
138 // throws URISyntaxException, InterruptedException, SSLException {
139 // URI uri = new URI(strUrl);
140 //
141 // String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
142 // String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
143 //
144 // int port = uri.getPort();
145 //
146 // if (port == -1) {
147 // if ("http".equalsIgnoreCase(scheme)) {
148 // port = 80;
149 // } else if ("https".equalsIgnoreCase(scheme)) {
150 // port = 443;
151 // }
152 // }
153 //
154 // if (!"http".equalsIgnoreCase(scheme)
155 // && !"https".equalsIgnoreCase(scheme)) {
156 // return;
157 // }
158 //
159 // final boolean ssl = "https".equalsIgnoreCase(scheme);
160 // final SslContext sslCtx;
161 //
162 // if (ssl) {
163 // sslCtx = SslContextBuilder.forClient()
164 // .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
165 // } else {
166 // sslCtx = null;
167 // }
168 //
169 // EventLoopGroup group = new NioEventLoopGroup();
170 //
171 // try {
172 // Bootstrap b = new Bootstrap();
173 // b.group(group);
174 // b.channel(NioSocketChannel.class);
175 // b.handler(new HttpClientInitializer(sslCtx));
176 //
177 // Channel ch = b.connect(host, port).sync().channel();
178 //
179 // HttpRequest request = new DefaultFullHttpRequest(
180 // HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
181 // request.headers().set(HttpHeaders.Names.HOST, host);
182 // request.headers().set(HttpHeaders.Names.CONNECTION,
183 // HttpHeaders.Values.CLOSE);
184 // request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING,
185 // HttpHeaders.Values.GZIP);
186 //
187 // request.headers().set(HttpHeaders.Names.COOKIE,
188 // ClientCookieEncoder.encode(
189 // new DefaultCookie("my-cookie", "foo"),
190 // new DefaultCookie("another-cookie", "bar")));
191 //
192 // ch.writeAndFlush(request);
193 //
194 // ch.closeFuture().sync();
195 // } finally {
196 // group.shutdownGracefully();
197 // }
198 // }
199 //
200 // }