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.protocols.proxy;
17 // import java.nio.charset.StandardCharsets;
18 // import java.util.HashMap;
19 // import java.util.List;
20 // import java.util.Map.Entry;
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;
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;
46 // public class CoapHttpProxyHandler extends ChannelDuplexHandler {
48 // // Proxy converts http request to coaprequest and coapresponse to
50 // private SessionManager sessionManager = null;
52 // private static final AttributeKey<ChannelHandlerContext> keyHttpCtx =
54 // .newInstance("httpCtx");
56 // public CoapHttpProxyHandler(SessionManager sessionManager) {
57 // this.sessionManager = sessionManager;
61 // public void channelRead(ChannelHandlerContext ctx, Object msg)
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());
72 // List<String> didList = queryStringDecoder.parameters().get("di");
74 // if (didList != null) {
75 // ChannelHandlerContext coapClient = sessionManager
76 // .querySession(didList.get(0));
78 // if (coapClient != null) {
79 // List<String> uriList = queryStringDecoder.parameters()
81 // if (uriList != null) {
82 // coapClient.channel().attr(keyHttpCtx).set(ctx);
83 // coapClient.writeAndFlush(httpRequestToCoAPRequest(
84 // uriList.get(0), (HttpRequest) msg));
89 // Logger.d("Unable to find session: " + didList.get(0));
93 // // Prints available sessions to html
95 // ctx.writeAndFlush(printsAvailableSessions())
96 // .addListener(ChannelFutureListener.CLOSE);
100 // if (msg instanceof CoapResponse) {
101 // ctx.channel().attr(keyHttpCtx).get()
103 // coapResponseToHttpResponse((CoapResponse) msg))
104 // .addListener(ChannelFutureListener.CLOSE);
108 // // Pass to upper-layer
109 // super.channelRead(ctx, msg);
113 // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
114 // cause.printStackTrace();
118 // HttpResponse printsAvailableSessions() {
120 // StringBuilder strBuilder = new StringBuilder();
121 // List<String> sessions = sessionManager.getSessions();
123 // strBuilder.append("<html>");
124 // strBuilder.append("<b>Available sessions</b><br>");
126 // for (String session : sessions) {
127 // strBuilder.append(session);
128 // strBuilder.append("<br>");
131 // strBuilder.append("</html>");
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");
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");
155 // CoapRequest httpRequestToCoAPRequest(String uri, HttpRequest httpRequest) {
156 // CoapRequest coapRequest;
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);
169 // throw new IllegalArgumentException();
172 // coapRequest.setUriPath(uri);
174 // return coapRequest;
177 // HttpResponse coapResponseToHttpResponse(CoapResponse coapResponse) {
179 // Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
181 // HashMap<String, Object> rep = cbor
182 // .parsePayloadFromCbor(coapResponse.getPayload(), HashMap.class);
184 // StringBuilder strBuilder = new StringBuilder();
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>");
192 // HttpResponse httpResponse = new DefaultFullHttpResponse(
193 // HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
194 // Unpooled.wrappedBuffer(strBuilder.toString()
195 // .getBytes(StandardCharsets.UTF_8)));
197 // httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE,
198 // "text/html; charset=UTF-8");
200 // // TODO: httpResponse converter required
202 // return httpResponse;