View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import static io.netty.handler.codec.http2.Http2FrameLogger.Direction.OUTBOUND;
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  import io.netty.buffer.ByteBuf;
21  import io.netty.channel.ChannelFuture;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.channel.ChannelPromise;
24  
25  /**
26   * Decorator around a {@link Http2FrameWriter} that logs all outbound frames before calling the
27   * writer.
28   */
29  public class Http2OutboundFrameLogger implements Http2FrameWriter {
30      private final Http2FrameWriter writer;
31      private final Http2FrameLogger logger;
32  
33      public Http2OutboundFrameLogger(Http2FrameWriter writer, Http2FrameLogger logger) {
34          this.writer = checkNotNull(writer, "writer");
35          this.logger = checkNotNull(logger, "logger");
36      }
37  
38      @Override
39      public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data,
40              int padding, boolean endStream, ChannelPromise promise) {
41          logger.logData(OUTBOUND, streamId, data, padding, endStream);
42          return writer.writeData(ctx, streamId, data, padding, endStream, promise);
43      }
44  
45      @Override
46      public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
47              Http2Headers headers, int padding, boolean endStream, ChannelPromise promise) {
48          logger.logHeaders(OUTBOUND, streamId, headers, padding, endStream);
49          return writer.writeHeaders(ctx, streamId, headers, padding, endStream, promise);
50      }
51  
52      @Override
53      public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
54              Http2Headers headers, int streamDependency, short weight, boolean exclusive,
55              int padding, boolean endStream, ChannelPromise promise) {
56          logger.logHeaders(OUTBOUND, streamId, headers, streamDependency, weight, exclusive,
57                  padding, endStream);
58          return writer.writeHeaders(ctx, streamId, headers, streamDependency, weight,
59                  exclusive, padding, endStream, promise);
60      }
61  
62      @Override
63      public ChannelFuture writePriority(ChannelHandlerContext ctx, int streamId,
64              int streamDependency, short weight, boolean exclusive, ChannelPromise promise) {
65          logger.logPriority(OUTBOUND, streamId, streamDependency, weight, exclusive);
66          return writer.writePriority(ctx, streamId, streamDependency, weight, exclusive, promise);
67      }
68  
69      @Override
70      public ChannelFuture writeRstStream(ChannelHandlerContext ctx,
71              int streamId, long errorCode, ChannelPromise promise) {
72          logger.logRstStream(OUTBOUND, streamId, errorCode);
73          return writer.writeRstStream(ctx, streamId, errorCode, promise);
74      }
75  
76      @Override
77      public ChannelFuture writeSettings(ChannelHandlerContext ctx,
78              Http2Settings settings, ChannelPromise promise) {
79          logger.logSettings(OUTBOUND, settings);
80          return writer.writeSettings(ctx, settings, promise);
81      }
82  
83      @Override
84      public ChannelFuture writeSettingsAck(ChannelHandlerContext ctx, ChannelPromise promise) {
85          logger.logSettingsAck(OUTBOUND);
86          return writer.writeSettingsAck(ctx, promise);
87      }
88  
89      @Override
90      public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack,
91              ByteBuf data, ChannelPromise promise) {
92          logger.logPing(OUTBOUND, data);
93          return writer.writePing(ctx, ack, data, promise);
94      }
95  
96      @Override
97      public ChannelFuture writePushPromise(ChannelHandlerContext ctx, int streamId,
98              int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) {
99          logger.logPushPromise(OUTBOUND, streamId, promisedStreamId, headers, padding);
100         return writer.writePushPromise(ctx, streamId, promisedStreamId, headers, padding, promise);
101     }
102 
103     @Override
104     public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
105             ByteBuf debugData, ChannelPromise promise) {
106         logger.logGoAway(OUTBOUND, lastStreamId, errorCode, debugData);
107         return writer.writeGoAway(ctx, lastStreamId, errorCode, debugData, promise);
108     }
109 
110     @Override
111     public ChannelFuture writeWindowUpdate(ChannelHandlerContext ctx,
112             int streamId, int windowSizeIncrement, ChannelPromise promise) {
113         logger.logWindowsUpdate(OUTBOUND, streamId, windowSizeIncrement);
114         return writer.writeWindowUpdate(ctx, streamId, windowSizeIncrement, promise);
115     }
116 
117     @Override
118     public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
119             Http2Flags flags, ByteBuf payload, ChannelPromise promise) {
120         logger.logUnknownFrame(OUTBOUND, frameType, streamId, flags, payload);
121         return writer.writeFrame(ctx, frameType, streamId, flags, payload, promise);
122     }
123 
124     @Override
125     public void close() {
126         writer.close();
127     }
128 
129     @Override
130     public Configuration configuration() {
131         return writer.configuration();
132     }
133 }