View Javadoc
1   /*
2    * Copyright 2020 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.http3;
17  
18  import io.netty.channel.ChannelInitializer;
19  import io.netty.channel.ChannelPipeline;
20  import io.netty.handler.codec.quic.QuicStreamChannel;
21  import io.netty.util.internal.StringUtil;
22  
23  /**
24   * Abstract base class that users can extend to init HTTP/3 request-streams. This initializer
25   * will automatically add HTTP/3 codecs etc to the {@link ChannelPipeline} as well.
26   */
27  public abstract class Http3RequestStreamInitializer extends ChannelInitializer<QuicStreamChannel> {
28  
29      @Override
30      protected final void initChannel(QuicStreamChannel ch) {
31          ChannelPipeline pipeline = ch.pipeline();
32          Http3ConnectionHandler connectionHandler = ch.parent().pipeline().get(Http3ConnectionHandler.class);
33          if (connectionHandler == null) {
34              throw new IllegalStateException("Couldn't obtain the " +
35                      StringUtil.simpleClassName(Http3ConnectionHandler.class) + " of the parent Channel");
36          }
37  
38          Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
39          Http3RequestStreamDecodeStateValidator decodeStateValidator = new Http3RequestStreamDecodeStateValidator();
40  
41          // Add the encoder and decoder in the pipeline so we can handle Http3Frames
42          pipeline.addLast(connectionHandler.newCodec(encodeStateValidator, decodeStateValidator));
43          // Add the handler that will validate what we write and receive on this stream.
44          pipeline.addLast(encodeStateValidator);
45          pipeline.addLast(decodeStateValidator);
46          pipeline.addLast(connectionHandler.newRequestStreamValidationHandler(ch, encodeStateValidator,
47                  decodeStateValidator));
48          initRequestStream(ch);
49      }
50  
51      /**
52       * Init the {@link QuicStreamChannel} to handle {@link Http3RequestStreamFrame}s. At the point of calling this
53       * method it is already valid to write {@link Http3RequestStreamFrame}s as the codec is already in the pipeline.
54       * @param ch    the {QuicStreamChannel} for the request stream.
55       */
56      protected abstract void initRequestStream(QuicStreamChannel ch);
57  }