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  package org.apache.hc.core5.http.impl.bootstrap;
28  
29  import javax.net.ssl.SSLSocketFactory;
30  
31  import org.apache.hc.core5.annotation.Experimental;
32  import org.apache.hc.core5.http.ConnectionReuseStrategy;
33  import org.apache.hc.core5.http.HttpHost;
34  import org.apache.hc.core5.http.config.CharCodingConfig;
35  import org.apache.hc.core5.http.config.H1Config;
36  import org.apache.hc.core5.http.config.SocketConfig;
37  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
38  import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
39  import org.apache.hc.core5.http.impl.Http1StreamListener;
40  import org.apache.hc.core5.http.impl.HttpProcessors;
41  import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
42  import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
43  import org.apache.hc.core5.http.io.HttpClientConnection;
44  import org.apache.hc.core5.http.io.HttpConnectionFactory;
45  import org.apache.hc.core5.http.protocol.HttpProcessor;
46  import org.apache.hc.core5.pool.ConnPoolListener;
47  import org.apache.hc.core5.pool.LaxConnPool;
48  import org.apache.hc.core5.pool.ManagedConnPool;
49  import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
50  import org.apache.hc.core5.pool.PoolReusePolicy;
51  import org.apache.hc.core5.pool.StrictConnPool;
52  import org.apache.hc.core5.util.Timeout;
53  
54  /**
55   * @since 5.0
56   */
57  public class RequesterBootstrap {
58  
59      private HttpProcessor httpProcessor;
60      private ConnectionReuseStrategy connReuseStrategy;
61      private SocketConfig socketConfig;
62      private HttpConnectionFactory<? extends HttpClientConnection> connectFactory;
63      private SSLSocketFactory sslSocketFactory;
64      private int defaultMaxPerRoute;
65      private int maxTotal;
66      private Timeout timeToLive;
67      private PoolReusePolicy poolReusePolicy;
68      private PoolConcurrencyPolicy poolConcurrencyPolicy;
69      private Http1StreamListener streamListener;
70      private ConnPoolListener<HttpHost> connPoolListener;
71  
72      private RequesterBootstrap() {
73      }
74  
75      public static RequesterBootstrap bootstrap() {
76          return new RequesterBootstrap();
77      }
78  
79      /**
80       * Assigns {@link HttpProcessor} instance.
81       */
82      public final RequesterBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
83          this.httpProcessor = httpProcessor;
84          return this;
85      }
86  
87      /**
88       * Assigns {@link ConnectionReuseStrategy} instance.
89       */
90      public final RequesterBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
91          this.connReuseStrategy = connStrategy;
92          return this;
93      }
94  
95      /**
96       * Sets socket configuration.
97       */
98      public final RequesterBootstrap setSocketConfig(final SocketConfig socketConfig) {
99          this.socketConfig = socketConfig;
100         return this;
101     }
102 
103     public final RequesterBootstrap setConnectionFactory(final HttpConnectionFactory<? extends HttpClientConnection> connectFactory) {
104         this.connectFactory = connectFactory;
105         return this;
106     }
107 
108     public final RequesterBootstrap setSslSocketFactory(final SSLSocketFactory sslSocketFactory) {
109         this.sslSocketFactory = sslSocketFactory;
110         return this;
111     }
112 
113     public final RequesterBootstrap setDefaultMaxPerRoute(final int defaultMaxPerRoute) {
114         this.defaultMaxPerRoute = defaultMaxPerRoute;
115         return this;
116     }
117 
118     public final RequesterBootstrap setMaxTotal(final int maxTotal) {
119         this.maxTotal = maxTotal;
120         return this;
121     }
122 
123     public final RequesterBootstrap setTimeToLive(final Timeout timeToLive) {
124         this.timeToLive = timeToLive;
125         return this;
126     }
127 
128     public final RequesterBootstrap setPoolReusePolicy(final PoolReusePolicy poolReusePolicy) {
129         this.poolReusePolicy = poolReusePolicy;
130         return this;
131     }
132 
133     @Experimental
134     public final RequesterBootstrap setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
135         this.poolConcurrencyPolicy = poolConcurrencyPolicy;
136         return this;
137     }
138 
139     public final RequesterBootstrap setStreamListener(final Http1StreamListener streamListener) {
140         this.streamListener = streamListener;
141         return this;
142     }
143 
144     public final RequesterBootstrap setConnPoolListener(final ConnPoolListener<HttpHost> connPoolListener) {
145         this.connPoolListener = connPoolListener;
146         return this;
147     }
148 
149     public HttpRequester create() {
150         final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(
151                 HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE,
152                 connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE,
153                 streamListener);
154         final ManagedConnPool<HttpHost, HttpClientConnection> connPool;
155         switch (poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
156             case LAX:
157                 connPool = new LaxConnPool<>(
158                         defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
159                         timeToLive,
160                         poolReusePolicy,
161                         connPoolListener);
162                 break;
163             case STRICT:
164             default:
165                 connPool = new StrictConnPool<>(
166                         defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
167                         maxTotal > 0 ? maxTotal : 50,
168                         timeToLive,
169                         poolReusePolicy,
170                         connPoolListener);
171                 break;
172         }
173         return new HttpRequester(
174                 requestExecutor,
175                 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
176                 connPool,
177                 socketConfig != null ? socketConfig : SocketConfig.DEFAULT,
178                 connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(
179                         H1Config.DEFAULT, CharCodingConfig.DEFAULT),
180                 sslSocketFactory,
181                 DefaultAddressResolver.INSTANCE);
182     }
183 
184 }