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