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