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.sync;
29  
30  import java.util.Collection;
31  
32  import org.apache.hc.client5.http.AuthenticationStrategy;
33  import org.apache.hc.client5.http.HttpRequestRetryStrategy;
34  import org.apache.hc.client5.http.UserTokenHandler;
35  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
36  import org.apache.hc.client5.http.classic.ExecChainHandler;
37  import org.apache.hc.client5.http.config.ConnectionConfig;
38  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
39  import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
40  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
41  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
42  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
43  import org.apache.hc.client5.testing.SSLTestContexts;
44  import org.apache.hc.core5.http.Header;
45  import org.apache.hc.core5.http.HttpRequestInterceptor;
46  import org.apache.hc.core5.http.HttpResponseInterceptor;
47  import org.apache.hc.core5.http.config.Lookup;
48  import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
49  import org.apache.hc.core5.http.io.SocketConfig;
50  import org.apache.hc.core5.util.Timeout;
51  
52  final class StandardTestClientBuilder implements TestClientBuilder {
53  
54      private final HttpClientBuilder clientBuilder;
55  
56      private Timeout timeout;
57  
58      private HttpClientConnectionManager connectionManager;
59  
60      public StandardTestClientBuilder() {
61          this.clientBuilder = HttpClientBuilder.create();
62      }
63  
64      @Override
65      public ClientProtocolLevel getProtocolLevel() {
66          return ClientProtocolLevel.STANDARD;
67      }
68  
69      @Override
70      public TestClientBuilder setTimeout(final Timeout timeout) {
71          this.timeout = timeout;
72          return this;
73      }
74  
75      @Override
76      public TestClientBuilder setConnectionManager(final HttpClientConnectionManager connectionManager) {
77          this.connectionManager = connectionManager;
78          return this;
79      }
80  
81      @Override
82      public TestClientBuilder addResponseInterceptorFirst(final HttpResponseInterceptor interceptor) {
83          this.clientBuilder.addResponseInterceptorFirst(interceptor);
84          return this;
85      }
86  
87      @Override
88      public TestClientBuilder addResponseInterceptorLast(final HttpResponseInterceptor interceptor) {
89          this.clientBuilder.addResponseInterceptorLast(interceptor);
90          return this;
91      }
92  
93      @Override
94      public TestClientBuilder addRequestInterceptorFirst(final HttpRequestInterceptor interceptor) {
95          this.clientBuilder.addRequestInterceptorFirst(interceptor);
96          return this;
97      }
98  
99      @Override
100     public TestClientBuilder addRequestInterceptorLast(final HttpRequestInterceptor interceptor) {
101         this.clientBuilder.addRequestInterceptorLast(interceptor);
102         return this;
103     }
104 
105     @Override
106     public TestClientBuilder setUserTokenHandler(final UserTokenHandler userTokenHandler) {
107         this.clientBuilder.setUserTokenHandler(userTokenHandler);
108         return this;
109     }
110 
111     @Override
112     public TestClientBuilder setDefaultHeaders(final Collection<? extends Header> defaultHeaders) {
113         this.clientBuilder.setDefaultHeaders(defaultHeaders);
114         return this;
115     }
116 
117     @Override
118     public TestClientBuilder setRetryStrategy(final HttpRequestRetryStrategy retryStrategy) {
119         this.clientBuilder.setRetryStrategy(retryStrategy);
120         return this;
121     }
122 
123     @Override
124     public TestClientBuilder setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
125         this.clientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy);
126         return this;
127     }
128 
129     @Override
130     public TestClientBuilder setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
131         this.clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
132         return this;
133     }
134 
135     @Override
136     public TestClientBuilder setRequestExecutor(final HttpRequestExecutor requestExec) {
137         this.clientBuilder.setRequestExecutor(requestExec);
138         return this;
139     }
140 
141     @Override
142     public TestClientBuilder addExecInterceptorFirst(final String name, final ExecChainHandler interceptor) {
143         this.clientBuilder.addExecInterceptorFirst(name, interceptor);
144         return this;
145     }
146 
147     @Override
148     public TestClientBuilder addExecInterceptorLast(final String name, final ExecChainHandler interceptor) {
149         this.clientBuilder.addExecInterceptorLast(name, interceptor);
150         return this;
151     }
152 
153     @Override
154     public TestClient build() throws Exception {
155         final HttpClientConnectionManager connectionManagerCopy = connectionManager != null ? connectionManager :
156                 PoolingHttpClientConnectionManagerBuilder.create()
157                         .setTlsSocketStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
158                         .setDefaultSocketConfig(SocketConfig.custom()
159                                 .setSoTimeout(timeout)
160                                 .build())
161                         .setDefaultConnectionConfig(ConnectionConfig.custom()
162                                 .setConnectTimeout(timeout)
163                                 .build())
164                         .build();
165 
166         final CloseableHttpClient client = clientBuilder
167                 .setConnectionManager(connectionManagerCopy)
168                 .build();
169         return new TestClient(client, connectionManagerCopy);
170     }
171 
172 }