|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 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 | + * You may obtain a 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 |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.http.client; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.ObjectProvider; |
| 25 | +import org.springframework.boot.autoconfigure.http.client.AbstractHttpClientProperties.Ssl; |
| 26 | +import org.springframework.boot.autoconfigure.http.client.AbstractHttpRequestFactoryProperties.Factory; |
| 27 | +import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder; |
| 28 | +import org.springframework.boot.http.client.ClientHttpRequestFactorySettings; |
| 29 | +import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects; |
| 30 | +import org.springframework.boot.http.client.HttpRedirects; |
| 31 | +import org.springframework.boot.ssl.SslBundle; |
| 32 | +import org.springframework.boot.ssl.SslBundles; |
| 33 | +import org.springframework.util.StringUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * Helper class to create {@link ClientHttpRequestFactoryBuilder} and |
| 37 | + * {@link ClientHttpRequestFactorySettings}. |
| 38 | + * |
| 39 | + * @author Phillip Webb |
| 40 | + */ |
| 41 | +class ClientHttpRequestFactories { |
| 42 | + |
| 43 | + private final ObjectProvider<SslBundles> sslBundles; |
| 44 | + |
| 45 | + private final AbstractHttpRequestFactoryProperties[] orderedProperties; |
| 46 | + |
| 47 | + ClientHttpRequestFactories(ObjectProvider<SslBundles> sslBundles, |
| 48 | + AbstractHttpRequestFactoryProperties... orderedProperties) { |
| 49 | + this.sslBundles = sslBundles; |
| 50 | + this.orderedProperties = orderedProperties; |
| 51 | + } |
| 52 | + |
| 53 | + ClientHttpRequestFactoryBuilder<?> builder(ClassLoader classLoader) { |
| 54 | + Factory factory = getProperty(AbstractHttpRequestFactoryProperties::getFactory); |
| 55 | + return (factory != null) ? factory.builder() : ClientHttpRequestFactoryBuilder.detect(classLoader); |
| 56 | + } |
| 57 | + |
| 58 | + ClientHttpRequestFactorySettings settings() { |
| 59 | + HttpRedirects redirects = getProperty(AbstractHttpRequestFactoryProperties::getRedirects); |
| 60 | + Duration connectTimeout = getProperty(AbstractHttpRequestFactoryProperties::getConnectTimeout); |
| 61 | + Duration readTimeout = getProperty(AbstractHttpRequestFactoryProperties::getReadTimeout); |
| 62 | + String sslBundleName = getProperty(AbstractHttpRequestFactoryProperties::getSsl, Ssl::getBundle, |
| 63 | + StringUtils::hasLength); |
| 64 | + SslBundle sslBundle = (StringUtils.hasLength(sslBundleName)) |
| 65 | + ? this.sslBundles.getObject().getBundle(sslBundleName) : null; |
| 66 | + return new ClientHttpRequestFactorySettings(Redirects.of(redirects), connectTimeout, readTimeout, sslBundle); |
| 67 | + } |
| 68 | + |
| 69 | + private <T> T getProperty(Function<AbstractHttpRequestFactoryProperties, T> accessor) { |
| 70 | + return getProperty(accessor, Function.identity(), Objects::nonNull); |
| 71 | + } |
| 72 | + |
| 73 | + private <P, T> T getProperty(Function<AbstractHttpRequestFactoryProperties, P> accessor, Function<P, T> extractor, |
| 74 | + Predicate<T> predicate) { |
| 75 | + for (AbstractHttpRequestFactoryProperties properties : this.orderedProperties) { |
| 76 | + P value = accessor.apply(properties); |
| 77 | + T extracted = (value != null) ? extractor.apply(value) : null; |
| 78 | + if (predicate.test(extracted)) { |
| 79 | + return extracted; |
| 80 | + } |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments