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