View Javadoc
1   /*
2    * Copyright 2016 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  
17  package io.netty.handler.codec.http2;
18  
19  import io.netty.channel.ChannelDuplexHandler;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelPipeline;
22  import io.netty.util.internal.StringUtil;
23  
24  /**
25   * A {@link ChannelDuplexHandler} providing additional functionality for HTTP/2. Specifically it allows to:
26   * <ul>
27   *     <li>Create new outbound streams using {@link #newStream()}.</li>
28   *     <li>Iterate over all active streams using {@link #forEachActiveStream(Http2FrameStreamVisitor)}.</li>
29   * </ul>
30   *
31   * <p>The {@link Http2FrameCodec} is required to be part of the {@link ChannelPipeline} before this handler is added,
32   * or else an {@link IllegalStateException} will be thrown.
33   */
34  public abstract class Http2ChannelDuplexHandler extends ChannelDuplexHandler {
35  
36      private volatile Http2FrameCodec frameCodec;
37  
38      @Override
39      public final void handlerAdded(ChannelHandlerContext ctx) throws Exception {
40          frameCodec = requireHttp2FrameCodec(ctx);
41          handlerAdded0(ctx);
42      }
43  
44      protected void handlerAdded0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
45          // NOOP
46      }
47  
48      @Override
49      public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
50          try {
51              handlerRemoved0(ctx);
52          } finally {
53              frameCodec = null;
54          }
55      }
56  
57      protected void handlerRemoved0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
58          // NOOP
59      }
60  
61      /**
62       * Creates a new {@link Http2FrameStream} object.
63       *
64       * <p>This method is <em>thread-safe</em>.
65       */
66      public final Http2FrameStream newStream() {
67          Http2FrameCodec codec = frameCodec;
68          if (codec == null) {
69              throw new IllegalStateException(StringUtil.simpleClassName(Http2FrameCodec.class) + " not found." +
70                      " Has the handler been added to a pipeline?");
71          }
72          return codec.newStream();
73      }
74  
75      /**
76       * Allows to iterate over all currently active streams.
77       *
78       * <p>This method may only be called from the eventloop thread.
79       */
80      protected final void forEachActiveStream(Http2FrameStreamVisitor streamVisitor) throws Http2Exception {
81          frameCodec.forEachActiveStream(streamVisitor);
82      }
83  
84      private static Http2FrameCodec requireHttp2FrameCodec(ChannelHandlerContext ctx) {
85          ChannelHandlerContext frameCodecCtx = ctx.pipeline().context(Http2FrameCodec.class);
86          if (frameCodecCtx == null) {
87              throw new IllegalArgumentException(Http2FrameCodec.class.getSimpleName()
88                                                 + " was not found in the channel pipeline.");
89          }
90          return (Http2FrameCodec) frameCodecCtx.handler();
91      }
92  }