|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/* |
| 17 | + * ==================================================================== |
| 18 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 19 | + * or more contributor license agreements. See the NOTICE file |
| 20 | + * distributed with this work for additional information |
| 21 | + * regarding copyright ownership. The ASF licenses this file |
| 22 | + * to you under the Apache License, Version 2.0 (the |
| 23 | + * "License"); you may not use this file except in compliance |
| 24 | + * with the License. You may obtain a copy of the License at |
| 25 | + * |
| 26 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | + * |
| 28 | + * Unless required by applicable law or agreed to in writing, |
| 29 | + * software distributed under the License is distributed on an |
| 30 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 31 | + * KIND, either express or implied. See the License for the |
| 32 | + * specific language governing permissions and limitations |
| 33 | + * under the License. |
| 34 | + * ==================================================================== |
| 35 | + * |
| 36 | + * This software consists of voluntary contributions made by many |
| 37 | + * individuals on behalf of the Apache Software Foundation. For more |
| 38 | + * information on the Apache Software Foundation, please see |
| 39 | + * <http://www.apache.org/>. |
| 40 | + * |
| 41 | + */ |
| 42 | + |
| 43 | +package software.amazon.awssdk.http.apache5.internal.conn; |
| 44 | + |
| 45 | +import org.apache.hc.client5.http.DnsResolver; |
| 46 | +import org.apache.hc.client5.http.HttpRoute; |
| 47 | +import org.apache.hc.client5.http.SchemePortResolver; |
| 48 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
| 49 | +import org.apache.hc.client5.http.config.TlsConfig; |
| 50 | +import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator; |
| 51 | +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; |
| 52 | +import org.apache.hc.client5.http.io.HttpClientConnectionOperator; |
| 53 | +import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; |
| 54 | +import org.apache.hc.client5.http.ssl.TlsSocketStrategy; |
| 55 | +import org.apache.hc.core5.function.Resolver; |
| 56 | +import org.apache.hc.core5.http.HttpHost; |
| 57 | +import org.apache.hc.core5.http.URIScheme; |
| 58 | +import org.apache.hc.core5.http.config.RegistryBuilder; |
| 59 | +import org.apache.hc.core5.http.io.SocketConfig; |
| 60 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 61 | + |
| 62 | +/** |
| 63 | + * This is a fork of {@link PoolingHttpClientConnectionManagerBuilder} from Apache 5. The purpose of this forked class is to |
| 64 | + * enable usage of the {@link SafePoolingHttpClientConnectionManager} to enable the workaround for |
| 65 | + * https://github.com/aws/aws-sdk-java-v2/issues/6786. |
| 66 | + */ |
| 67 | +// This a direct copy of PoolingHttpClientConnectionManagerBuilder with minor changes to remove methods we don't use and |
| 68 | +// updates to follow our style guide. |
| 69 | +@SdkInternalApi |
| 70 | +public final class SafePoolingHttpClientConnectionManagerBuilder { |
| 71 | + |
| 72 | + private TlsSocketStrategy tlsSocketStrategy; |
| 73 | + private SchemePortResolver schemePortResolver; |
| 74 | + private DnsResolver dnsResolver; |
| 75 | + private Resolver<HttpRoute, SocketConfig> socketConfigResolver; |
| 76 | + private Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver; |
| 77 | + private Resolver<HttpHost, TlsConfig> tlsConfigResolver; |
| 78 | + |
| 79 | + private int maxConnTotal; |
| 80 | + private int maxConnPerRoute; |
| 81 | + |
| 82 | + public static SafePoolingHttpClientConnectionManagerBuilder create() { |
| 83 | + return new SafePoolingHttpClientConnectionManagerBuilder(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets {@link TlsSocketStrategy} instance. |
| 88 | + * |
| 89 | + * @return this instance. |
| 90 | + */ |
| 91 | + public SafePoolingHttpClientConnectionManagerBuilder setTlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy) { |
| 92 | + this.tlsSocketStrategy = tlsSocketStrategy; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets {@link DnsResolver} instance. |
| 98 | + * |
| 99 | + * @return this instance. |
| 100 | + */ |
| 101 | + public SafePoolingHttpClientConnectionManagerBuilder setDnsResolver(DnsResolver dnsResolver) { |
| 102 | + this.dnsResolver = dnsResolver; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets {@link SchemePortResolver} instance. |
| 108 | + * |
| 109 | + * @return this instance. |
| 110 | + */ |
| 111 | + public SafePoolingHttpClientConnectionManagerBuilder setSchemePortResolver(SchemePortResolver schemePortResolver) { |
| 112 | + this.schemePortResolver = schemePortResolver; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets maximum total connection value. |
| 118 | + * |
| 119 | + * @return this instance. |
| 120 | + */ |
| 121 | + public SafePoolingHttpClientConnectionManagerBuilder setMaxConnTotal(int maxConnTotal) { |
| 122 | + this.maxConnTotal = maxConnTotal; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets maximum connection per route value. |
| 128 | + * |
| 129 | + * @return this instance. |
| 130 | + */ |
| 131 | + public SafePoolingHttpClientConnectionManagerBuilder setMaxConnPerRoute(int maxConnPerRoute) { |
| 132 | + this.maxConnPerRoute = maxConnPerRoute; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Sets the same {@link SocketConfig} for all routes. |
| 138 | + * |
| 139 | + * @return this instance. |
| 140 | + */ |
| 141 | + public SafePoolingHttpClientConnectionManagerBuilder setDefaultSocketConfig(SocketConfig config) { |
| 142 | + this.socketConfigResolver = route -> config; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets {@link Resolver} of {@link SocketConfig} on a per route basis. |
| 148 | + * |
| 149 | + * @return this instance. |
| 150 | + * @since 5.2 |
| 151 | + */ |
| 152 | + public SafePoolingHttpClientConnectionManagerBuilder setSocketConfigResolver( |
| 153 | + Resolver<HttpRoute, SocketConfig> socketConfigResolver) { |
| 154 | + this.socketConfigResolver = socketConfigResolver; |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Sets the same {@link ConnectionConfig} for all routes. |
| 160 | + * |
| 161 | + * @return this instance. |
| 162 | + * @since 5.2 |
| 163 | + */ |
| 164 | + public SafePoolingHttpClientConnectionManagerBuilder setDefaultConnectionConfig(ConnectionConfig config) { |
| 165 | + this.connectionConfigResolver = route -> config; |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Sets {@link Resolver} of {@link ConnectionConfig} on a per route basis. |
| 171 | + * |
| 172 | + * @return this instance. |
| 173 | + * @since 5.2 |
| 174 | + */ |
| 175 | + public SafePoolingHttpClientConnectionManagerBuilder setConnectionConfigResolver( |
| 176 | + Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver) { |
| 177 | + this.connectionConfigResolver = connectionConfigResolver; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Sets the same {@link TlsConfig} for all hosts. |
| 183 | + * |
| 184 | + * @return this instance. |
| 185 | + * @since 5.2 |
| 186 | + */ |
| 187 | + public SafePoolingHttpClientConnectionManagerBuilder setDefaultTlsConfig(TlsConfig config) { |
| 188 | + this.tlsConfigResolver = host -> config; |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Sets {@link Resolver} of {@link TlsConfig} on a per host basis. |
| 194 | + * |
| 195 | + * @return this instance. |
| 196 | + * @since 5.2 |
| 197 | + */ |
| 198 | + public SafePoolingHttpClientConnectionManagerBuilder setTlsConfigResolver( |
| 199 | + Resolver<HttpHost, TlsConfig> tlsConfigResolver) { |
| 200 | + this.tlsConfigResolver = tlsConfigResolver; |
| 201 | + return this; |
| 202 | + } |
| 203 | + |
| 204 | + protected HttpClientConnectionOperator createConnectionOperator(SchemePortResolver schemePortResolver, |
| 205 | + DnsResolver dnsResolver, |
| 206 | + TlsSocketStrategy tlsSocketStrategy) { |
| 207 | + return new DefaultHttpClientConnectionOperator(schemePortResolver, dnsResolver, |
| 208 | + RegistryBuilder.<TlsSocketStrategy>create() |
| 209 | + .register(URIScheme.HTTPS.id, tlsSocketStrategy) |
| 210 | + .build()); |
| 211 | + } |
| 212 | + |
| 213 | + public SafePoolingHttpClientConnectionManager build() { |
| 214 | + TlsSocketStrategy tlsSocketStrategyCopy; |
| 215 | + if (tlsSocketStrategy != null) { |
| 216 | + tlsSocketStrategyCopy = tlsSocketStrategy; |
| 217 | + } else { |
| 218 | + tlsSocketStrategyCopy = DefaultClientTlsStrategy.createDefault(); |
| 219 | + } |
| 220 | + |
| 221 | + SafePoolingHttpClientConnectionManager poolingmgr = new SafePoolingHttpClientConnectionManager( |
| 222 | + createConnectionOperator(schemePortResolver, dnsResolver, tlsSocketStrategyCopy)); |
| 223 | + poolingmgr.setSocketConfigResolver(socketConfigResolver); |
| 224 | + poolingmgr.setConnectionConfigResolver(connectionConfigResolver); |
| 225 | + poolingmgr.setTlsConfigResolver(tlsConfigResolver); |
| 226 | + if (maxConnTotal > 0) { |
| 227 | + poolingmgr.setMaxTotal(maxConnTotal); |
| 228 | + } |
| 229 | + if (maxConnPerRoute > 0) { |
| 230 | + poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute); |
| 231 | + } |
| 232 | + return poolingmgr; |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments