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.sync;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.hc.client5.testing.SSLTestContexts;
34  import org.apache.hc.core5.function.Decorator;
35  import org.apache.hc.core5.http.URIScheme;
36  import org.apache.hc.core5.http.config.Http1Config;
37  import org.apache.hc.core5.http.io.HttpRequestHandler;
38  import org.apache.hc.core5.http.io.HttpServerRequestHandler;
39  import org.apache.hc.core5.http.io.SocketConfig;
40  import org.apache.hc.core5.http.protocol.HttpProcessor;
41  import org.apache.hc.core5.testing.classic.ClassicTestServer;
42  import org.apache.hc.core5.util.Args;
43  import org.apache.hc.core5.util.Timeout;
44  
45  public class TestServerBootstrap {
46  
47      static final class HandlerEntry<T> {
48  
49          final String hostname;
50          final String uriPattern;
51          final T handler;
52  
53          public HandlerEntry(final String hostname, final String uriPattern, final T handler) {
54              this.hostname = hostname;
55              this.uriPattern = uriPattern;
56              this.handler = handler;
57          }
58  
59          public HandlerEntry(final String uriPattern, final T handler) {
60              this(null, uriPattern, handler);
61          }
62  
63      }
64  
65      private final URIScheme scheme;
66  
67      private final List<HandlerEntry<HttpRequestHandler>> handlerList;
68      private Timeout timeout;
69      private HttpProcessor httpProcessor;
70      private Decorator<HttpServerRequestHandler> exchangeHandlerDecorator;
71  
72      public TestServerBootstrap(final URIScheme scheme) {
73          this.scheme = scheme != null ? scheme : URIScheme.HTTP;
74          this.handlerList = new ArrayList<>();
75      }
76  
77      public TestServerBootstrap register(final String uriPattern, final HttpRequestHandler requestHandler) {
78          return register(null, uriPattern, requestHandler);
79      }
80  
81      public TestServerBootstrap register(final String hostname, final String uriPattern, final HttpRequestHandler requestHandler) {
82          Args.notNull(uriPattern, "URI pattern");
83          Args.notNull(requestHandler, "Exchange handler");
84          handlerList.add(new HandlerEntry<>(hostname, uriPattern, requestHandler));
85          return this;
86      }
87  
88      public TestServerBootstrap setTimeout(final Timeout timeout) {
89          this.timeout = timeout;
90          return this;
91      }
92  
93      public TestServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
94          this.httpProcessor = httpProcessor;
95          return this;
96      }
97  
98      public TestServerBootstrap setExchangeHandlerDecorator(final Decorator<HttpServerRequestHandler> exchangeHandlerDecorator) {
99          this.exchangeHandlerDecorator = exchangeHandlerDecorator;
100         return this;
101     }
102 
103     public TestServer build() throws Exception {
104         final ClassicTestServer server = new ClassicTestServer(
105                 scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
106                 SocketConfig.custom()
107                         .setSoTimeout(timeout)
108                         .build());
109         for (final HandlerEntry<HttpRequestHandler> entry: handlerList) {
110             if (entry.hostname != null) {
111                 server.register(entry.hostname, entry.uriPattern, entry.handler);
112             } else {
113                 server.register(entry.uriPattern, entry.handler);
114             }
115         }
116         return new TestServer(
117                 server,
118                 Http1Config.DEFAULT,
119                 httpProcessor,
120                 exchangeHandlerDecorator);
121     }
122 
123 }