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.client5.testing.extension.async;
29  
30  import java.net.InetSocketAddress;
31  import java.util.Set;
32  import java.util.concurrent.Future;
33  
34  import org.apache.hc.core5.function.Decorator;
35  import org.apache.hc.core5.http.config.Http1Config;
36  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
37  import org.apache.hc.core5.http.protocol.HttpProcessor;
38  import org.apache.hc.core5.http2.config.H2Config;
39  import org.apache.hc.core5.reactor.IOReactorStatus;
40  import org.apache.hc.core5.reactor.ListenerEndpoint;
41  import org.apache.hc.core5.testing.nio.H2TestServer;
42  import org.apache.hc.core5.util.TimeValue;
43  
44  public class TestAsyncServer {
45  
46      private final H2TestServer server;
47      private final H2Config h2Config;
48      private final Http1Config http1Config;
49      private final HttpProcessor httpProcessor;
50      private final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator;
51  
52      TestAsyncServer(
53              final H2TestServer server,
54              final H2Config h2Config,
55              final Http1Config http1Config,
56              final HttpProcessor httpProcessor,
57              final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator) {
58          this.server = server;
59          this.h2Config = h2Config;
60          this.http1Config = http1Config;
61          this.httpProcessor = httpProcessor;
62          this.exchangeHandlerDecorator = exchangeHandlerDecorator;
63      }
64  
65      public Future<ListenerEndpoint> listen(final InetSocketAddress address) {
66          return server.listen(address);
67      }
68  
69      public Set<ListenerEndpoint> getEndpoints() {
70          return server.getEndpoints();
71      }
72  
73      public IOReactorStatus getStatus() {
74          return server.getStatus();
75      }
76  
77      public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
78          server.awaitShutdown(waitTime);
79      }
80  
81      public void initiateShutdown() {
82          server.initiateShutdown();
83      }
84  
85      public void shutdown(final TimeValue graceTime) {
86          server.shutdown(graceTime);
87      }
88  
89      public void close() throws Exception {
90          server.close();
91      }
92  
93      public InetSocketAddress start() throws Exception {
94          if (http1Config == null) {
95              return server.start(httpProcessor, exchangeHandlerDecorator, h2Config);
96          } else {
97              return server.start(httpProcessor, exchangeHandlerDecorator, http1Config);
98          }
99      }
100 
101 }