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