/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.hc.core5.http.impl.io.bootstrap; import java.net.InetAddress; import java.util.HashMap; import java.util.Map; import javax.net.ServerSocketFactory; import javax.net.ssl.SSLContext; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.ConnectionReuseStrategy; import org.apache.hc.core5.http.ExceptionListener; import org.apache.hc.core5.http.HttpResponseFactory; import org.apache.hc.core5.http.config.ConnectionConfig; import org.apache.hc.core5.http.config.SocketConfig; import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy; import org.apache.hc.core5.http.impl.Http1StreamListener; import org.apache.hc.core5.http.impl.HttpProcessors; import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnection; import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnectionFactory; import org.apache.hc.core5.http.impl.io.DefaultClassicHttpResponseFactory; import org.apache.hc.core5.http.impl.io.HttpService; import org.apache.hc.core5.http.io.HttpConnectionFactory; import org.apache.hc.core5.http.io.HttpExpectationVerifier; import org.apache.hc.core5.http.io.HttpRequestHandler; import org.apache.hc.core5.http.io.HttpRequestHandlerMapper; import org.apache.hc.core5.http.io.UriHttpRequestHandlerMapper; import org.apache.hc.core5.http.protocol.HttpProcessor; /** * @since 4.4 */ public class ServerBootstrap { private int listenerPort; private InetAddress localAddress; private SocketConfig socketConfig; private ConnectionConfig connectionConfig; private HttpProcessor httpProcessor; private ConnectionReuseStrategy connStrategy; private HttpResponseFactory responseFactory; private HttpRequestHandlerMapper handlerMapper; private Map handlerMap; private HttpExpectationVerifier expectationVerifier; private ServerSocketFactory serverSocketFactory; private SSLContext sslContext; private SSLServerSetupHandler sslSetupHandler; private HttpConnectionFactory connectionFactory; private ExceptionListener exceptionListener; private Http1StreamListener streamListener; private ServerBootstrap() { } public static ServerBootstrap bootstrap() { return new ServerBootstrap(); } /** * Sets listener port number. */ public final ServerBootstrap setListenerPort(final int listenerPort) { this.listenerPort = listenerPort; return this; } /** * Assigns local interface for the listener. */ public final ServerBootstrap setLocalAddress(final InetAddress localAddress) { this.localAddress = localAddress; return this; } /** * Sets socket configuration. */ public final ServerBootstrap setSocketConfig(final SocketConfig socketConfig) { this.socketConfig = socketConfig; return this; } /** * Sets connection configuration. *

* Please note this value can be overridden by the {@link #setConnectionFactory( *HttpConnectionFactory)} method. */ public final ServerBootstrap setConnectionConfig(final ConnectionConfig connectionConfig) { this.connectionConfig = connectionConfig; return this; } /** * Assigns {@link HttpProcessor} instance. */ public final ServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) { this.httpProcessor = httpProcessor; return this; } /** * Assigns {@link ConnectionReuseStrategy} instance. */ public final ServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) { this.connStrategy = connStrategy; return this; } /** * Assigns {@link HttpResponseFactory} instance. */ public final ServerBootstrap setResponseFactory(final HttpResponseFactory responseFactory) { this.responseFactory = responseFactory; return this; } /** * Assigns {@link HttpRequestHandlerMapper} instance. */ public final ServerBootstrap setHandlerMapper(final HttpRequestHandlerMapper handlerMapper) { this.handlerMapper = handlerMapper; return this; } /** * Registers the given {@link HttpRequestHandler} as a handler for URIs * matching the given pattern. *

* Please note this value can be overridden by the {@link #setHandlerMapper( *HttpRequestHandlerMapper)} method. * * @param pattern the pattern to register the handler for. * @param handler the handler. */ public final ServerBootstrap registerHandler(final String pattern, final HttpRequestHandler handler) { if (pattern == null || handler == null) { return this; } if (handlerMap == null) { handlerMap = new HashMap<>(); } handlerMap.put(pattern, handler); return this; } /** * Assigns {@link HttpExpectationVerifier} instance. */ public final ServerBootstrap setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) { this.expectationVerifier = expectationVerifier; return this; } /** * Assigns {@link HttpConnectionFactory} instance. */ public final ServerBootstrap setConnectionFactory( final HttpConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; return this; } /** * Assigns {@link SSLServerSetupHandler} instance. */ public final ServerBootstrap setSslSetupHandler(final SSLServerSetupHandler sslSetupHandler) { this.sslSetupHandler = sslSetupHandler; return this; } /** * Assigns {@link javax.net.ServerSocketFactory} instance. */ public final ServerBootstrap setServerSocketFactory(final ServerSocketFactory serverSocketFactory) { this.serverSocketFactory = serverSocketFactory; return this; } /** * Assigns {@link javax.net.ssl.SSLContext} instance. *

* Please note this value can be overridden by the {@link #setServerSocketFactory( * javax.net.ServerSocketFactory)} method. */ public final ServerBootstrap setSslContext(final SSLContext sslContext) { this.sslContext = sslContext; return this; } /** * Assigns {@link ExceptionListener} instance. */ public final ServerBootstrap setExceptionListener(final ExceptionListener exceptionListener) { this.exceptionListener = exceptionListener; return this; } /** * Assigns {@link ExceptionListener} instance. */ public final ServerBootstrap setStreamListener(final Http1StreamListener streamListener) { this.streamListener = streamListener; return this; } public HttpServer create() { HttpProcessor httpProcessorCopy = this.httpProcessor; if (httpProcessorCopy == null) { httpProcessorCopy = HttpProcessors.server(); } HttpRequestHandlerMapper handlerMapperCopy = this.handlerMapper; if (handlerMapperCopy == null) { final UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); if (handlerMap != null) { for (final Map.Entry entry: handlerMap.entrySet()) { reqistry.register(entry.getKey(), entry.getValue()); } } handlerMapperCopy = reqistry; } ConnectionReuseStrategy connStrategyCopy = this.connStrategy; if (connStrategyCopy == null) { connStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE; } HttpResponseFactory responseFactoryCopy = this.responseFactory; if (responseFactoryCopy == null) { responseFactoryCopy = DefaultClassicHttpResponseFactory.INSTANCE; } final HttpService httpService = new HttpService( httpProcessorCopy, connStrategyCopy, responseFactoryCopy, handlerMapperCopy, this.expectationVerifier, this.streamListener); ServerSocketFactory serverSocketFactoryCopy = this.serverSocketFactory; if (serverSocketFactoryCopy == null) { if (this.sslContext != null) { serverSocketFactoryCopy = this.sslContext.getServerSocketFactory(); } else { serverSocketFactoryCopy = ServerSocketFactory.getDefault(); } } HttpConnectionFactory connectionFactoryCopy = this.connectionFactory; if (connectionFactoryCopy == null) { if (this.connectionConfig != null) { connectionFactoryCopy = new DefaultBHttpServerConnectionFactory(this.connectionConfig); } else { connectionFactoryCopy = DefaultBHttpServerConnectionFactory.INSTANCE; } } ExceptionListener exceptionListenerCopy = this.exceptionListener; if (exceptionListenerCopy == null) { exceptionListenerCopy = ExceptionListener.NO_OP; } return new HttpServer( this.listenerPort > 0 ? this.listenerPort : 0, this.localAddress, this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT, serverSocketFactoryCopy, httpService, connectionFactoryCopy, this.sslSetupHandler, exceptionListenerCopy); } }