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