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.ArrayList;
33  import java.util.List;
34  import java.util.concurrent.Future;
35  
36  import javax.net.ssl.SSLContext;
37  
38  import org.apache.hc.core5.function.Decorator;
39  import org.apache.hc.core5.function.Supplier;
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.impl.DefaultConnectionReuseStrategy;
43  import org.apache.hc.core5.http.impl.HttpProcessors;
44  import org.apache.hc.core5.http.impl.routing.RequestRouter;
45  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
46  import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
47  import org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator;
48  import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
49  import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
50  import org.apache.hc.core5.http.protocol.HttpProcessor;
51  import org.apache.hc.core5.http.protocol.UriPatternType;
52  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
53  import org.apache.hc.core5.reactor.IOReactorConfig;
54  import org.apache.hc.core5.reactor.IOReactorStatus;
55  import org.apache.hc.core5.reactor.ListenerEndpoint;
56  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
57  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
58  import org.apache.hc.core5.util.Asserts;
59  
60  public class Http1TestServer extends AsyncServer {
61  
62      private final List<RequestRouter.Entry<Supplier<AsyncServerExchangeHandler>>> routeEntries;
63      private final SSLContext sslContext;
64      private final SSLSessionInitializer sslSessionInitializer;
65      private final SSLSessionVerifier sslSessionVerifier;
66  
67      public Http1TestServer(
68              final IOReactorConfig ioReactorConfig,
69              final SSLContext sslContext,
70              final SSLSessionInitializer sslSessionInitializer,
71              final SSLSessionVerifier sslSessionVerifier) throws IOException {
72          super(ioReactorConfig);
73          this.routeEntries = new ArrayList<>();
74          this.sslContext = sslContext;
75          this.sslSessionInitializer = sslSessionInitializer;
76          this.sslSessionVerifier = sslSessionVerifier;
77      }
78  
79      public Http1TestServer() throws IOException {
80          this(IOReactorConfig.DEFAULT, null, null, null);
81      }
82  
83      public void register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
84          Asserts.check(getStatus() == IOReactorStatus.INACTIVE, "Server has already been started");
85          routeEntries.add(new RequestRouter.Entry<>(uriPattern, supplier));
86      }
87  
88      public <T> void register(
89              final String uriPattern,
90              final AsyncServerRequestHandler<T> requestHandler) {
91          register(uriPattern, () -> new BasicServerExchangeHandler<>(requestHandler));
92      }
93  
94      public InetSocketAddress start(final IOEventHandlerFactory handlerFactory) throws Exception {
95          execute(handlerFactory);
96          final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
97          final ListenerEndpoint listener = future.get();
98          return (InetSocketAddress) listener.getAddress();
99      }
100 
101     public InetSocketAddress start(
102             final HttpProcessor httpProcessor,
103             final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
104             final Http1Config http1Config) throws Exception {
105         return start(new InternalServerHttp1EventHandlerFactory(
106                 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
107                 new DefaultAsyncResponseExchangeHandlerFactory(
108                         RequestRouter.create(RequestRouter.LOCAL_AUTHORITY, UriPatternType.URI_PATTERN, routeEntries, RequestRouter.LOCAL_AUTHORITY_RESOLVER, null),
109                         exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new),
110                 http1Config,
111                 CharCodingConfig.DEFAULT,
112                 DefaultConnectionReuseStrategy.INSTANCE,
113                 sslContext,
114                 sslSessionInitializer,
115                 sslSessionVerifier));
116     }
117 
118     public InetSocketAddress start(final HttpProcessor httpProcessor, final Http1Config http1Config) throws Exception {
119         return start(httpProcessor, null, http1Config);
120     }
121 
122     public InetSocketAddress start() throws Exception {
123         return start(null, null, Http1Config.DEFAULT);
124     }
125 
126 }