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