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