View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.impl.nio;
29  
30  import org.apache.hc.client5.http.DnsResolver;
31  import org.apache.hc.client5.http.SchemePortResolver;
32  import org.apache.hc.client5.http.ssl.ConscryptClientTlsStrategy;
33  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
34  import org.apache.hc.core5.http.config.RegistryBuilder;
35  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
36  import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
37  import org.apache.hc.core5.pool.PoolReusePolicy;
38  import org.apache.hc.core5.util.ReflectionUtils;
39  import org.apache.hc.core5.util.TimeValue;
40  
41  /**
42   * Builder for {@link PoolingAsyncClientConnectionManager} instances.
43   * <p>
44   * When a particular component is not explicitly set this class will
45   * use its default implementation. System properties will be taken
46   * into account when configuring the default implementations when
47   * {@link #useSystemProperties()} method is called prior to calling
48   * {@link #build()}.
49   * </p>
50   * <ul>
51   *  <li>ssl.TrustManagerFactory.algorithm</li>
52   *  <li>javax.net.ssl.trustStoreType</li>
53   *  <li>javax.net.ssl.trustStore</li>
54   *  <li>javax.net.ssl.trustStoreProvider</li>
55   *  <li>javax.net.ssl.trustStorePassword</li>
56   *  <li>ssl.KeyManagerFactory.algorithm</li>
57   *  <li>javax.net.ssl.keyStoreType</li>
58   *  <li>javax.net.ssl.keyStore</li>
59   *  <li>javax.net.ssl.keyStoreProvider</li>
60   *  <li>javax.net.ssl.keyStorePassword</li>
61   *  <li>https.protocols</li>
62   *  <li>https.cipherSuites</li>
63   * </ul>
64   *
65   * @since 5.0
66   */
67  public class PoolingAsyncClientConnectionManagerBuilder {
68  
69      private TlsStrategy tlsStrategy;
70      private SchemePortResolver schemePortResolver;
71      private DnsResolver dnsResolver;
72      private PoolConcurrencyPolicy poolConcurrencyPolicy;
73      private PoolReusePolicy poolReusePolicy;
74  
75      private boolean systemProperties;
76  
77      private int maxConnTotal;
78      private int maxConnPerRoute;
79  
80      private TimeValue timeToLive;
81      private TimeValue validateAfterInactivity;
82  
83      public static PoolingAsyncClientConnectionManagerBuilder create() {
84          return new PoolingAsyncClientConnectionManagerBuilder();
85      }
86  
87      PoolingAsyncClientConnectionManagerBuilder() {
88          super();
89      }
90  
91      /**
92       * Assigns {@link TlsStrategy} instance for TLS connections.
93       */
94      public final PoolingAsyncClientConnectionManagerBuilder setTlsStrategy(
95              final TlsStrategy tlsStrategy) {
96          this.tlsStrategy = tlsStrategy;
97          return this;
98      }
99  
100     /**
101      * Assigns {@link DnsResolver} instance.
102      */
103     public final PoolingAsyncClientConnectionManagerBuilder setDnsResolver(final DnsResolver dnsResolver) {
104         this.dnsResolver = dnsResolver;
105         return this;
106     }
107 
108     /**
109      * Assigns {@link SchemePortResolver} instance.
110      */
111     public final PoolingAsyncClientConnectionManagerBuilder setSchemePortResolver(final SchemePortResolver schemePortResolver) {
112         this.schemePortResolver = schemePortResolver;
113         return this;
114     }
115 
116     /**
117      * Assigns {@link PoolConcurrencyPolicy} value.
118      */
119     public final PoolingAsyncClientConnectionManagerBuilder setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
120         this.poolConcurrencyPolicy = poolConcurrencyPolicy;
121         return this;
122     }
123 
124     /**
125      * Assigns {@link PoolReusePolicy} value.
126      */
127     public final PoolingAsyncClientConnectionManagerBuilder setConnPoolPolicy(final PoolReusePolicy poolReusePolicy) {
128         this.poolReusePolicy = poolReusePolicy;
129         return this;
130     }
131 
132     /**
133      * Assigns maximum total connection value.
134      */
135     public final PoolingAsyncClientConnectionManagerBuilder setMaxConnTotal(final int maxConnTotal) {
136         this.maxConnTotal = maxConnTotal;
137         return this;
138     }
139 
140     /**
141      * Assigns maximum connection per route value.
142      */
143     public final PoolingAsyncClientConnectionManagerBuilder setMaxConnPerRoute(final int maxConnPerRoute) {
144         this.maxConnPerRoute = maxConnPerRoute;
145         return this;
146     }
147 
148     /**
149      * Sets maximum time to live for persistent connections
150      */
151     public final PoolingAsyncClientConnectionManagerBuilder setConnectionTimeToLive(final TimeValue timeToLive) {
152         this.timeToLive = timeToLive;
153         return this;
154     }
155 
156     /**
157      * Sets period after inactivity after which persistent
158      * connections must be checked to ensure they are still valid.
159      *
160      * @see org.apache.hc.core5.http.io.HttpClientConnection#isStale()
161      */
162     public final PoolingAsyncClientConnectionManagerBuilder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
163         this.validateAfterInactivity = validateAfterInactivity;
164         return this;
165     }
166 
167     /**
168      * Use system properties when creating and configuring default
169      * implementations.
170      */
171     public final PoolingAsyncClientConnectionManagerBuilder useSystemProperties() {
172         this.systemProperties = true;
173         return this;
174     }
175 
176     public PoolingAsyncClientConnectionManager build() {
177         final TlsStrategy tlsStrategyCopy;
178         if (tlsStrategy != null) {
179             tlsStrategyCopy = tlsStrategy;
180         } else {
181             if (ReflectionUtils.determineJRELevel() <= 8 && ConscryptClientTlsStrategy.isSupported()) {
182                 if (systemProperties) {
183                     tlsStrategyCopy = ConscryptClientTlsStrategy.getSystemDefault();
184                 } else {
185                     tlsStrategyCopy = ConscryptClientTlsStrategy.getDefault();
186                 }
187             } else {
188                 if (systemProperties) {
189                     tlsStrategyCopy = DefaultClientTlsStrategy.getSystemDefault();
190                 } else {
191                     tlsStrategyCopy = DefaultClientTlsStrategy.getDefault();
192                 }
193             }
194         }
195         final PoolingAsyncClientConnectionManagerntConnectionManager.html#PoolingAsyncClientConnectionManager">PoolingAsyncClientConnectionManager poolingmgr = new PoolingAsyncClientConnectionManager(
196                 RegistryBuilder.<TlsStrategy>create()
197                         .register("https", tlsStrategyCopy)
198                         .build(),
199                 poolConcurrencyPolicy,
200                 poolReusePolicy,
201                 timeToLive,
202                 schemePortResolver,
203                 dnsResolver);
204         if (validateAfterInactivity != null) {
205             poolingmgr.setValidateAfterInactivity(validateAfterInactivity);
206         }
207         if (maxConnTotal > 0) {
208             poolingmgr.setMaxTotal(maxConnTotal);
209         }
210         if (maxConnPerRoute > 0) {
211             poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
212         }
213         return poolingmgr;
214     }
215 
216 }