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 org.apache.hc.core5.annotation.Experimental;
30  import org.apache.hc.core5.function.Callback;
31  import org.apache.hc.core5.function.Decorator;
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.Http1Config;
36  import org.apache.hc.core5.http.impl.Http1StreamListener;
37  import org.apache.hc.core5.http.impl.HttpProcessors;
38  import org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory;
39  import org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory;
40  import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
41  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
42  import org.apache.hc.core5.http.protocol.HttpProcessor;
43  import org.apache.hc.core5.pool.ConnPoolListener;
44  import org.apache.hc.core5.pool.DefaultDisposalCallback;
45  import org.apache.hc.core5.pool.LaxConnPool;
46  import org.apache.hc.core5.pool.ManagedConnPool;
47  import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
48  import org.apache.hc.core5.pool.PoolReusePolicy;
49  import org.apache.hc.core5.pool.StrictConnPool;
50  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
51  import org.apache.hc.core5.reactor.IOReactorConfig;
52  import org.apache.hc.core5.reactor.IOSession;
53  import org.apache.hc.core5.reactor.IOSessionListener;
54  import org.apache.hc.core5.util.Timeout;
55  
56  /**
57   * {@link HttpAsyncRequester} bootstrap.
58   *
59   * @since 5.0
60   */
61  public class AsyncRequesterBootstrap {
62  
63      private IOReactorConfig ioReactorConfig;
64      private Http1Config http1Config;
65      private CharCodingConfig charCodingConfig;
66      private HttpProcessor httpProcessor;
67      private ConnectionReuseStrategy connStrategy;
68      private int defaultMaxPerRoute;
69      private int maxTotal;
70      private Timeout timeToLive;
71      private PoolReusePolicy poolReusePolicy;
72      private PoolConcurrencyPolicy poolConcurrencyPolicy;
73      private TlsStrategy tlsStrategy;
74      private Timeout handshakeTimeout;
75      private Decorator<IOSession> ioSessionDecorator;
76      private Callback<Exception> exceptionCallback;
77      private IOSessionListener sessionListener;
78      private Http1StreamListener streamListener;
79      private ConnPoolListener<HttpHost> connPoolListener;
80  
81      private AsyncRequesterBootstrap() {
82      }
83  
84      public static AsyncRequesterBootstrap bootstrap() {
85          return new AsyncRequesterBootstrap();
86      }
87  
88      /**
89       * Sets I/O reactor configuration.
90       */
91      public final AsyncRequesterBootstrap setIOReactorConfig(final IOReactorConfig ioReactorConfig) {
92          this.ioReactorConfig = ioReactorConfig;
93          return this;
94      }
95  
96      /**
97       * Sets HTTP/1.1 protocol parameters
98       */
99      public final AsyncRequesterBootstrap setHttp1Config(final Http1Config http1Config) {
100         this.http1Config = http1Config;
101         return this;
102     }
103 
104     /**
105      * Sets message char coding.
106      */
107     public final AsyncRequesterBootstrap setCharCodingConfig(final CharCodingConfig charCodingConfig) {
108         this.charCodingConfig = charCodingConfig;
109         return this;
110     }
111 
112     /**
113      * Assigns {@link HttpProcessor} instance.
114      */
115     public final AsyncRequesterBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
116         this.httpProcessor = httpProcessor;
117         return this;
118     }
119 
120     /**
121      * Assigns {@link ConnectionReuseStrategy} instance.
122      */
123     public final AsyncRequesterBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
124         this.connStrategy = connStrategy;
125         return this;
126     }
127 
128     public final AsyncRequesterBootstrap setDefaultMaxPerRoute(final int defaultMaxPerRoute) {
129         this.defaultMaxPerRoute = defaultMaxPerRoute;
130         return this;
131     }
132 
133     public final AsyncRequesterBootstrap setMaxTotal(final int maxTotal) {
134         this.maxTotal = maxTotal;
135         return this;
136     }
137 
138     public final AsyncRequesterBootstrap setTimeToLive(final Timeout timeToLive) {
139         this.timeToLive = timeToLive;
140         return this;
141     }
142 
143     /**
144      * Assigns {@link PoolReusePolicy} instance.
145      */
146     public final AsyncRequesterBootstrap setPoolReusePolicy(final PoolReusePolicy poolReusePolicy) {
147         this.poolReusePolicy = poolReusePolicy;
148         return this;
149     }
150 
151     /**
152      * Assigns {@link PoolConcurrencyPolicy} instance.
153      */
154     @Experimental
155     public final AsyncRequesterBootstrap setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
156         this.poolConcurrencyPolicy = poolConcurrencyPolicy;
157         return this;
158     }
159 
160     /**
161      * Assigns {@link TlsStrategy} instance.
162      */
163     public final AsyncRequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
164         this.tlsStrategy = tlsStrategy;
165         return this;
166     }
167 
168     public final AsyncRequesterBootstrap setTlsHandshakeTimeout(final Timeout handshakeTimeout) {
169         this.handshakeTimeout = handshakeTimeout;
170         return this;
171     }
172 
173     /**
174      * Assigns {@link IOSession} {@link Decorator} instance.
175      */
176     public final AsyncRequesterBootstrap setIOSessionDecorator(final Decorator<IOSession> ioSessionDecorator) {
177         this.ioSessionDecorator = ioSessionDecorator;
178         return this;
179     }
180 
181     /**
182      * Assigns {@link Exception} {@link Callback} instance.
183      */
184     public final AsyncRequesterBootstrap setExceptionCallback(final Callback<Exception> exceptionCallback) {
185         this.exceptionCallback = exceptionCallback;
186         return this;
187     }
188 
189     /**
190      * Assigns {@link IOSessionListener} instance.
191      */
192     public final AsyncRequesterBootstrap setIOSessionListener(final IOSessionListener sessionListener) {
193         this.sessionListener = sessionListener;
194         return this;
195     }
196 
197     /**
198      * Assigns {@link Http1StreamListener} instance.
199      */
200     public final AsyncRequesterBootstrap setStreamListener(final Http1StreamListener streamListener) {
201         this.streamListener = streamListener;
202         return this;
203     }
204 
205     /**
206      * Assigns {@link ConnPoolListener} instance.
207      */
208     public final AsyncRequesterBootstrap setConnPoolListener(final ConnPoolListener<HttpHost> connPoolListener) {
209         this.connPoolListener = connPoolListener;
210         return this;
211     }
212 
213     public HttpAsyncRequester create() {
214         final ManagedConnPool<HttpHost, IOSession> connPool;
215         switch (poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
216             case LAX:
217                 connPool = new LaxConnPool<>(
218                         defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
219                         timeToLive,
220                         poolReusePolicy,
221                         new DefaultDisposalCallback<IOSession>(),
222                         connPoolListener);
223                 break;
224             case STRICT:
225             default:
226                 connPool = new StrictConnPool<>(
227                         defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
228                         maxTotal > 0 ? maxTotal : 50,
229                         timeToLive,
230                         poolReusePolicy,
231                         new DefaultDisposalCallback<IOSession>(),
232                         connPoolListener);
233                 break;
234         }
235         final ClientHttp1StreamDuplexerFactoryactory.html#ClientHttp1StreamDuplexerFactory">ClientHttp1StreamDuplexerFactory streamDuplexerFactory = new ClientHttp1StreamDuplexerFactory(
236                 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
237                 http1Config != null ? http1Config : Http1Config.DEFAULT,
238                 charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT,
239                 connStrategy,
240                 null,
241                 null,
242                 streamListener);
243         final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttp1IOEventHandlerFactory(
244                 streamDuplexerFactory,
245                 tlsStrategy != null ? tlsStrategy : new BasicClientTlsStrategy(),
246                 handshakeTimeout);
247         return new HttpAsyncRequester(
248                 ioReactorConfig,
249                 ioEventHandlerFactory,
250                 ioSessionDecorator,
251                 exceptionCallback,
252                 sessionListener,
253                 connPool);
254     }
255 
256 }