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