1 /* 2 * Copyright 2015 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, version 2.0 (the 5 * "License"); you may not use this file except in compliance with the License. You may obtain a 6 * 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 distributed under the License 11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 12 * or implied. See the License for the specific language governing permissions and limitations under 13 * the License. 14 */ 15 package io.netty.handler.codec.http2; 16 17 import io.netty.handler.codec.TooLongFrameException; 18 19 import static io.netty.util.internal.ObjectUtil.checkNotNull; 20 21 /** 22 * A skeletal builder implementation of {@link InboundHttp2ToHttpAdapter} and its subtypes. 23 */ 24 public abstract class AbstractInboundHttp2ToHttpAdapterBuilder< 25 T extends InboundHttp2ToHttpAdapter, B extends AbstractInboundHttp2ToHttpAdapterBuilder<T, B>> { 26 27 private final Http2Connection connection; 28 private int maxContentLength; 29 private boolean validateHttpHeaders; 30 private boolean propagateSettings; 31 32 /** 33 * Creates a new {@link InboundHttp2ToHttpAdapter} builder for the specified {@link Http2Connection}. 34 * 35 * @param connection the object which will provide connection notification events 36 * for the current connection 37 */ 38 protected AbstractInboundHttp2ToHttpAdapterBuilder(Http2Connection connection) { 39 this.connection = checkNotNull(connection, "connection"); 40 } 41 42 @SuppressWarnings("unchecked") 43 protected final B self() { 44 return (B) this; 45 } 46 47 /** 48 * Returns the {@link Http2Connection}. 49 */ 50 protected Http2Connection connection() { 51 return connection; 52 } 53 54 /** 55 * Returns the maximum length of the message content. 56 */ 57 protected int maxContentLength() { 58 return maxContentLength; 59 } 60 61 /** 62 * Specifies the maximum length of the message content. 63 * 64 * @param maxContentLength the maximum length of the message content. If the length of the message content 65 * exceeds this value, a {@link TooLongFrameException} will be raised 66 * @return {@link AbstractInboundHttp2ToHttpAdapterBuilder} the builder for the {@link InboundHttp2ToHttpAdapter} 67 */ 68 protected B maxContentLength(int maxContentLength) { 69 this.maxContentLength = maxContentLength; 70 return self(); 71 } 72 73 /** 74 * Return {@code true} if HTTP header validation should be performed. 75 */ 76 protected boolean isValidateHttpHeaders() { 77 return validateHttpHeaders; 78 } 79 80 /** 81 * Specifies whether validation of HTTP headers should be performed. 82 * 83 * @param validate 84 * <ul> 85 * <li>{@code true} to validate HTTP headers in the http-codec</li> 86 * <li>{@code false} not to validate HTTP headers in the http-codec</li> 87 * </ul> 88 * @return {@link AbstractInboundHttp2ToHttpAdapterBuilder} the builder for the {@link InboundHttp2ToHttpAdapter} 89 */ 90 protected B validateHttpHeaders(boolean validate) { 91 validateHttpHeaders = validate; 92 return self(); 93 } 94 95 /** 96 * Returns {@code true} if a read settings frame should be propagated along the channel pipeline. 97 */ 98 protected boolean isPropagateSettings() { 99 return propagateSettings; 100 } 101 102 /** 103 * Specifies whether a read settings frame should be propagated along the channel pipeline. 104 * 105 * @param propagate if {@code true} read settings will be passed along the pipeline. This can be useful 106 * to clients that need hold off sending data until they have received the settings. 107 * @return {@link AbstractInboundHttp2ToHttpAdapterBuilder} the builder for the {@link InboundHttp2ToHttpAdapter} 108 */ 109 protected B propagateSettings(boolean propagate) { 110 propagateSettings = propagate; 111 return self(); 112 } 113 114 /** 115 * Builds/creates a new {@link InboundHttp2ToHttpAdapter} instance using this builder's current settings. 116 */ 117 protected T build() { 118 final T instance; 119 try { 120 instance = build(connection(), maxContentLength(), 121 isValidateHttpHeaders(), isPropagateSettings()); 122 } catch (Throwable t) { 123 throw new IllegalStateException("failed to create a new InboundHttp2ToHttpAdapter", t); 124 } 125 connection.addListener(instance); 126 return instance; 127 } 128 129 /** 130 * Creates a new {@link InboundHttp2ToHttpAdapter} with the specified properties. 131 */ 132 protected abstract T build(Http2Connection connection, int maxContentLength, 133 boolean validateHttpHeaders, boolean propagateSettings) throws Exception; 134 }