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.nio;
29  
30  import java.io.IOException;
31  import java.util.concurrent.Future;
32  
33  import javax.net.ssl.SSLContext;
34  
35  import org.apache.hc.core5.concurrent.FutureContribution;
36  import org.apache.hc.core5.concurrent.BasicFuture;
37  import org.apache.hc.core5.concurrent.FutureCallback;
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.DefaultConnectionReuseStrategy;
42  import org.apache.hc.core5.http.impl.HttpProcessors;
43  import org.apache.hc.core5.http.protocol.HttpProcessor;
44  import org.apache.hc.core5.reactor.IOReactorConfig;
45  import org.apache.hc.core5.reactor.IOSession;
46  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
47  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
48  import org.apache.hc.core5.util.Timeout;
49  
50  public class Http1TestClient extends AsyncRequester  {
51  
52      private final SSLContext sslContext;
53      private final SSLSessionInitializer sslSessionInitializer;
54      private final SSLSessionVerifier sslSessionVerifier;
55  
56      public Http1TestClient(
57              final IOReactorConfig ioReactorConfig,
58              final SSLContext sslContext,
59              final SSLSessionInitializer sslSessionInitializer,
60              final SSLSessionVerifier sslSessionVerifier) throws IOException {
61          super(ioReactorConfig);
62          this.sslContext = sslContext;
63          this.sslSessionInitializer = sslSessionInitializer;
64          this.sslSessionVerifier = sslSessionVerifier;
65      }
66  
67      public Http1TestClient() throws IOException {
68          this(IOReactorConfig.DEFAULT, null, null, null);
69      }
70  
71      public void start(
72              final HttpProcessor httpProcessor,
73              final Http1Config http1Config) throws IOException {
74          execute(new InternalClientHttp1EventHandlerFactory(
75                  httpProcessor,
76                  http1Config,
77                  CharCodingConfig.DEFAULT,
78                  DefaultConnectionReuseStrategy.INSTANCE,
79                  sslContext,
80                  sslSessionInitializer,
81                  sslSessionVerifier));
82      }
83  
84      public void start(final Http1Config http1Config) throws IOException {
85          start(HttpProcessors.client(), http1Config);
86      }
87  
88      public void start() throws IOException {
89          start(Http1Config.DEFAULT);
90      }
91  
92      public Future<ClientSessionEndpoint> connect(
93              final HttpHost host,
94              final Timeout timeout,
95              final FutureCallback<ClientSessionEndpoint> callback) {
96          final BasicFuture<ClientSessionEndpoint> future = new BasicFuture<>(callback);
97          requestSession(host, timeout, new FutureContribution<IOSession>(future) {
98  
99              @Override
100             public void completed(final IOSession session) {
101                 future.completed(new ClientSessionEndpoint(session));
102             }
103 
104         });
105         return future;
106     }
107 
108     public Future<ClientSessionEndpoint> connect(final HttpHost host, final Timeout timeout) {
109         return connect(host, timeout, null);
110     }
111 
112     public Future<ClientSessionEndpoint> connect(final String hostname, final int port, final Timeout timeout) {
113         return connect(new HttpHost(hostname, port), timeout, null);
114     }
115 
116 }