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 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
15 // package org.iotivity.cloud.base;
17 // import java.net.URI;
18 // import java.net.URISyntaxException;
20 // import javax.net.ssl.SSLException;
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;
50 // public class HttpClient {
52 // private static class HttpClientInitializer
53 // extends ChannelInitializer<SocketChannel> {
55 // public static class HttpSnoopClientHandler
56 // extends SimpleChannelInboundHandler<HttpObject> {
59 // public void channelRead0(ChannelHandlerContext ctx,
61 // if (msg instanceof HttpResponse) {
62 // HttpResponse response = (HttpResponse) msg;
64 // System.err.println("STATUS: " + response.getStatus());
65 // System.err.println(
66 // "VERSION: " + response.getProtocolVersion());
67 // System.err.println();
69 // if (!response.headers().isEmpty()) {
70 // for (String name : response.headers().names()) {
71 // for (String value : response.headers()
73 // System.err.println(
74 // "HEADER: " + name + " = " + value);
77 // System.err.println();
80 // if (HttpHeaders.isTransferEncodingChunked(response)) {
81 // System.err.println("CHUNKED CONTENT {");
83 // System.err.println("CONTENT {");
86 // if (msg instanceof HttpContent) {
87 // HttpContent content = (HttpContent) msg;
90 // content.content().toString(CharsetUtil.UTF_8));
91 // System.err.flush();
93 // if (content instanceof LastHttpContent) {
94 // System.err.println("} END OF CONTENT");
101 // public void exceptionCaught(ChannelHandlerContext ctx,
102 // Throwable cause) {
103 // cause.printStackTrace();
108 // private final SslContext sslCtx;
110 // public HttpClientInitializer(SslContext sslCtx) {
111 // this.sslCtx = sslCtx;
115 // public void initChannel(SocketChannel ch) {
116 // ChannelPipeline p = ch.pipeline();
118 // // Enable HTTPS if necessary.
119 // if (sslCtx != null) {
120 // p.addLast(sslCtx.newHandler(ch.alloc()));
123 // p.addLast(new HttpClientCodec());
125 // // Remove the following line if you don't want automatic content
127 // p.addLast(new HttpContentDecompressor());
129 // // Uncomment the following line if you don't want to handle
131 // // p.addLast(new HttpObjectAggregator(1048576));
133 // p.addLast(new HttpSnoopClientHandler());
137 // public void connect(String strUrl)
138 // throws URISyntaxException, InterruptedException, SSLException {
139 // URI uri = new URI(strUrl);
141 // String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
142 // String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
144 // int port = uri.getPort();
147 // if ("http".equalsIgnoreCase(scheme)) {
149 // } else if ("https".equalsIgnoreCase(scheme)) {
154 // if (!"http".equalsIgnoreCase(scheme)
155 // && !"https".equalsIgnoreCase(scheme)) {
159 // final boolean ssl = "https".equalsIgnoreCase(scheme);
160 // final SslContext sslCtx;
163 // sslCtx = SslContextBuilder.forClient()
164 // .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
169 // EventLoopGroup group = new NioEventLoopGroup();
172 // Bootstrap b = new Bootstrap();
174 // b.channel(NioSocketChannel.class);
175 // b.handler(new HttpClientInitializer(sslCtx));
177 // Channel ch = b.connect(host, port).sync().channel();
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);
187 // request.headers().set(HttpHeaders.Names.COOKIE,
188 // ClientCookieEncoder.encode(
189 // new DefaultCookie("my-cookie", "foo"),
190 // new DefaultCookie("another-cookie", "bar")));
192 // ch.writeAndFlush(request);
194 // ch.closeFuture().sync();
196 // group.shutdownGracefully();