/*
* ====================================================================
* 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.nio.bootstrap;
import java.util.ArrayList;
import java.util.List;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ConnectionReuseStrategy;
import org.apache.hc.core5.http.ExceptionListener;
import org.apache.hc.core5.http.config.ConnectionConfig;
import org.apache.hc.core5.http.impl.ConnectionListener;
import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
import org.apache.hc.core5.http.impl.Http1StreamListener;
import org.apache.hc.core5.http.impl.HttpProcessors;
import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory;
import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory;
import org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory;
import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
import org.apache.hc.core5.http.nio.ssl.BasicServerTlsStrategy;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
import org.apache.hc.core5.http.nio.support.RequestConsumerSupplier;
import org.apache.hc.core5.http.nio.support.ResponseHandler;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.net.InetAddressUtils;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.util.Args;
/**
* @since 4.4
*/
public class ServerBootstrap {
private final List handlerList;
private String canonicalHostName;
private IOReactorConfig ioReactorConfig;
private ConnectionConfig connectionConfig;
private HttpProcessor httpProcessor;
private ConnectionReuseStrategy connStrategy;
private TlsStrategy tlsStrategy;
private ExceptionListener exceptionListener;
private ConnectionListener connectionListener;
private Http1StreamListener streamListener;
private ServerBootstrap() {
this.handlerList = new ArrayList<>();
}
public static ServerBootstrap bootstrap() {
return new ServerBootstrap();
}
/**
* Sets canonical name (fully qualified domain name) of the server.
*
* @since 5.0
*/
public final ServerBootstrap setCanonicalHostName(final String canonicalHostName) {
this.canonicalHostName = canonicalHostName;
return this;
}
/**
* Sets I/O reactor configuration.
*/
public final ServerBootstrap setIOReactorConfig(final IOReactorConfig ioReactorConfig) {
this.ioReactorConfig = ioReactorConfig;
return this;
}
/**
* Sets connection configuration.
*/
public final ServerBootstrap setConnectionConfig(final ConnectionConfig connectionConfig) {
this.connectionConfig = connectionConfig;
return this;
}
/**
* Assigns {@link org.apache.hc.core5.http.protocol.HttpProcessor} instance.
*/
public final ServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
this.httpProcessor = httpProcessor;
return this;
}
/**
* Assigns {@link org.apache.hc.core5.http.ConnectionReuseStrategy} instance.
*/
public final ServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
this.connStrategy = connStrategy;
return this;
}
/**
* Assigns {@link TlsStrategy} instance.
*/
public final ServerBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
this.tlsStrategy = tlsStrategy;
return this;
}
/**
* Assigns {@link ExceptionListener} instance.
*/
public final ServerBootstrap setExceptionListener(final ExceptionListener exceptionListener) {
this.exceptionListener = exceptionListener;
return this;
}
/**
* Assigns {@link ConnectionListener} instance.
*/
public final ServerBootstrap setConnectionListener(final ConnectionListener connectionListener) {
this.connectionListener = connectionListener;
return this;
}
/**
* Assigns {@link Http1StreamListener} instance.
*
* @since 5.0
*/
public final ServerBootstrap setStreamListener(final Http1StreamListener streamListener) {
this.streamListener = streamListener;
return this;
}
public final ServerBootstrap register(final String uriPattern, final Supplier supplier) {
Args.notBlank(uriPattern, "URI pattern");
Args.notNull(supplier, "Supplier");
handlerList.add(new HandlerEntry(null, uriPattern, supplier));
return this;
}
public final ServerBootstrap registerVirtual(final String hostname, final String uriPattern, final Supplier supplier) {
Args.notBlank(hostname, "Hostname");
Args.notBlank(uriPattern, "URI pattern");
Args.notNull(supplier, "Supplier");
handlerList.add(new HandlerEntry(hostname, uriPattern, supplier));
return this;
}
public final ServerBootstrap register(
final String uriPattern,
final RequestConsumerSupplier consumerSupplier,
final ResponseHandler responseHandler) {
register(uriPattern, new Supplier() {
@Override
public AsyncServerExchangeHandler get() {
return new BasicServerExchangeHandler<>(consumerSupplier, responseHandler);
}
});
return this;
}
public final ServerBootstrap registerVirtual(
final String hostname,
final String uriPattern,
final RequestConsumerSupplier consumerSupplier,
final ResponseHandler responseHandler) {
registerVirtual(hostname, uriPattern, new Supplier() {
@Override
public AsyncServerExchangeHandler get() {
return new BasicServerExchangeHandler<>(consumerSupplier, responseHandler);
}
});
return this;
}
public HttpAsyncServer create() {
final AsyncServerExchangeHandlerRegistry exchangeHandlerFactory = new AsyncServerExchangeHandlerRegistry(
canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName());
for (HandlerEntry entry: handlerList) {
exchangeHandlerFactory.register(entry.hostname, entry.uriPattern, entry.supplier);
}
final ServerHttp1IOEventHandlerFactory ioEventHandlerFactory = new ServerHttp1IOEventHandlerFactory(
httpProcessor != null ? httpProcessor : HttpProcessors.server(),
exchangeHandlerFactory,
connectionConfig,
connStrategy != null ? connStrategy : DefaultConnectionReuseStrategy.INSTANCE,
DefaultHttpRequestParserFactory.INSTANCE,
DefaultHttpResponseWriterFactory.INSTANCE,
DefaultContentLengthStrategy.INSTANCE,
DefaultContentLengthStrategy.INSTANCE,
tlsStrategy != null ? tlsStrategy : new BasicServerTlsStrategy(new int[] {443, 8443}),
connectionListener,
streamListener);
return new HttpAsyncServer(
ioEventHandlerFactory,
ioReactorConfig,
exceptionListener);
}
private static class HandlerEntry {
final String hostname;
final String uriPattern;
final Supplier supplier;
public HandlerEntry(final String hostname, final String uriPattern, final Supplier supplier) {
this.hostname = hostname;
this.uriPattern = uriPattern;
this.supplier = supplier;
}
}
}