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.testing.extension.async;
29  
30  import org.apache.hc.client5.http.config.ConnectionConfig;
31  import org.apache.hc.client5.http.config.TlsConfig;
32  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
33  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
34  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
35  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
36  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
37  import org.apache.hc.client5.testing.SSLTestContexts;
38  import org.apache.hc.core5.http.config.Http1Config;
39  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
40  import org.apache.hc.core5.http2.config.H2Config;
41  import org.apache.hc.core5.reactor.IOReactorConfig;
42  import org.apache.hc.core5.util.Timeout;
43  
44  final class MinimalTestClientBuilder implements TestAsyncClientBuilder {
45  
46      private final PoolingAsyncClientConnectionManagerBuilder connectionManagerBuilder;
47  
48      private Timeout timeout;
49      private TlsStrategy tlsStrategy;
50      private Http1Config http1Config;
51      private H2Config h2Config;
52  
53      public MinimalTestClientBuilder() {
54          this.connectionManagerBuilder = PoolingAsyncClientConnectionManagerBuilder.create();
55      }
56  
57      @Override
58      public ClientProtocolLevel getProtocolLevel() {
59          return ClientProtocolLevel.MINIMAL;
60      }
61  
62      @Override
63      public TestAsyncClientBuilder setTimeout(final Timeout timeout) {
64          this.timeout = timeout;
65          return this;
66      }
67  
68      @Override
69      public TestAsyncClientBuilder setTlsStrategy(final TlsStrategy tlsStrategy) {
70          this.tlsStrategy = tlsStrategy;
71          return this;
72      }
73  
74      @Override
75      public TestAsyncClientBuilder setDefaultTlsConfig(final TlsConfig tlsConfig) {
76          this.connectionManagerBuilder.setDefaultTlsConfig(tlsConfig);
77          return this;
78      }
79  
80      @Override
81      public TestAsyncClientBuilder setHttp1Config(final Http1Config http1Config) {
82          this.http1Config = http1Config;
83          return this;
84      }
85  
86      @Override
87      public TestAsyncClientBuilder setH2Config(final H2Config h2Config) {
88          this.h2Config = h2Config;
89          return this;
90      }
91  
92      @Override
93      public TestAsyncClient build() throws Exception {
94          final PoolingAsyncClientConnectionManager connectionManager = connectionManagerBuilder
95                  .setTlsStrategy(tlsStrategy != null ? tlsStrategy : new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
96                  .setDefaultConnectionConfig(ConnectionConfig.custom()
97                          .setSocketTimeout(timeout)
98                          .setConnectTimeout(timeout)
99                          .build())
100                 .build();
101         final CloseableHttpAsyncClient client = HttpAsyncClients.createMinimal(
102                         h2Config,
103                         http1Config,
104                         IOReactorConfig.custom()
105                                 .setSoTimeout(timeout)
106                                 .build(),
107                 connectionManager);
108         return new TestAsyncClient(client, connectionManager);
109     }
110 
111 }