查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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  import io.netty.util.internal.UnstableApi;
25  
26  /**
27   * Decorator around a {@link Http2FrameWriter} that logs all outbound frames before calling the
28   * writer.
29   */
30  @UnstableApi
31  public class Http2OutboundFrameLogger implements Http2FrameWriter {
32      private final Http2FrameWriter writer;
33      private final Http2FrameLogger logger;
34  
35      public Http2OutboundFrameLogger(Http2FrameWriter writer, Http2FrameLogger logger) {
36          this.writer = checkNotNull(writer, "writer");
37          this.logger = checkNotNull(logger, "logger");
38      }
39  
40      @Override
41      public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data,
42              int padding, boolean endStream, ChannelPromise promise) {
43          logger.logData(OUTBOUND, ctx, streamId, data, padding, endStream);
44          return writer.writeData(ctx, streamId, data, padding, endStream, promise);
45      }
46  
47      @Override
48      public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
49              Http2Headers headers, int padding, boolean endStream, ChannelPromise promise) {
50          logger.logHeaders(OUTBOUND, ctx, streamId, headers, padding, endStream);
51          return writer.writeHeaders(ctx, streamId, headers, padding, endStream, promise);
52      }
53  
54      @Override
55      public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
56              Http2Headers headers, int streamDependency, short weight, boolean exclusive,
57              int padding, boolean endStream, ChannelPromise promise) {
58          logger.logHeaders(OUTBOUND, ctx, streamId, headers, streamDependency, weight, exclusive,
59                  padding, endStream);
60          return writer.writeHeaders(ctx, streamId, headers, streamDependency, weight,
61                  exclusive, padding, endStream, promise);
62      }
63  
64      @Override
65      public ChannelFuture writePriority(ChannelHandlerContext ctx, int streamId,
66              int streamDependency, short weight, boolean exclusive, ChannelPromise promise) {
67          logger.logPriority(OUTBOUND, ctx, streamId, streamDependency, weight, exclusive);
68          return writer.writePriority(ctx, streamId, streamDependency, weight, exclusive, promise);
69      }
70  
71      @Override
72      public ChannelFuture writeRstStream(ChannelHandlerContext ctx,
73              int streamId, long errorCode, ChannelPromise promise) {
74          logger.logRstStream(OUTBOUND, ctx, streamId, errorCode);
75          return writer.writeRstStream(ctx, streamId, errorCode, promise);
76      }
77  
78      @Override
79      public ChannelFuture writeSettings(ChannelHandlerContext ctx,
80              Http2Settings settings, ChannelPromise promise) {
81          logger.logSettings(OUTBOUND, ctx, settings);
82          return writer.writeSettings(ctx, settings, promise);
83      }
84  
85      @Override
86      public ChannelFuture writeSettingsAck(ChannelHandlerContext ctx, ChannelPromise promise) {
87          logger.logSettingsAck(OUTBOUND, ctx);
88          return writer.writeSettingsAck(ctx, promise);
89      }
90  
91      @Override
92      public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack,
93              long data, ChannelPromise promise) {
94          if (ack) {
95              logger.logPingAck(OUTBOUND, ctx, data);
96          } else {
97              logger.logPing(OUTBOUND, ctx, data);
98          }
99          return writer.writePing(ctx, ack, data, promise);
100     }
101 
102     @Override
103     public ChannelFuture writePushPromise(ChannelHandlerContext ctx, int streamId,
104             int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) {
105         logger.logPushPromise(OUTBOUND, ctx, streamId, promisedStreamId, headers, padding);
106         return writer.writePushPromise(ctx, streamId, promisedStreamId, headers, padding, promise);
107     }
108 
109     @Override
110     public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
111             ByteBuf debugData, ChannelPromise promise) {
112         logger.logGoAway(OUTBOUND, ctx, lastStreamId, errorCode, debugData);
113         return writer.writeGoAway(ctx, lastStreamId, errorCode, debugData, promise);
114     }
115 
116     @Override
117     public ChannelFuture writeWindowUpdate(ChannelHandlerContext ctx,
118             int streamId, int windowSizeIncrement, ChannelPromise promise) {
119         logger.logWindowsUpdate(OUTBOUND, ctx, streamId, windowSizeIncrement);
120         return writer.writeWindowUpdate(ctx, streamId, windowSizeIncrement, promise);
121     }
122 
123     @Override
124     public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
125             Http2Flags flags, ByteBuf payload, ChannelPromise promise) {
126         logger.logUnknownFrame(OUTBOUND, ctx, frameType, streamId, flags, payload);
127         return writer.writeFrame(ctx, frameType, streamId, flags, payload, promise);
128     }
129 
130     @Override
131     public void close() {
132         writer.close();
133     }
134 
135     @Override
136     public Configuration configuration() {
137         return writer.configuration();
138     }
139 }