package org.glassfish.grizzly.http.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.ConnectionProbe;
import org.glassfish.grizzly.EmptyCompletionHandler;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.GrizzlyFuture;
import org.glassfish.grizzly.PortRange;
import org.glassfish.grizzly.Processor;
import org.glassfish.grizzly.Transport;
import org.glassfish.grizzly.TransportProbe;
import org.glassfish.grizzly.attributes.AttributeBuilder;
import org.glassfish.grizzly.filterchain.FilterChain;
import org.glassfish.grizzly.filterchain.FilterChainBuilder;
import org.glassfish.grizzly.filterchain.FilterChainEvent;
import org.glassfish.grizzly.filterchain.TransportFilter;
import org.glassfish.grizzly.http.CompressionConfig;
import org.glassfish.grizzly.http.CompressionConfig.CompressionMode;
import org.glassfish.grizzly.http.ContentEncoding;
import org.glassfish.grizzly.http.GZipContentEncoding;
import org.glassfish.grizzly.http.LZMAContentEncoding;
import org.glassfish.grizzly.http.server.filecache.FileCache;
import org.glassfish.grizzly.http.server.http2.PushEvent;
import org.glassfish.grizzly.http.server.jmxbase.JmxEventListener;
import org.glassfish.grizzly.impl.FutureImpl;
import org.glassfish.grizzly.memory.MemoryProbe;
import org.glassfish.grizzly.jmxbase.GrizzlyJmxManager;
import org.glassfish.grizzly.monitoring.MonitoringConfig;
import org.glassfish.grizzly.monitoring.MonitoringUtils;
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
import org.glassfish.grizzly.ssl.SSLBaseFilter;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.grizzly.threadpool.DefaultWorkerThread;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
import org.glassfish.grizzly.threadpool.ThreadPoolProbe;
import org.glassfish.grizzly.utils.DelayedExecutor;
import org.glassfish.grizzly.utils.Futures;
import org.glassfish.grizzly.utils.IdleTimeoutFilter;
public class HttpServer {
private static final Logger LOGGER = Grizzly.logger(HttpServer.class);
private final ServerConfiguration serverConfig = new ServerConfiguration(this);
private State state = State.STOPPED;
private FutureImpl<HttpServer> shutdownFuture;
private final HttpHandlerChain httpHandlerChain = new HttpHandlerChain(this);
private final Map<String, NetworkListener> listeners =
new HashMap<>(2);
private volatile ExecutorService auxExecutorService;
volatile DelayedExecutor delayedExecutor;
protected volatile GrizzlyJmxManager jmxManager;
protected volatile Object managementObject;
public final ServerConfiguration getServerConfiguration() {
return serverConfig;
}
public synchronized void addListener(final NetworkListener listener) {
if (state == State.RUNNING) {
configureListener(listener);
if (!listener.isStarted()) {
try {
listener.start();
} catch (IOException ioe) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE,
"Failed to start listener [{0}] : {1}",
new Object[] { listener.toString(), ioe.toString() });
LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
}
}
}
}
listeners.put(listener.getName(), listener);
}
public synchronized NetworkListener getListener(final String name) {
return listeners.get(name);
}
public synchronized Collection<NetworkListener> getListeners() {
return Collections.unmodifiableCollection(listeners.values());
}
@SuppressWarnings("UnusedReturnValue")
public synchronized NetworkListener removeListener(final String name) {
final NetworkListener listener = listeners.remove(name);
if (listener != null) {
if (listener.isStarted()) {
try {
listener.shutdownNow();
} catch (IOException ioe) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE,
"Failed to shutdown listener [{0}] : {1}",
new Object[] { listener.toString(), ioe.toString() });
LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
}
}
}
}
return listener;
}
public synchronized void start() throws IOException{
if (state == State.RUNNING) {
return;
} else if (state == State.STOPPING) {
throw new IllegalStateException("The server is currently in pending"
+ " shutdown state. Wait for the shutdown to"
+ " complete or force it by calling shutdownNow()");
}
state = State.RUNNING;
shutdownFuture = null;
configureAuxThreadPool();
delayedExecutor = new DelayedExecutor(auxExecutorService);
delayedExecutor.start();
for (final NetworkListener listener : listeners.values()) {
configureListener(listener);
}
if (serverConfig.isJmxEnabled()) {
enableJMX();
}
for (final NetworkListener listener : listeners.values()) {
try {
listener.start();
} catch (IOException ioe) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"Failed to start listener [{0}] : {1}",
new Object[]{listener.toString(), ioe.toString()});
LOGGER.log(Level.FINEST, ioe.toString(), ioe);
}
throw ioe;
}
}
setupHttpHandler();
if (serverConfig.isJmxEnabled()) {
for (final JmxEventListener l : serverConfig.getJmxEventListeners()) {
l.jmxEnabled();
}
}
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "[{0}] Started.", getServerConfiguration().getName());
}
}
private void setupHttpHandler() {
serverConfig.addJmxEventListener(httpHandlerChain);
synchronized (serverConfig.handlersSync) {
for (final HttpHandler httpHandler : serverConfig.orderedHandlers) {
httpHandlerChain.addHandler(httpHandler,
serverConfig.handlers.get(httpHandler));
}
}
httpHandlerChain.start();
}
private void tearDownHttpHandler() {
httpHandlerChain.destroy();
}
public HttpHandler getHttpHandler() {
return httpHandlerChain;
}
public boolean isStarted() {
return state != State.STOPPED;
}
public Object getManagementObject(boolean clear) {
if (!clear && managementObject == null) {
synchronized (serverConfig) {
if (managementObject == null) {
managementObject = MonitoringUtils.loadJmxObject(
"org.glassfish.grizzly.http.server.jmx.HttpServer",
this, HttpServer.class);
}
}
}
try {
return managementObject;
} finally {
if (clear) {
managementObject = null;
}
}
}
public synchronized GrizzlyFuture<HttpServer> shutdown(final long gracePeriod,
final TimeUnit timeUnit) {
if (state != State.RUNNING) {
return shutdownFuture != null ? shutdownFuture :
Futures.createReadyFuture(this);
}
shutdownFuture = Futures.createSafeFuture();
state = State.STOPPING;
final int listenersCount = listeners.size();
final FutureImpl<HttpServer> shutdownFutureLocal = shutdownFuture;
final CompletionHandler<NetworkListener> shutdownCompletionHandler =
new EmptyCompletionHandler<NetworkListener>() {
final AtomicInteger counter =
new AtomicInteger(listenersCount);
@Override
public void completed(final NetworkListener networkListener) {
if (counter.decrementAndGet() == 0) {
try {
shutdownFutureLocal.result(HttpServer.this);
} catch (Throwable e) {
shutdownFutureLocal.failure(e);
}
}
}
};
if (listenersCount > 0) {
for (NetworkListener listener : listeners.values()) {
listener.shutdown(gracePeriod, timeUnit).addCompletionHandler(shutdownCompletionHandler);
}
} else {
shutdownNow();
shutdownFutureLocal.result(HttpServer.this);
}
return shutdownFuture;
}
public synchronized GrizzlyFuture<HttpServer> shutdown() {
return shutdown(-1, TimeUnit.MILLISECONDS);
}
public synchronized void shutdownNow() {
if (state == State.STOPPED) {
return;
}
state = State.STOPPED;
try {
if (serverConfig.isJmxEnabled()) {
for (final JmxEventListener l : serverConfig.getJmxEventListeners()) {
l.jmxDisabled();
}
}
tearDownHttpHandler();
final String[] names = listeners.keySet().toArray(new String[listeners.size()]);
for (final String name : names) {
removeListener(name);
}
delayedExecutor.stop();
delayedExecutor.destroy();
delayedExecutor = null;
stopAuxThreadPool();
if (serverConfig.isJmxEnabled()) {
disableJMX();
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, null, e);
} finally {
for (final NetworkListener listener : listeners.values()) {
final Processor p = listener.getTransport().getProcessor();
if (p instanceof FilterChain) {
((FilterChain) p).clear();
}
}
if (shutdownFuture != null) {
shutdownFuture.result(this);
}
}
}
public void stop() {
shutdownNow();
}
public static HttpServer createSimpleServer() {
return createSimpleServer(".");
}
public static HttpServer createSimpleServer(final String docRoot) {
return createSimpleServer(docRoot, NetworkListener.DEFAULT_NETWORK_PORT);
}
public static HttpServer createSimpleServer(final String docRoot,
final int port) {
return createSimpleServer(docRoot, NetworkListener.DEFAULT_NETWORK_HOST, port);
}
public static HttpServer createSimpleServer(final String docRoot,
final PortRange range) {
return createSimpleServer(docRoot,
NetworkListener.DEFAULT_NETWORK_HOST,
range);
}
public static HttpServer createSimpleServer(final String docRoot,
final SocketAddress socketAddress) {
final InetSocketAddress inetAddr = (InetSocketAddress) socketAddress;
return createSimpleServer(docRoot, inetAddr.getHostName(), inetAddr.getPort());
}
public static HttpServer createSimpleServer(final String docRoot,
final String host,
final int port) {
return createSimpleServer(docRoot, host, new PortRange(port));
}
public static HttpServer createSimpleServer(final String docRoot,
final String host,
final PortRange range) {
final HttpServer server = new HttpServer();
final ServerConfiguration config = server.getServerConfiguration();
if (docRoot != null) {
config.addHttpHandler(new StaticHttpHandler(docRoot), "/");
}
final NetworkListener listener =
new NetworkListener("grizzly",
host,
range);
server.addListener(listener);
return server;
}
protected void enableJMX() {
if (jmxManager == null) {
synchronized (serverConfig) {
if (jmxManager == null) {
jmxManager= GrizzlyJmxManager.instance();
}
}
}
jmxManager.registerAtRoot(getManagementObject(false),
serverConfig.getName());
}
protected void disableJMX() {
if (jmxManager != null) {
jmxManager.deregister(getManagementObject(true));
}
}
private void configureListener(final NetworkListener listener) {
FilterChain chain = listener.getFilterChain();
if (chain == null) {
final FilterChainBuilder builder = FilterChainBuilder.stateless();
builder.add(new TransportFilter());
if (listener.isSecure()) {
SSLEngineConfigurator sslConfig = listener.getSslEngineConfig();
if (sslConfig == null) {
sslConfig = new SSLEngineConfigurator(
SSLContextConfigurator.DEFAULT_CONFIG,
false,
false,
false);
listener.setSSLEngineConfig(sslConfig);
}
final SSLBaseFilter filter = new SSLBaseFilter(sslConfig);
builder.add(filter);
}
final int maxHeaderSize = listener.getMaxHttpHeaderSize() == -1
? org.glassfish.grizzly.http.HttpServerFilter.DEFAULT_MAX_HTTP_PACKET_HEADER_SIZE
: listener.getMaxHttpHeaderSize();
@SuppressWarnings("deprecation")
final org.glassfish.grizzly.http.HttpServerFilter httpServerCodecFilter =
new org.glassfish.grizzly.http.HttpServerFilter(listener.isChunkingEnabled(),
maxHeaderSize,
null,
listener.getKeepAlive(),
null,
listener.getMaxRequestHeaders(),
listener.getMaxResponseHeaders());
final Set<ContentEncoding> contentEncodings =
configureCompressionEncodings(listener);
for (ContentEncoding contentEncoding : contentEncodings) {
httpServerCodecFilter.addContentEncoding(contentEncoding);
}
httpServerCodecFilter.setAllowPayloadForUndefinedHttpMethods(
serverConfig.isAllowPayloadForUndefinedHttpMethods());
httpServerCodecFilter.setMaxPayloadRemainderToSkip(
serverConfig.getMaxPayloadRemainderToSkip());
httpServerCodecFilter.getMonitoringConfig().addProbes(
serverConfig.getMonitoringConfig().getHttpConfig().getProbes());
builder.add(httpServerCodecFilter);
builder.add(new IdleTimeoutFilter(delayedExecutor,
listener.getKeepAlive().getIdleTimeoutInSeconds(),
TimeUnit.SECONDS));
final Transport transport = listener.getTransport();
final FileCache fileCache = listener.getFileCache();
fileCache.initialize(delayedExecutor);
final FileCacheFilter fileCacheFilter = new FileCacheFilter(fileCache);
fileCache.getMonitoringConfig().addProbes(
serverConfig.getMonitoringConfig().getFileCacheConfig().getProbes());
builder.add(fileCacheFilter);
final ServerFilterConfiguration config = new ServerFilterConfiguration(serverConfig);
if (listener.isSendFileExplicitlyConfigured()) {
config.setSendFileEnabled(listener.isSendFileEnabled());
fileCache.setFileSendEnabled(listener.isSendFileEnabled());
}
if (listener.getBackendConfiguration() != null) {
config.setBackendConfiguration(listener.getBackendConfiguration());
}
if (listener.getDefaultErrorPageGenerator() != null) {
config.setDefaultErrorPageGenerator(listener.getDefaultErrorPageGenerator());
}
if (listener.getSessionManager() != null) {
config.setSessionManager(listener.getSessionManager());
}
config.setTraceEnabled(config.isTraceEnabled() || listener.isTraceEnabled());
config.setMaxFormPostSize(listener.getMaxFormPostSize());
config.setMaxBufferedPostSize(listener.getMaxBufferedPostSize());
final HttpServerFilter httpServerFilter = new HttpServerFilter(
config,
delayedExecutor);
httpServerFilter.setHttpHandler(httpHandlerChain);
httpServerFilter.getMonitoringConfig().addProbes(
serverConfig.getMonitoringConfig().getWebServerConfig().getProbes());
builder.add(httpServerFilter);
final AddOn[] addons = listener.getAddOnSet().getArray();
if (addons != null) {
for (AddOn addon : addons) {
addon.setup(listener, builder);
}
}
chain = builder.build();
listener.setFilterChain(chain);
final int transactionTimeout = listener.getTransactionTimeout();
if (transactionTimeout >= 0) {
ThreadPoolConfig threadPoolConfig = transport.getWorkerThreadPoolConfig();
if (threadPoolConfig != null) {
threadPoolConfig.setTransactionTimeout(
delayedExecutor,
transactionTimeout,
TimeUnit.SECONDS);
}
}
}
configureMonitoring(listener);
}
protected Set<ContentEncoding> configureCompressionEncodings(
final NetworkListener listener) {
final CompressionConfig compressionConfig = listener.getCompressionConfig();
if (compressionConfig.getCompressionMode() != CompressionMode.OFF) {
final ContentEncoding gzipContentEncoding = new GZipContentEncoding(
GZipContentEncoding.DEFAULT_IN_BUFFER_SIZE,
GZipContentEncoding.DEFAULT_OUT_BUFFER_SIZE,
new CompressionEncodingFilter(compressionConfig,
GZipContentEncoding.getGzipAliases()));
final ContentEncoding lzmaEncoding = new LZMAContentEncoding(
new CompressionEncodingFilter(compressionConfig,
LZMAContentEncoding.getLzmaAliases()));
final Set<ContentEncoding> set = new HashSet<>(2);
set.add(gzipContentEncoding);
set.add(lzmaEncoding);
return set;
} else {
return Collections.emptySet();
}
}
@SuppressWarnings("unchecked")
private void configureMonitoring(final NetworkListener listener) {
final TCPNIOTransport transport = listener.getTransport();
final MonitoringConfig<TransportProbe> transportMonitoringCfg =
transport.getMonitoringConfig();
final MonitoringConfig<ConnectionProbe> connectionMonitoringCfg =
transport.getConnectionMonitoringConfig();
final MonitoringConfig<MemoryProbe> memoryMonitoringCfg =
transport.getMemoryManager().getMonitoringConfig();
final MonitoringConfig<ThreadPoolProbe> threadPoolMonitoringCfg =
transport.getThreadPoolMonitoringConfig();
transportMonitoringCfg.addProbes(serverConfig.getMonitoringConfig()
.getTransportConfig().getProbes());
connectionMonitoringCfg.addProbes(serverConfig.getMonitoringConfig()
.getConnectionConfig().getProbes());
memoryMonitoringCfg.addProbes(serverConfig.getMonitoringConfig()
.getMemoryConfig().getProbes());
threadPoolMonitoringCfg.addProbes(serverConfig.getMonitoringConfig()
.getThreadPoolConfig().getProbes());
}
private void configureAuxThreadPool() {
final AtomicInteger threadCounter = new AtomicInteger();
auxExecutorService = Executors.newCachedThreadPool(
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
final Thread newThread = new DefaultWorkerThread(
AttributeBuilder.DEFAULT_ATTRIBUTE_BUILDER,
serverConfig.getName() + "-" + threadCounter.getAndIncrement(),
null,
r);
newThread.setDaemon(true);
return newThread;
}
});
}
private void stopAuxThreadPool() {
final ExecutorService localThreadPool = auxExecutorService;
auxExecutorService = null;
if (localThreadPool != null) {
localThreadPool.shutdownNow();
}
}
synchronized void onAddHttpHandler(HttpHandler httpHandler,
final HttpHandlerRegistration[] registrations) {
if (isStarted()) {
httpHandlerChain.addHandler(httpHandler, registrations);
}
}
synchronized void onRemoveHttpHandler(HttpHandler httpHandler) {
if (isStarted()) {
httpHandlerChain.removeHttpHandler(httpHandler);
}
}
}