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.function.Supplier;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.config.CharCodingConfig;
41  import org.apache.hc.core5.http.config.Http1Config;
42  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
43  import org.apache.hc.core5.http.protocol.HttpProcessor;
44  import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
45  import org.apache.hc.core5.http2.HttpVersionPolicy;
46  import org.apache.hc.core5.http2.config.H2Config;
47  import org.apache.hc.core5.http2.impl.H2Processors;
48  import org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory;
49  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
50  import org.apache.hc.core5.reactor.IOReactorConfig;
51  import org.apache.hc.core5.reactor.IOSession;
52  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
53  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
54  import org.apache.hc.core5.util.Args;
55  import org.apache.hc.core5.util.Timeout;
56  
57  public class H2TestClient extends AsyncRequester {
58  
59      private final SSLContext sslContext;
60      private final SSLSessionInitializer sslSessionInitializer;
61      private final SSLSessionVerifier sslSessionVerifier;
62      private final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry;
63  
64      public H2TestClient(
65              final IOReactorConfig ioReactorConfig,
66              final SSLContext sslContext,
67              final SSLSessionInitializer sslSessionInitializer,
68              final SSLSessionVerifier sslSessionVerifier) throws IOException {
69          super(ioReactorConfig);
70          this.sslContext = sslContext;
71          this.sslSessionInitializer = sslSessionInitializer;
72          this.sslSessionVerifier = sslSessionVerifier;
73          this.registry = new RequestHandlerRegistry<>();
74      }
75  
76      public H2TestClient() throws IOException {
77          this(IOReactorConfig.DEFAULT, null, null, null);
78      }
79  
80      public void register(final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
81          Args.notNull(uriPattern, "URI pattern");
82          Args.notNull(supplier, "Supplier");
83          registry.register(null, uriPattern, supplier);
84      }
85  
86      public void start(final IOEventHandlerFactory handlerFactory) throws IOException {
87          super.execute(handlerFactory);
88      }
89  
90      public void start(final HttpProcessor httpProcessor, final H2Config h2Config) throws IOException {
91          start(new InternalClientH2EventHandlerFactory(
92                  httpProcessor,
93                  new DefaultAsyncPushConsumerFactory(registry),
94                  HttpVersionPolicy.FORCE_HTTP_2,
95                  h2Config,
96                  Http1Config.DEFAULT,
97                  CharCodingConfig.DEFAULT,
98                  sslContext,
99                  sslSessionInitializer,
100                 sslSessionVerifier));
101     }
102 
103     public void start(final HttpProcessor httpProcessor, final Http1Config http1Config) throws IOException {
104         start(new InternalClientH2EventHandlerFactory(
105                 httpProcessor,
106                 new DefaultAsyncPushConsumerFactory(registry),
107                 HttpVersionPolicy.FORCE_HTTP_1,
108                 H2Config.DEFAULT,
109                 http1Config,
110                 CharCodingConfig.DEFAULT,
111                 sslContext,
112                 sslSessionInitializer,
113                 sslSessionVerifier));
114     }
115 
116     public void start(final H2Config h2Config) throws IOException {
117         start(H2Processors.client(), h2Config);
118     }
119 
120     public void start(final Http1Config http1Config) throws IOException {
121         start(H2Processors.client(), http1Config);
122     }
123 
124     public void start() throws Exception {
125         start(H2Config.DEFAULT);
126     }
127 
128     public Future<ClientSessionEndpoint> connect(
129             final HttpHost host,
130             final Timeout timeout,
131             final FutureCallback<ClientSessionEndpoint> callback) {
132         final BasicFuture<ClientSessionEndpoint> future = new BasicFuture<>(callback);
133         requestSession(host, timeout, new FutureContribution<IOSession>(future) {
134 
135             @Override
136             public void completed(final IOSession session) {
137                 future.completed(new ClientSessionEndpoint(session));
138             }
139 
140         });
141         return future;
142     }
143 
144     public Future<ClientSessionEndpoint> connect(final HttpHost host,final Timeout timeout) {
145         return connect(host, timeout, null);
146     }
147 
148     public Future<ClientSessionEndpoint> connect(final String hostname, final int port, final Timeout timeout) {
149         return connect(new HttpHost(hostname, port), timeout, null);
150     }
151 
152 }