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