View Javadoc
1   /*
2    * Copyright 2017 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  package io.netty.handler.codec.http2;
17  
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelHandlerAdapter;
20  import io.netty.util.internal.UnstableApi;
21  
22  import static io.netty.util.internal.ObjectUtil.checkNotNull;
23  
24  /**
25   * A builder for {@link Http2MultiplexCodec}.
26   */
27  @UnstableApi
28  public class Http2MultiplexCodecBuilder
29          extends AbstractHttp2ConnectionHandlerBuilder<Http2MultiplexCodec, Http2MultiplexCodecBuilder> {
30  
31      final ChannelHandler childHandler;
32      private ChannelHandler upgradeStreamHandler;
33  
34      Http2MultiplexCodecBuilder(boolean server, ChannelHandler childHandler) {
35          server(server);
36          this.childHandler = checkSharable(checkNotNull(childHandler, "childHandler"));
37      }
38  
39      private static ChannelHandler checkSharable(ChannelHandler handler) {
40          if ((handler instanceof ChannelHandlerAdapter && !((ChannelHandlerAdapter) handler).isSharable()) &&
41                  !handler.getClass().isAnnotationPresent(ChannelHandler.Sharable.class)) {
42              throw new IllegalArgumentException("The handler must be Sharable");
43          }
44          return handler;
45      }
46  
47      /**
48       * Creates a builder for a HTTP/2 client.
49       *
50       * @param childHandler the handler added to channels for remotely-created streams. It must be
51       *     {@link ChannelHandler.Sharable}.
52       */
53      public static Http2MultiplexCodecBuilder forClient(ChannelHandler childHandler) {
54          return new Http2MultiplexCodecBuilder(false, childHandler);
55      }
56  
57      /**
58       * Creates a builder for a HTTP/2 server.
59       *
60       * @param childHandler the handler added to channels for remotely-created streams. It must be
61       *     {@link ChannelHandler.Sharable}.
62       */
63      public static Http2MultiplexCodecBuilder forServer(ChannelHandler childHandler) {
64          return new Http2MultiplexCodecBuilder(true, childHandler);
65      }
66  
67      @Override
68      public Http2Settings initialSettings() {
69          return super.initialSettings();
70      }
71  
72      @Override
73      public Http2MultiplexCodecBuilder initialSettings(Http2Settings settings) {
74          return super.initialSettings(settings);
75      }
76  
77      @Override
78      public long gracefulShutdownTimeoutMillis() {
79          return super.gracefulShutdownTimeoutMillis();
80      }
81  
82      @Override
83      public Http2MultiplexCodecBuilder gracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis) {
84          return super.gracefulShutdownTimeoutMillis(gracefulShutdownTimeoutMillis);
85      }
86  
87      public Http2MultiplexCodecBuilder withUpgradeStreamHandler(ChannelHandler upgradeStreamHandler) {
88          if (this.isServer()) {
89              throw new IllegalArgumentException("Server codecs don't use an extra handler for the upgrade stream");
90          }
91          this.upgradeStreamHandler = upgradeStreamHandler;
92          return this;
93      }
94  
95      @Override
96      public boolean isServer() {
97          return super.isServer();
98      }
99  
100     @Override
101     public int maxReservedStreams() {
102         return super.maxReservedStreams();
103     }
104 
105     @Override
106     public Http2MultiplexCodecBuilder maxReservedStreams(int maxReservedStreams) {
107         return super.maxReservedStreams(maxReservedStreams);
108     }
109 
110     @Override
111     public boolean isValidateHeaders() {
112         return super.isValidateHeaders();
113     }
114 
115     @Override
116     public Http2MultiplexCodecBuilder validateHeaders(boolean validateHeaders) {
117         return super.validateHeaders(validateHeaders);
118     }
119 
120     @Override
121     public Http2FrameLogger frameLogger() {
122         return super.frameLogger();
123     }
124 
125     @Override
126     public Http2MultiplexCodecBuilder frameLogger(Http2FrameLogger frameLogger) {
127         return super.frameLogger(frameLogger);
128     }
129 
130     @Override
131     public boolean encoderEnforceMaxConcurrentStreams() {
132         return super.encoderEnforceMaxConcurrentStreams();
133     }
134 
135     @Override
136     public Http2MultiplexCodecBuilder encoderEnforceMaxConcurrentStreams(boolean encoderEnforceMaxConcurrentStreams) {
137         return super.encoderEnforceMaxConcurrentStreams(encoderEnforceMaxConcurrentStreams);
138     }
139 
140     @Override
141     public Http2HeadersEncoder.SensitivityDetector headerSensitivityDetector() {
142         return super.headerSensitivityDetector();
143     }
144 
145     @Override
146     public Http2MultiplexCodecBuilder headerSensitivityDetector(
147             Http2HeadersEncoder.SensitivityDetector headerSensitivityDetector) {
148         return super.headerSensitivityDetector(headerSensitivityDetector);
149     }
150 
151     @Override
152     public Http2MultiplexCodecBuilder encoderIgnoreMaxHeaderListSize(boolean ignoreMaxHeaderListSize) {
153         return super.encoderIgnoreMaxHeaderListSize(ignoreMaxHeaderListSize);
154     }
155 
156     @Override
157     public Http2MultiplexCodecBuilder initialHuffmanDecodeCapacity(int initialHuffmanDecodeCapacity) {
158         return super.initialHuffmanDecodeCapacity(initialHuffmanDecodeCapacity);
159     }
160 
161     @Override
162     public Http2MultiplexCodec build() {
163         return super.build();
164     }
165 
166     @Override
167     protected Http2MultiplexCodec build(
168             Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings) {
169         return new Http2MultiplexCodec(encoder, decoder, initialSettings, childHandler, upgradeStreamHandler);
170     }
171 }