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 java.util.Collection;
31  
32  import org.apache.hc.client5.http.AuthenticationStrategy;
33  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
34  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
35  import org.apache.hc.client5.http.impl.async.H2AsyncClientBuilder;
36  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
37  import org.apache.hc.client5.testing.SSLTestContexts;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpRequestInterceptor;
40  import org.apache.hc.core5.http.HttpResponseInterceptor;
41  import org.apache.hc.core5.http.config.Lookup;
42  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
43  import org.apache.hc.core5.http2.config.H2Config;
44  import org.apache.hc.core5.reactor.IOReactorConfig;
45  import org.apache.hc.core5.util.Timeout;
46  
47  final class H2OnlyTestClientBuilder implements TestAsyncClientBuilder {
48  
49      private final H2AsyncClientBuilder clientBuilder;
50  
51      private Timeout timeout;
52      private TlsStrategy tlsStrategy;
53      private H2Config h2Config;
54  
55      public H2OnlyTestClientBuilder() {
56          this.clientBuilder = H2AsyncClientBuilder.create();
57      }
58  
59      @Override
60      public ClientProtocolLevel getProtocolLevel() {
61          return ClientProtocolLevel.H2_ONLY;
62      }
63  
64      @Override
65      public TestAsyncClientBuilder setTimeout(final Timeout timeout) {
66          this.timeout = timeout;
67          return this;
68      }
69  
70      @Override
71      public TestAsyncClientBuilder addResponseInterceptorFirst(final HttpResponseInterceptor interceptor) {
72          this.clientBuilder.addResponseInterceptorFirst(interceptor);
73          return this;
74      }
75  
76      @Override
77      public TestAsyncClientBuilder addResponseInterceptorLast(final HttpResponseInterceptor interceptor) {
78          this.clientBuilder.addResponseInterceptorLast(interceptor);
79          return this;
80      }
81  
82      @Override
83      public TestAsyncClientBuilder addRequestInterceptorFirst(final HttpRequestInterceptor interceptor) {
84          this.clientBuilder.addRequestInterceptorFirst(interceptor);
85          return this;
86      }
87  
88      @Override
89      public TestAsyncClientBuilder addRequestInterceptorLast(final HttpRequestInterceptor interceptor) {
90          this.clientBuilder.addRequestInterceptorLast(interceptor);
91          return this;
92      }
93  
94      @Override
95      public TestAsyncClientBuilder setTlsStrategy(final TlsStrategy tlsStrategy) {
96          this.tlsStrategy = tlsStrategy;
97          return this;
98      }
99  
100     @Override
101     public TestAsyncClientBuilder setH2Config(final H2Config h2Config) {
102         this.h2Config = h2Config;
103         return this;
104     }
105 
106     @Override
107     public TestAsyncClientBuilder setDefaultHeaders(final Collection<? extends Header> defaultHeaders) {
108         this.clientBuilder.setDefaultHeaders(defaultHeaders);
109         return this;
110     }
111 
112     @Override
113     public TestAsyncClientBuilder setTargetAuthenticationStrategy(final AuthenticationStrategy targetAuthStrategy) {
114         this.clientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy);
115         return this;
116     }
117 
118     @Override
119     public TestAsyncClientBuilder setDefaultAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
120         this.clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
121         return this;
122     }
123 
124     @Override
125     public TestAsyncClient build() throws Exception {
126         final CloseableHttpAsyncClient client = clientBuilder
127                 .setTlsStrategy(tlsStrategy != null ? tlsStrategy : new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
128                 .setIOReactorConfig(IOReactorConfig.custom()
129                         .setSoTimeout(timeout)
130                         .build())
131                 .setH2Config(h2Config)
132                 .build();
133         return new TestAsyncClient(client, null);
134     }
135 
136 }