630eca397713f95c636149580d58434b4fd4b679
[iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / protocols / http / HttpLogHandler.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.http;
23
24 import org.iotivity.cloud.util.Log;
25
26 import io.netty.channel.ChannelDuplexHandler;
27 import io.netty.channel.ChannelHandler.Sharable;
28 import io.netty.handler.codec.http.HttpContent;
29 import io.netty.handler.codec.http.HttpObject;
30 import io.netty.handler.codec.http.HttpRequest;
31 import io.netty.handler.codec.http.HttpResponse;
32 import io.netty.handler.codec.http.HttpUtil;
33 import io.netty.handler.codec.http.LastHttpContent;
34 import io.netty.util.CharsetUtil;
35 import io.netty.channel.ChannelHandlerContext;
36 import io.netty.channel.ChannelPromise;
37
38 /**
39  * This class provides a set of APIs to print out logs
40  * for HTTP request and response.
41  */
42 @Sharable
43 public class HttpLogHandler extends ChannelDuplexHandler {
44
45     @Override
46     public void channelActive(ChannelHandlerContext ctx) throws Exception {
47         Log.v(ctx.channel().id().asLongText().substring(26)
48                 + " HTTP Connected, Address: "
49                 + ctx.channel().remoteAddress().toString());
50
51         ctx.fireChannelActive();
52     }
53
54     @Override
55     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
56         Log.v(ctx.channel().id().asLongText().substring(26)
57                 + " HTTP Disconnected, Address: "
58                 + ctx.channel().remoteAddress().toString());
59
60         ctx.fireChannelInactive();
61     }
62
63     @Override
64     public void channelRead(ChannelHandlerContext ctx, Object msg)
65             throws Exception {
66
67         if (!(msg instanceof HttpObject)) {
68             Log.v("Non-HTTP message has been received to the HC proxy:\n"
69                     + msg.toString());
70         }
71
72         if (msg instanceof HttpRequest) {
73
74             StringBuilder contentStrBuilder = new StringBuilder();
75             ctx.channel().attr(HCProxyProcessor.ctxStrContent)
76                     .set(contentStrBuilder);
77
78             HttpRequest httpRequest = (HttpRequest) msg;
79
80             Log.v(httpRequest.toString());
81
82             if (HttpUtil.isTransferEncodingChunked(httpRequest)) {
83                 Log.v("BEGINNING OF HTTP CHUNKED CONTENT");
84             } else {
85                 Log.v("BEGINNING OF HTTP CONTENT");
86             }
87         }
88
89         if (msg instanceof HttpContent) {
90
91             HttpContent content = (HttpContent) msg;
92
93             StringBuilder contentStrBuilder = ctx.channel()
94                     .attr(HCProxyProcessor.ctxStrContent).get();
95             contentStrBuilder
96                     .append(content.content().toString(CharsetUtil.UTF_8));
97
98             if (content instanceof LastHttpContent) {
99
100                 Log.v(contentStrBuilder.toString());
101
102                 Log.v("END OF HTTP CONTENT");
103
104                 contentStrBuilder.setLength(0);
105             }
106         }
107
108         ctx.fireChannelRead(msg);
109     }
110
111     @Override
112     public void write(ChannelHandlerContext ctx, Object msg,
113             ChannelPromise promise) {
114
115         if (!(msg instanceof HttpObject)) {
116             Log.v("Non-HTTP message has been sent from the HC proxy:\n"
117                     + msg.toString());
118         }
119
120         if (msg instanceof HttpResponse) {
121
122             StringBuilder contentStrBuilder = ctx.channel()
123                     .attr(HCProxyProcessor.ctxStrContent).get();
124
125             HttpResponse httpResponse = (HttpResponse) msg;
126
127             if (contentStrBuilder.length() > 0) {
128                 Log.v(httpResponse.toString() + "\n\n"
129                         + contentStrBuilder.toString());
130             } else {
131                 Log.v(httpResponse.toString());
132             }
133         }
134
135         ctx.writeAndFlush(msg);
136     }
137 }