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.core5.testing.classic;
29  
30  import java.io.IOException;
31  import java.util.concurrent.atomic.AtomicReference;
32  
33  import javax.net.ssl.SSLContext;
34  
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpHost;
39  import org.apache.hc.core5.http.config.CharCodingConfig;
40  import org.apache.hc.core5.http.config.Http1Config;
41  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
42  import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
43  import org.apache.hc.core5.http.impl.HttpProcessors;
44  import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
45  import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
46  import org.apache.hc.core5.http.io.HttpClientConnection;
47  import org.apache.hc.core5.http.io.SocketConfig;
48  import org.apache.hc.core5.http.protocol.HttpContext;
49  import org.apache.hc.core5.http.protocol.HttpProcessor;
50  import org.apache.hc.core5.io.CloseMode;
51  import org.apache.hc.core5.net.URIAuthority;
52  import org.apache.hc.core5.pool.PoolReusePolicy;
53  import org.apache.hc.core5.pool.StrictConnPool;
54  import org.apache.hc.core5.util.TimeValue;
55  
56  public class ClassicTestClient {
57  
58      private final SSLContext sslContext;
59      private final SocketConfig socketConfig;
60  
61      private final AtomicReference<HttpRequester> requesterRef;
62  
63      public ClassicTestClient(final SSLContext sslContext, final SocketConfig socketConfig) {
64          super();
65          this.sslContext = sslContext;
66          this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
67          this.requesterRef = new AtomicReference<>(null);
68      }
69  
70      public ClassicTestClient(final SocketConfig socketConfig) {
71          this(null, socketConfig);
72      }
73  
74      public ClassicTestClient() {
75          this(null, null);
76      }
77  
78      public void start() {
79          start(null);
80      }
81  
82      public void start(final HttpProcessor httpProcessor) {
83          if (requesterRef.get() == null) {
84              final HttpRequestExecutorExecutor.html#HttpRequestExecutor">HttpRequestExecutor requestExecutor = new HttpRequestExecutor(
85                      HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE,
86                      DefaultConnectionReuseStrategy.INSTANCE,
87                      LoggingHttp1StreamListener.INSTANCE);
88              final StrictConnPool<HttpHost, HttpClientConnection> connPool = new StrictConnPool<>(
89                      20,
90                      50,
91                      TimeValue.NEG_ONE_MILLISECOND,
92                      PoolReusePolicy.LIFO,
93                      LoggingConnPoolListener.INSTANCE);
94              final HttpRequesterotstrap/HttpRequester.html#HttpRequester">HttpRequester requester = new HttpRequester(
95                      requestExecutor,
96                      httpProcessor != null ? httpProcessor : HttpProcessors.client(),
97                      connPool,
98                      socketConfig,
99                      new LoggingBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT),
100                     sslContext != null ? sslContext.getSocketFactory() : null,
101                     null,
102                     null,
103                     DefaultAddressResolver.INSTANCE);
104             requesterRef.compareAndSet(null, requester);
105         } else {
106             throw new IllegalStateException("Requester has already been started");
107         }
108     }
109 
110     public void shutdown(final CloseMode closeMode) {
111         final HttpRequester requester = requesterRef.getAndSet(null);
112         if (requester != null) {
113             requester.close(closeMode);
114         }
115     }
116 
117     public ClassicHttpResponse execute(
118             final HttpHost targetHost,
119             final ClassicHttpRequest request,
120             final HttpContext context) throws HttpException, IOException {
121         final HttpRequester requester = this.requesterRef.get();
122         if (requester == null) {
123             throw new IllegalStateException("Requester has not been started");
124         }
125         if (request.getAuthority() == null) {
126             request.setAuthority(new URIAuthority(targetHost));
127         }
128         request.setScheme(targetHost.getSchemeName());
129         return requester.execute(targetHost, request, socketConfig.getSoTimeout(), context);
130     }
131 
132 }