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  
17  package io.netty.handler.codec.http2;
18  
19  import io.netty.util.internal.UnstableApi;
20  
21  import static io.netty.util.internal.ObjectUtil.checkNotNull;
22  
23  /**
24   * Builder for the {@link Http2FrameCodec}.
25   */
26  @UnstableApi
27  public class Http2FrameCodecBuilder extends
28          AbstractHttp2ConnectionHandlerBuilder<Http2FrameCodec, Http2FrameCodecBuilder> {
29  
30      private Http2FrameWriter frameWriter;
31  
32      Http2FrameCodecBuilder(boolean server) {
33          server(server);
34      }
35  
36      /**
37       * Creates a builder for a HTTP/2 client.
38       */
39      public static Http2FrameCodecBuilder forClient() {
40          return new Http2FrameCodecBuilder(false);
41      }
42  
43      /**
44       * Creates a builder for a HTTP/2 server.
45       */
46      public static Http2FrameCodecBuilder forServer() {
47          return new Http2FrameCodecBuilder(true);
48      }
49  
50      // For testing only.
51      Http2FrameCodecBuilder frameWriter(Http2FrameWriter frameWriter) {
52          this.frameWriter = checkNotNull(frameWriter, "frameWriter");
53          return this;
54      }
55  
56      @Override
57      public Http2Settings initialSettings() {
58          return super.initialSettings();
59      }
60  
61      @Override
62      public Http2FrameCodecBuilder initialSettings(Http2Settings settings) {
63          return super.initialSettings(settings);
64      }
65  
66      @Override
67      public long gracefulShutdownTimeoutMillis() {
68          return super.gracefulShutdownTimeoutMillis();
69      }
70  
71      @Override
72      public Http2FrameCodecBuilder gracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis) {
73          return super.gracefulShutdownTimeoutMillis(gracefulShutdownTimeoutMillis);
74      }
75  
76      @Override
77      public boolean isServer() {
78          return super.isServer();
79      }
80  
81      @Override
82      public int maxReservedStreams() {
83          return super.maxReservedStreams();
84      }
85  
86      @Override
87      public Http2FrameCodecBuilder maxReservedStreams(int maxReservedStreams) {
88          return super.maxReservedStreams(maxReservedStreams);
89      }
90  
91      @Override
92      public boolean isValidateHeaders() {
93          return super.isValidateHeaders();
94      }
95  
96      @Override
97      public Http2FrameCodecBuilder validateHeaders(boolean validateHeaders) {
98          return super.validateHeaders(validateHeaders);
99      }
100 
101     @Override
102     public Http2FrameLogger frameLogger() {
103         return super.frameLogger();
104     }
105 
106     @Override
107     public Http2FrameCodecBuilder frameLogger(Http2FrameLogger frameLogger) {
108         return super.frameLogger(frameLogger);
109     }
110 
111     @Override
112     public boolean encoderEnforceMaxConcurrentStreams() {
113         return super.encoderEnforceMaxConcurrentStreams();
114     }
115 
116     @Override
117     public Http2FrameCodecBuilder encoderEnforceMaxConcurrentStreams(boolean encoderEnforceMaxConcurrentStreams) {
118         return super.encoderEnforceMaxConcurrentStreams(encoderEnforceMaxConcurrentStreams);
119     }
120 
121     @Override
122     public Http2HeadersEncoder.SensitivityDetector headerSensitivityDetector() {
123         return super.headerSensitivityDetector();
124     }
125 
126     @Override
127     public Http2FrameCodecBuilder headerSensitivityDetector(
128             Http2HeadersEncoder.SensitivityDetector headerSensitivityDetector) {
129         return super.headerSensitivityDetector(headerSensitivityDetector);
130     }
131 
132     @Override
133     public Http2FrameCodecBuilder encoderIgnoreMaxHeaderListSize(boolean ignoreMaxHeaderListSize) {
134         return super.encoderIgnoreMaxHeaderListSize(ignoreMaxHeaderListSize);
135     }
136 
137     @Override
138     public Http2FrameCodecBuilder initialHuffmanDecodeCapacity(int initialHuffmanDecodeCapacity) {
139         return super.initialHuffmanDecodeCapacity(initialHuffmanDecodeCapacity);
140     }
141 
142     /**
143      * Build a {@link Http2FrameCodec} object.
144      */
145     @Override
146     public Http2FrameCodec build() {
147         Http2FrameWriter frameWriter = this.frameWriter;
148         if (frameWriter != null) {
149             // This is to support our tests and will never be executed by the user as frameWriter(...)
150             // is package-private.
151             DefaultHttp2Connection connection = new DefaultHttp2Connection(isServer(), maxReservedStreams());
152             Long maxHeaderListSize = initialSettings().maxHeaderListSize();
153             Http2FrameReader frameReader = new DefaultHttp2FrameReader(maxHeaderListSize == null ?
154                     new DefaultHttp2HeadersDecoder(true) :
155                     new DefaultHttp2HeadersDecoder(true, maxHeaderListSize));
156 
157             if (frameLogger() != null) {
158                 frameWriter = new Http2OutboundFrameLogger(frameWriter, frameLogger());
159                 frameReader = new Http2InboundFrameLogger(frameReader, frameLogger());
160             }
161             Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
162             if (encoderEnforceMaxConcurrentStreams()) {
163                 encoder = new StreamBufferingEncoder(encoder);
164             }
165             Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader);
166 
167             return build(decoder, encoder, initialSettings());
168         }
169         return super.build();
170     }
171 
172     @Override
173     protected Http2FrameCodec build(
174             Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings) {
175         return new Http2FrameCodec(encoder, decoder, initialSettings);
176     }
177 }