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  package org.apache.hc.core5.http.impl.bootstrap;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.hc.core5.function.Decorator;
33  import org.apache.hc.core5.function.Supplier;
34  import org.apache.hc.core5.http.ConnectionReuseStrategy;
35  import org.apache.hc.core5.http.config.CharCodingConfig;
36  import org.apache.hc.core5.http.config.H1Config;
37  import org.apache.hc.core5.http.config.NamedElementChain;
38  import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
39  import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
40  import org.apache.hc.core5.http.impl.Http1StreamListener;
41  import org.apache.hc.core5.http.impl.HttpProcessors;
42  import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory;
43  import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory;
44  import org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory;
45  import org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory;
46  import org.apache.hc.core5.http.nio.AsyncFilterHandler;
47  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
48  import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
49  import org.apache.hc.core5.http.nio.HandlerFactory;
50  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
51  import org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter;
52  import org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement;
53  import org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory;
54  import org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator;
55  import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
56  import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
57  import org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter;
58  import org.apache.hc.core5.http.protocol.HttpProcessor;
59  import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
60  import org.apache.hc.core5.http.protocol.UriPatternType;
61  import org.apache.hc.core5.net.InetAddressUtils;
62  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
63  import org.apache.hc.core5.reactor.IOReactorConfig;
64  import org.apache.hc.core5.reactor.IOSession;
65  import org.apache.hc.core5.reactor.IOSessionListener;
66  import org.apache.hc.core5.util.Args;
67  
68  /**
69   * @since 5.0
70   */
71  public class AsyncServerBootstrap {
72  
73      private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
74      private final List<FilterEntry<AsyncFilterHandler>> filters;
75      private String canonicalHostName;
76      private UriPatternType uriPatternType;
77      private IOReactorConfig ioReactorConfig;
78      private H1Config h1Config;
79      private CharCodingConfig charCodingConfig;
80      private HttpProcessor httpProcessor;
81      private ConnectionReuseStrategy connStrategy;
82      private TlsStrategy tlsStrategy;
83      private Decorator<IOSession> ioSessionDecorator;
84      private IOSessionListener sessionListener;
85      private Http1StreamListener streamListener;
86  
87      private AsyncServerBootstrap() {
88          this.handlerList = new ArrayList<>();
89          this.filters = new ArrayList<>();
90      }
91  
92      public static AsyncServerBootstrap bootstrap() {
93          return new AsyncServerBootstrap();
94      }
95  
96      /**
97       * Sets canonical name (fully qualified domain name) of the server.
98       */
99      public final AsyncServerBootstrap setCanonicalHostName(final String canonicalHostName) {
100         this.canonicalHostName = canonicalHostName;
101         return this;
102     }
103 
104     /**
105      * Sets I/O reactor configuration.
106      */
107     public final AsyncServerBootstrap setIOReactorConfig(final IOReactorConfig ioReactorConfig) {
108         this.ioReactorConfig = ioReactorConfig;
109         return this;
110     }
111 
112     /**
113      * Sets HTTP/1.1 protocol parameters.
114      */
115     public final AsyncServerBootstrap setH1Config(final H1Config h1Config) {
116         this.h1Config = h1Config;
117         return this;
118     }
119 
120     /**
121      * Sets connection configuration.
122      */
123     public final AsyncServerBootstrap setCharCodingConfig(final CharCodingConfig charCodingConfig) {
124         this.charCodingConfig = charCodingConfig;
125         return this;
126     }
127 
128     /**
129      * Assigns {@link org.apache.hc.core5.http.protocol.HttpProcessor} instance.
130      */
131     public final AsyncServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
132         this.httpProcessor = httpProcessor;
133         return this;
134     }
135 
136     /**
137      * Assigns {@link org.apache.hc.core5.http.ConnectionReuseStrategy} instance.
138      */
139     public final AsyncServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
140         this.connStrategy = connStrategy;
141         return this;
142     }
143 
144     /**
145      * Assigns {@link TlsStrategy} instance.
146      */
147     public final AsyncServerBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
148         this.tlsStrategy = tlsStrategy;
149         return this;
150     }
151 
152     /**
153      * Assigns {@link IOSession} {@link Decorator} instance.
154      */
155     public final AsyncServerBootstrap setIOSessionDecorator(final Decorator<IOSession> ioSessionDecorator) {
156         this.ioSessionDecorator = ioSessionDecorator;
157         return this;
158     }
159 
160     /**
161      * Assigns {@link IOSessionListener} instance.
162      */
163     public final AsyncServerBootstrap setIOSessionListener(final IOSessionListener sessionListener) {
164         this.sessionListener = sessionListener;
165         return this;
166     }
167 
168     /**
169      * Assigns {@link Http1StreamListener} instance.
170      *
171      * @since 5.0
172      */
173     public final AsyncServerBootstrap setStreamListener(final Http1StreamListener streamListener) {
174         this.streamListener = streamListener;
175         return this;
176     }
177 
178     /**
179      * Assigns {@link UriPatternType} for handler registration.
180      */
181     public final AsyncServerBootstrap setUriPatternType(final UriPatternType uriPatternType) {
182         this.uriPatternType = uriPatternType;
183         return this;
184     }
185 
186     /**
187      * Registers the given {@link AsyncServerExchangeHandler} {@link Supplier} as a default handler for URIs
188      * matching the given pattern.
189      *
190      * @param uriPattern the pattern to register the handler for.
191      * @param supplier the handler supplier.
192      */
193     public final AsyncServerBootstrap register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
194         Args.notBlank(uriPattern, "URI pattern");
195         Args.notNull(supplier, "Supplier");
196         handlerList.add(new HandlerEntry<>(null, uriPattern, supplier));
197         return this;
198     }
199 
200     /**
201      * Registers the given {@link AsyncServerExchangeHandler} {@link Supplier} as a handler for URIs
202      * matching the given host and the pattern.
203      *
204      * @param hostname the host name
205      * @param uriPattern the pattern to register the handler for.
206      * @param supplier the handler supplier.
207      */
208     public final AsyncServerBootstrap registerVirtual(final String hostname, final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
209         Args.notBlank(hostname, "Hostname");
210         Args.notBlank(uriPattern, "URI pattern");
211         Args.notNull(supplier, "Supplier");
212         handlerList.add(new HandlerEntry<>(hostname, uriPattern, supplier));
213         return this;
214     }
215 
216     /**
217      * Registers the given {@link AsyncServerRequestHandler} as a default handler for URIs
218      * matching the given pattern.
219      *
220      * @param uriPattern the pattern to register the handler for.
221      * @param requestHandler the handler.
222      */
223     public final <T> AsyncServerBootstrap register(
224             final String uriPattern,
225             final AsyncServerRequestHandler<T> requestHandler) {
226         register(uriPattern, new Supplier<AsyncServerExchangeHandler>() {
227 
228             @Override
229             public AsyncServerExchangeHandler get() {
230                 return new BasicServerExchangeHandler<>(requestHandler);
231             }
232 
233         });
234         return this;
235     }
236 
237     /**
238      * Registers the given {@link AsyncServerRequestHandler} as a handler for URIs
239      * matching the given host and the pattern.
240      *
241      * @param hostname the host name
242      * @param uriPattern the pattern to register the handler for.
243      * @param requestHandler the handler.
244      */
245     public final <T> AsyncServerBootstrap registerVirtual(
246             final String hostname,
247             final String uriPattern,
248             final AsyncServerRequestHandler<T> requestHandler) {
249         registerVirtual(hostname, uriPattern, new Supplier<AsyncServerExchangeHandler>() {
250 
251             @Override
252             public AsyncServerExchangeHandler get() {
253                 return new BasicServerExchangeHandler<>(requestHandler);
254             }
255 
256         });
257         return this;
258     }
259 
260     /**
261      * Adds the filter before the filter with the given name.
262      */
263     public final AsyncServerBootstrap addFilterBefore(final String existing, final String name, final AsyncFilterHandler filterHandler) {
264         Args.notBlank(existing, "Existing");
265         Args.notBlank(name, "Name");
266         Args.notNull(filterHandler, "Filter handler");
267         filters.add(new FilterEntry<>(FilterEntry.Postion.BEFORE, name, filterHandler, existing));
268         return this;
269     }
270 
271     /**
272      * Adds the filter after the filter with the given name.
273      */
274     public final AsyncServerBootstrap addFilterAfter(final String existing, final String name, final AsyncFilterHandler filterHandler) {
275         Args.notBlank(existing, "Existing");
276         Args.notBlank(name, "Name");
277         Args.notNull(filterHandler, "Filter handler");
278         filters.add(new FilterEntry<>(FilterEntry.Postion.AFTER, name, filterHandler, existing));
279         return this;
280     }
281 
282     /**
283      * Replace an existing filter with the given name with new filter.
284      */
285     public final AsyncServerBootstrap replaceFilter(final String existing, final AsyncFilterHandler filterHandler) {
286         Args.notBlank(existing, "Existing");
287         Args.notNull(filterHandler, "Filter handler");
288         filters.add(new FilterEntry<>(FilterEntry.Postion.REPLACE, existing, filterHandler, existing));
289         return this;
290     }
291 
292     /**
293      * Add an filter to the head of the processing list.
294      */
295     public final AsyncServerBootstrap addFilterFirst(final String name, final AsyncFilterHandler filterHandler) {
296         Args.notNull(name, "Name");
297         Args.notNull(filterHandler, "Filter handler");
298         filters.add(new FilterEntry<>(FilterEntry.Postion.FIRST, name, filterHandler, null));
299         return this;
300     }
301 
302     /**
303      * Add an filter to the tail of the processing list.
304      */
305     public final AsyncServerBootstrap addFilterLast(final String name, final AsyncFilterHandler filterHandler) {
306         Args.notNull(name, "Name");
307         Args.notNull(filterHandler, "Filter handler");
308         filters.add(new FilterEntry<>(FilterEntry.Postion.LAST, name, filterHandler, null));
309         return this;
310     }
311 
312     public HttpAsyncServer create() {
313         final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
314                 canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
315                 uriPatternType);
316         for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
317             registry.register(entry.hostname, entry.uriPattern, entry.handler);
318         }
319 
320         final HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
321         if (!filters.isEmpty()) {
322             final NamedElementChain<AsyncFilterHandler> filterChainDefinition = new NamedElementChain<>();
323             filterChainDefinition.addLast(
324                     new TerminalAsyncServerFilter(new DefaultAsyncResponseExchangeHandlerFactory(registry)),
325                     StandardFilters.MAIN_HANDLER.name());
326             filterChainDefinition.addFirst(
327                     new AsyncServerExpectationFilter(),
328                     StandardFilters.EXPECT_CONTINUE.name());
329 
330             for (final FilterEntry<AsyncFilterHandler> entry: filters) {
331                 switch (entry.postion) {
332                     case AFTER:
333                         filterChainDefinition.addAfter(entry.existing, entry.filterHandler, entry.name);
334                         break;
335                     case BEFORE:
336                         filterChainDefinition.addBefore(entry.existing, entry.filterHandler, entry.name);
337                         break;
338                     case REPLACE:
339                         filterChainDefinition.replace(entry.existing, entry.filterHandler);
340                         break;
341                     case FIRST:
342                         filterChainDefinition.addFirst(entry.filterHandler, entry.name);
343                         break;
344                     case LAST:
345                         filterChainDefinition.addLast(entry.filterHandler, entry.name);
346                         break;
347                 }
348             }
349 
350             NamedElementChain<AsyncFilterHandler>.Node current = filterChainDefinition.getLast();
351             AsyncServerFilterChainElement execChain = null;
352             while (current != null) {
353                 execChain = new AsyncServerFilterChainElement(current.getValue(), execChain);
354                 current = current.getPrevious();
355             }
356 
357             handlerFactory = new AsyncServerFilterChainExchangeHandlerFactory(execChain);
358         } else {
359             handlerFactory = new DefaultAsyncResponseExchangeHandlerFactory(registry, new Decorator<AsyncServerExchangeHandler>() {
360 
361                 @Override
362                 public AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler handler) {
363                     return new BasicAsyncServerExpectationDecorator(handler);
364                 }
365 
366             });
367         }
368 
369         final ServerHttp1StreamDuplexerFactory streamHandlerFactory = new ServerHttp1StreamDuplexerFactory(
370                 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
371                 handlerFactory,
372                 h1Config != null ? h1Config : H1Config.DEFAULT,
373                 charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT,
374                 connStrategy != null ? connStrategy : DefaultConnectionReuseStrategy.INSTANCE,
375                 DefaultHttpRequestParserFactory.INSTANCE,
376                 DefaultHttpResponseWriterFactory.INSTANCE,
377                 DefaultContentLengthStrategy.INSTANCE,
378                 DefaultContentLengthStrategy.INSTANCE,
379                 streamListener);
380         final IOEventHandlerFactory ioEventHandlerFactory = new ServerHttp1IOEventHandlerFactory(
381                 streamHandlerFactory,
382                 tlsStrategy);
383         return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, sessionListener);
384     }
385 
386 }