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    *   https://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, ctx, 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, ctx, 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, ctx, 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, ctx, 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, ctx, 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, ctx, settings);
80          return writer.writeSettings(ctx, settings, promise);
81      }
82  
83      @Override
84      public ChannelFuture writeSettingsAck(ChannelHandlerContext ctx, ChannelPromise promise) {
85          logger.logSettingsAck(OUTBOUND, ctx);
86          return writer.writeSettingsAck(ctx, promise);
87      }
88  
89      @Override
90      public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack,
91              long data, ChannelPromise promise) {
92          if (ack) {
93              logger.logPingAck(OUTBOUND, ctx, data);
94          } else {
95              logger.logPing(OUTBOUND, ctx, data);
96          }
97          return writer.writePing(ctx, ack, data, promise);
98      }
99  
100     @Override
101     public ChannelFuture writePushPromise(ChannelHandlerContext ctx, int streamId,
102             int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) {
103         logger.logPushPromise(OUTBOUND, ctx, streamId, promisedStreamId, headers, padding);
104         return writer.writePushPromise(ctx, streamId, promisedStreamId, headers, padding, promise);
105     }
106 
107     @Override
108     public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
109             ByteBuf debugData, ChannelPromise promise) {
110         logger.logGoAway(OUTBOUND, ctx, lastStreamId, errorCode, debugData);
111         return writer.writeGoAway(ctx, lastStreamId, errorCode, debugData, promise);
112     }
113 
114     @Override
115     public ChannelFuture writeWindowUpdate(ChannelHandlerContext ctx,
116             int streamId, int windowSizeIncrement, ChannelPromise promise) {
117         logger.logWindowsUpdate(OUTBOUND, ctx, streamId, windowSizeIncrement);
118         return writer.writeWindowUpdate(ctx, streamId, windowSizeIncrement, promise);
119     }
120 
121     @Override
122     public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
123             Http2Flags flags, ByteBuf payload, ChannelPromise promise) {
124         logger.logUnknownFrame(OUTBOUND, ctx, frameType, streamId, flags, payload);
125         return writer.writeFrame(ctx, frameType, streamId, flags, payload, promise);
126     }
127 
128     @Override
129     public void close() {
130         writer.close();
131     }
132 
133     @Override
134     public Configuration configuration() {
135         return writer.configuration();
136     }
137 }