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.connector;
23 // package org.iotivity.cloud.base;
25 // import java.net.URI;
26 // import java.net.URISyntaxException;
28 // import javax.net.ssl.SSLException;
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;
58 // public class HttpClient {
60 // private static class HttpClientInitializer
61 // extends ChannelInitializer<SocketChannel> {
63 // public static class HttpSnoopClientHandler
64 // extends SimpleChannelInboundHandler<HttpObject> {
67 // public void channelRead0(ChannelHandlerContext ctx,
69 // if (msg instanceof HttpResponse) {
70 // HttpResponse response = (HttpResponse) msg;
72 // System.err.println("STATUS: " + response.getStatus());
73 // System.err.println(
74 // "VERSION: " + response.getProtocolVersion());
75 // System.err.println();
77 // if (!response.headers().isEmpty()) {
78 // for (String name : response.headers().names()) {
79 // for (String value : response.headers()
81 // System.err.println(
82 // "HEADER: " + name + " = " + value);
85 // System.err.println();
88 // if (HttpHeaders.isTransferEncodingChunked(response)) {
89 // System.err.println("CHUNKED CONTENT {");
91 // System.err.println("CONTENT {");
94 // if (msg instanceof HttpContent) {
95 // HttpContent content = (HttpContent) msg;
98 // content.content().toString(CharsetUtil.UTF_8));
99 // System.err.flush();
101 // if (content instanceof LastHttpContent) {
102 // System.err.println("} END OF CONTENT");
109 // public void exceptionCaught(ChannelHandlerContext ctx,
110 // Throwable cause) {
111 // cause.printStackTrace();
116 // private final SslContext sslCtx;
118 // public HttpClientInitializer(SslContext sslCtx) {
119 // this.sslCtx = sslCtx;
123 // public void initChannel(SocketChannel ch) {
124 // ChannelPipeline p = ch.pipeline();
126 // // Enable HTTPS if necessary.
127 // if (sslCtx != null) {
128 // p.addLast(sslCtx.newHandler(ch.alloc()));
131 // p.addLast(new HttpClientCodec());
133 // // Remove the following line if you don't want automatic content
135 // p.addLast(new HttpContentDecompressor());
137 // // Uncomment the following line if you don't want to handle
139 // // p.addLast(new HttpObjectAggregator(1048576));
141 // p.addLast(new HttpSnoopClientHandler());
145 // public void connect(String strUrl)
146 // throws URISyntaxException, InterruptedException, SSLException {
147 // URI uri = new URI(strUrl);
149 // String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
150 // String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
152 // int port = uri.getPort();
155 // if ("http".equalsIgnoreCase(scheme)) {
157 // } else if ("https".equalsIgnoreCase(scheme)) {
162 // if (!"http".equalsIgnoreCase(scheme)
163 // && !"https".equalsIgnoreCase(scheme)) {
167 // final boolean ssl = "https".equalsIgnoreCase(scheme);
168 // final SslContext sslCtx;
171 // sslCtx = SslContextBuilder.forClient()
172 // .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
177 // EventLoopGroup group = new NioEventLoopGroup();
180 // Bootstrap b = new Bootstrap();
182 // b.channel(NioSocketChannel.class);
183 // b.handler(new HttpClientInitializer(sslCtx));
185 // Channel ch = b.connect(host, port).sync().channel();
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);
195 // request.headers().set(HttpHeaders.Names.COOKIE,
196 // ClientCookieEncoder.encode(
197 // new DefaultCookie("my-cookie", "foo"),
198 // new DefaultCookie("another-cookie", "bar")));
200 // ch.writeAndFlush(request);
202 // ch.closeFuture().sync();
204 // group.shutdownGracefully();