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.net.InetSocketAddress;
32  import java.util.concurrent.Future;
33  
34  import javax.net.ssl.SSLContext;
35  
36  import org.apache.hc.core5.function.Decorator;
37  import org.apache.hc.core5.function.Supplier;
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.HttpProcessors;
41  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
42  import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
43  import org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator;
44  import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
45  import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
46  import org.apache.hc.core5.http.protocol.HttpProcessor;
47  import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
48  import org.apache.hc.core5.http2.HttpVersionPolicy;
49  import org.apache.hc.core5.http2.config.H2Config;
50  import org.apache.hc.core5.http2.impl.H2Processors;
51  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
52  import org.apache.hc.core5.reactor.IOReactorConfig;
53  import org.apache.hc.core5.reactor.ListenerEndpoint;
54  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
55  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
56  
57  public class H2TestServer extends AsyncServer {
58  
59      private final SSLContext sslContext;
60      private final SSLSessionInitializer sslSessionInitializer;
61      private final SSLSessionVerifier sslSessionVerifier;
62      private final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry;
63  
64      public H2TestServer(
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 H2TestServer() throws IOException {
77          this(IOReactorConfig.DEFAULT, null, null, null);
78      }
79  
80      public void register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
81          registry.register(null, uriPattern, supplier);
82      }
83  
84      public <T> void register(
85              final String uriPattern,
86              final AsyncServerRequestHandler<T> requestHandler) {
87          register(uriPattern, new Supplier<AsyncServerExchangeHandler>() {
88  
89              @Override
90              public AsyncServerExchangeHandler get() {
91                  return new BasicServerExchangeHandler<>(requestHandler);
92              }
93  
94          });
95      }
96  
97      public void start(final IOEventHandlerFactory handlerFactory) throws IOException {
98          execute(handlerFactory);
99      }
100 
101     public InetSocketAddress start(
102             final HttpProcessor httpProcessor,
103             final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
104             final H2Config h2Config) throws Exception {
105         start(new InternalServerH2EventHandlerFactory(
106                 httpProcessor != null ? httpProcessor : H2Processors.server(),
107                 new DefaultAsyncResponseExchangeHandlerFactory(
108                         registry,
109                         exchangeHandlerDecorator != null ? exchangeHandlerDecorator : new Decorator<AsyncServerExchangeHandler>() {
110 
111                             @Override
112                             public AsyncServerExchangeHandlerangeHandler.html#AsyncServerExchangeHandler">AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler handler) {
113                                 return new BasicAsyncServerExpectationDecorator(handler);
114                             }
115 
116                         }),
117                 HttpVersionPolicy.FORCE_HTTP_2,
118                 h2Config,
119                 Http1Config.DEFAULT,
120                 CharCodingConfig.DEFAULT,
121                 sslContext,
122                 sslSessionInitializer,
123                 sslSessionVerifier));
124         final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
125         final ListenerEndpoint listener = future.get();
126         return (InetSocketAddress) listener.getAddress();
127     }
128 
129     public InetSocketAddress start(
130             final HttpProcessor httpProcessor,
131             final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
132             final Http1Config http1Config) throws Exception {
133         start(new InternalServerH2EventHandlerFactory(
134                 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
135                 new DefaultAsyncResponseExchangeHandlerFactory(
136                         registry,
137                         exchangeHandlerDecorator != null ? exchangeHandlerDecorator : new Decorator<AsyncServerExchangeHandler>() {
138 
139                     @Override
140                     public AsyncServerExchangeHandlerangeHandler.html#AsyncServerExchangeHandler">AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler handler) {
141                         return new BasicAsyncServerExpectationDecorator(handler);
142                     }
143 
144                 }),
145                 HttpVersionPolicy.FORCE_HTTP_1,
146                 H2Config.DEFAULT,
147                 http1Config,
148                 CharCodingConfig.DEFAULT,
149                 sslContext,
150                 sslSessionInitializer,
151                 sslSessionVerifier));
152         final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
153         final ListenerEndpoint listener = future.get();
154         return (InetSocketAddress) listener.getAddress();
155     }
156 
157     public InetSocketAddress start(final H2Config h2Config) throws Exception {
158         return start(null, null, h2Config);
159     }
160 
161     public InetSocketAddress start(final Http1Config http1Config) throws Exception {
162         return start(null, null, http1Config);
163     }
164 
165     public InetSocketAddress start() throws Exception {
166         return start(H2Config.DEFAULT);
167     }
168 
169 }