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.protocols.proxy;
24 // import java.nio.charset.StandardCharsets;
25 // import java.util.HashMap;
26 // import java.util.List;
27 // import java.util.Map.Entry;
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;
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;
53 // public class CoapHttpProxyHandler extends ChannelDuplexHandler {
55 // // Proxy converts http request to coaprequest and coapresponse to
57 // private SessionManager sessionManager = null;
59 // private static final AttributeKey<ChannelHandlerContext> keyHttpCtx =
61 // .newInstance("httpCtx");
63 // public CoapHttpProxyHandler(SessionManager sessionManager) {
64 // this.sessionManager = sessionManager;
68 // public void channelRead(ChannelHandlerContext ctx, Object msg)
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());
79 // List<String> didList = queryStringDecoder.parameters().get("di");
81 // if (didList != null) {
82 // ChannelHandlerContext coapClient = sessionManager
83 // .querySession(didList.get(0));
85 // if (coapClient != null) {
86 // List<String> uriList = queryStringDecoder.parameters()
88 // if (uriList != null) {
89 // coapClient.channel().attr(keyHttpCtx).set(ctx);
90 // coapClient.writeAndFlush(httpRequestToCoAPRequest(
91 // uriList.get(0), (HttpRequest) msg));
96 // Logger.d("Unable to find session: " + didList.get(0));
100 // // Prints available sessions to html
102 // ctx.writeAndFlush(printsAvailableSessions())
103 // .addListener(ChannelFutureListener.CLOSE);
107 // if (msg instanceof CoapResponse) {
108 // ctx.channel().attr(keyHttpCtx).get()
110 // coapResponseToHttpResponse((CoapResponse) msg))
111 // .addListener(ChannelFutureListener.CLOSE);
115 // // Pass to upper-layer
116 // super.channelRead(ctx, msg);
120 // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
121 // cause.printStackTrace();
125 // HttpResponse printsAvailableSessions() {
127 // StringBuilder strBuilder = new StringBuilder();
128 // List<String> sessions = sessionManager.getSessions();
130 // strBuilder.append("<html>");
131 // strBuilder.append("<b>Available sessions</b><br>");
133 // for (String session : sessions) {
134 // strBuilder.append(session);
135 // strBuilder.append("<br>");
138 // strBuilder.append("</html>");
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");
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");
162 // CoapRequest httpRequestToCoAPRequest(String uri, HttpRequest httpRequest) {
163 // CoapRequest coapRequest;
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);
176 // throw new IllegalArgumentException();
179 // coapRequest.setUriPath(uri);
181 // return coapRequest;
184 // HttpResponse coapResponseToHttpResponse(CoapResponse coapResponse) {
186 // Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
188 // HashMap<String, Object> rep = cbor
189 // .parsePayloadFromCbor(coapResponse.getPayload(), HashMap.class);
191 // StringBuilder strBuilder = new StringBuilder();
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>");
199 // HttpResponse httpResponse = new DefaultFullHttpResponse(
200 // HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
201 // Unpooled.wrappedBuffer(strBuilder.toString()
202 // .getBytes(StandardCharsets.UTF_8)));
204 // httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE,
205 // "text/html; charset=UTF-8");
207 // // TODO: httpResponse converter required
209 // return httpResponse;