Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / protocols / proxy / CoapHttpProxyHandler.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.protocols.proxy;
23 //
24 // import java.nio.charset.StandardCharsets;
25 // import java.util.HashMap;
26 // import java.util.List;
27 // import java.util.Map.Entry;
28 //
29 // import org.iotivity.cloud.base.SessionManager;
30 // import org.iotivity.cloud.base.protocols.coap.CoapRequest;
31 // import org.iotivity.cloud.base.protocols.coap.CoapResponse;
32 // import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
33 // import org.iotivity.cloud.util.Cbor;
34 // import org.iotivity.cloud.util.Logger;
35 //
36 // import io.netty.buffer.Unpooled;
37 // import io.netty.channel.ChannelDuplexHandler;
38 // import io.netty.channel.ChannelFutureListener;
39 // import io.netty.channel.ChannelHandler.Sharable;
40 // import io.netty.channel.ChannelHandlerContext;
41 // import io.netty.handler.codec.http.DefaultFullHttpResponse;
42 // import io.netty.handler.codec.http.HttpHeaders;
43 // import io.netty.handler.codec.http.HttpMethod;
44 // import io.netty.handler.codec.http.HttpRequest;
45 // import io.netty.handler.codec.http.HttpResponse;
46 // import io.netty.handler.codec.http.HttpResponseStatus;
47 // import io.netty.handler.codec.http.HttpVersion;
48 // import io.netty.handler.codec.http.QueryStringDecoder;
49 // import io.netty.util.AttributeKey;
50 // import io.netty.util.CharsetUtil;
51 //
52 // @Sharable
53 // public class CoapHttpProxyHandler extends ChannelDuplexHandler {
54 //
55 // // Proxy converts http request to coaprequest and coapresponse to
56 // // httpresponse
57 // private SessionManager sessionManager = null;
58 //
59 // private static final AttributeKey<ChannelHandlerContext> keyHttpCtx =
60 // AttributeKey
61 // .newInstance("httpCtx");
62 //
63 // public CoapHttpProxyHandler(SessionManager sessionManager) {
64 // this.sessionManager = sessionManager;
65 // }
66 //
67 // @Override
68 // public void channelRead(ChannelHandlerContext ctx, Object msg)
69 // throws Exception {
70 //
71 // // in case of Receive Request from http
72 // if (msg instanceof HttpRequest) {
73 // // Check uri query param that contains coap device uuid
74 // // then search those and create coapRequest and send
75 // HttpRequest httpRequest = (HttpRequest) msg;
76 // QueryStringDecoder queryStringDecoder = new QueryStringDecoder(
77 // httpRequest.getUri());
78 //
79 // List<String> didList = queryStringDecoder.parameters().get("di");
80 //
81 // if (didList != null) {
82 // ChannelHandlerContext coapClient = sessionManager
83 // .querySession(didList.get(0));
84 //
85 // if (coapClient != null) {
86 // List<String> uriList = queryStringDecoder.parameters()
87 // .get("href");
88 // if (uriList != null) {
89 // coapClient.channel().attr(keyHttpCtx).set(ctx);
90 // coapClient.writeAndFlush(httpRequestToCoAPRequest(
91 // uriList.get(0), (HttpRequest) msg));
92 //
93 // return;
94 // }
95 // } else {
96 // Logger.d("Unable to find session: " + didList.get(0));
97 // }
98 // }
99 //
100 // // Prints available sessions to html
101 //
102 // ctx.writeAndFlush(printsAvailableSessions())
103 // .addListener(ChannelFutureListener.CLOSE);
104 // return;
105 // }
106 //
107 // if (msg instanceof CoapResponse) {
108 // ctx.channel().attr(keyHttpCtx).get()
109 // .writeAndFlush(
110 // coapResponseToHttpResponse((CoapResponse) msg))
111 // .addListener(ChannelFutureListener.CLOSE);
112 // return;
113 // }
114 //
115 // // Pass to upper-layer
116 // super.channelRead(ctx, msg);
117 // }
118 //
119 // @Override
120 // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
121 // cause.printStackTrace();
122 // ctx.close();
123 // }
124 //
125 // HttpResponse printsAvailableSessions() {
126 //
127 // StringBuilder strBuilder = new StringBuilder();
128 // List<String> sessions = sessionManager.getSessions();
129 //
130 // strBuilder.append("<html>");
131 // strBuilder.append("<b>Available sessions</b><br>");
132 //
133 // for (String session : sessions) {
134 // strBuilder.append(session);
135 // strBuilder.append("<br>");
136 // }
137 //
138 // strBuilder.append("</html>");
139 //
140 // HttpResponse response = new DefaultFullHttpResponse(
141 // HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
142 // Unpooled.copiedBuffer(strBuilder.toString(),
143 // CharsetUtil.UTF_8));
144 // response.headers().set(HttpHeaders.Names.CONTENT_TYPE,
145 // "text/html; charset=UTF-8");
146 //
147 // return response;
148 // }
149 //
150 // HttpResponse httpRequestToSendError() {
151 // HttpResponse response = new DefaultFullHttpResponse(
152 // HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND,
153 // Unpooled.copiedBuffer(
154 // "Failure: " + HttpResponseStatus.NOT_FOUND + "\r\n",
155 // CharsetUtil.UTF_8));
156 // response.headers().set(HttpHeaders.Names.CONTENT_TYPE,
157 // "text/html; charset=UTF-8");
158 //
159 // return response;
160 // }
161 //
162 // CoapRequest httpRequestToCoAPRequest(String uri, HttpRequest httpRequest) {
163 // CoapRequest coapRequest;
164 //
165 // // TODO: coapRequest converter required
166 // // coapRequest.getOptions().setUriQuery();
167 // if (httpRequest.getMethod() == HttpMethod.GET) {
168 // coapRequest = new CoapRequest(CoapMethod.GET);
169 // } else if (httpRequest.getMethod() == HttpMethod.PUT) {
170 // coapRequest = new CoapRequest(CoapMethod.PUT);
171 // } else if (httpRequest.getMethod() == HttpMethod.POST) {
172 // coapRequest = new CoapRequest(CoapMethod.POST);
173 // } else if (httpRequest.getMethod() == HttpMethod.DELETE) {
174 // coapRequest = new CoapRequest(CoapMethod.DELETE);
175 // } else {
176 // throw new IllegalArgumentException();
177 // }
178 //
179 // coapRequest.setUriPath(uri);
180 //
181 // return coapRequest;
182 // }
183 //
184 // HttpResponse coapResponseToHttpResponse(CoapResponse coapResponse) {
185 //
186 // Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
187 //
188 // HashMap<String, Object> rep = cbor
189 // .parsePayloadFromCbor(coapResponse.getPayload(), HashMap.class);
190 //
191 // StringBuilder strBuilder = new StringBuilder();
192 //
193 // for (Entry<String, Object> entry : rep.entrySet()) {
194 // String key = entry.getKey();
195 // String value = entry.getValue().toString();
196 // strBuilder.append("Key: " + key + " Value: " + value + "<br>");
197 // }
198 //
199 // HttpResponse httpResponse = new DefaultFullHttpResponse(
200 // HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
201 // Unpooled.wrappedBuffer(strBuilder.toString()
202 // .getBytes(StandardCharsets.UTF_8)));
203 //
204 // httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE,
205 // "text/html; charset=UTF-8");
206 //
207 // // TODO: httpResponse converter required
208 //
209 // return httpResponse;
210 // }
211 // }