All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.webpieces.frontend2.impl.FrontEndServerManagerImpl Maven / Gradle / Ivy

Go to download

Create a webserver with this library in just 3 lines of code. just register your HttpRequestListener and it feeds you a FrontendSocket that you write HttpResponses to

There is a newer version: 2.1.1
Show newest version
package org.webpieces.frontend2.impl;

import java.util.concurrent.ScheduledExecutorService;

import org.webpieces.asyncserver.api.AsyncServer;
import org.webpieces.asyncserver.api.AsyncServerManager;
import org.webpieces.frontend2.api.HttpFrontendManager;
import org.webpieces.frontend2.api.HttpServer;
import org.webpieces.frontend2.api.HttpSvrConfig;
import org.webpieces.frontend2.api.StreamListener;
import org.webpieces.httpparser.api.HttpParser;
import org.webpieces.nio.api.SSLEngineFactory;
import org.webpieces.util.futures.FutureHelper;

import com.webpieces.http2engine.api.server.Http2ServerEngineFactory;

public class FrontEndServerManagerImpl implements HttpFrontendManager {

	private AsyncServerManager svrManager;
	private ScheduledExecutorService timer;
	private HttpParser httpParser;
	private Http2ServerEngineFactory http2EngineFactory;

	public FrontEndServerManagerImpl(
			AsyncServerManager svrManager, ScheduledExecutorService svc, Http2ServerEngineFactory http2EngineFactory, HttpParser httpParser) {
		this.timer = svc;
		this.svrManager = svrManager;
		this.http2EngineFactory = http2EngineFactory;
		this.httpParser = httpParser;
	}

	@Override
	public HttpServer createHttpServer(HttpSvrConfig config, StreamListener httpListener) {
		preconditionCheck(config);
		Layer1ServerListener listener = buildDatalListener(httpListener, false);
		AsyncServer tcpServer = svrManager.createTcpServer(config.asyncServerConfig, listener);
		HttpServerImpl frontend = new HttpServerImpl(tcpServer, config, listener);

		return frontend;
	}

	private Layer1ServerListener buildDatalListener(StreamListener httpListener, boolean isBackend) {
		ProxyStreamListener proxyStreamListener = new ProxyStreamListener(httpListener);
		Layer2Http11Handler http11 = new Layer2Http11Handler(httpParser, proxyStreamListener);
		Layer2Http2Handler http2 = new Layer2Http2Handler(http2EngineFactory, proxyStreamListener);
		FutureHelper futureUtil = new FutureHelper();
		Layer1ServerListener listener = new Layer1ServerListener(futureUtil, http11, http2, isBackend);
		return listener;
	}

	private void preconditionCheck(HttpSvrConfig config) {
		if(config.bindAddress == null)
			throw new IllegalArgumentException("config.bindAddress must be set");
		if(config.keepAliveTimeoutMs != null && timer == null)
			throw new IllegalArgumentException("keepAliveTimeoutMs must be null since no timer was given when HttpFrontendFactory.createFrontEnd was called");
		else if(config.maxConnectToRequestTimeoutMs != null && timer == null)
			throw new IllegalArgumentException("keepAliveTimeoutMs must be null since no timer was given when HttpFrontendFactory.createFrontEnd was called");
	}

	@Override
	public HttpServer createHttpsServer(HttpSvrConfig config, StreamListener httpListener,
                                        SSLEngineFactory factory) {
		preconditionCheck(config);
		Layer1ServerListener listener = buildDatalListener(httpListener, false);
		AsyncServer tcpServer = svrManager.createTcpServer(config.asyncServerConfig, listener, factory);
		HttpServerImpl frontend = new HttpServerImpl(tcpServer, config, listener);
		
		return frontend;
	}


	@Override
	public HttpServer createUpgradableServer(HttpSvrConfig config, StreamListener httpListener, SSLEngineFactory factory) {
		preconditionCheck(config);
		Layer1ServerListener listener = buildDatalListener(httpListener, false);
		AsyncServer tcpServer = svrManager.createUpgradableServer(config.asyncServerConfig, listener, factory);
		HttpServerImpl frontend = new HttpServerImpl(tcpServer, config, listener);
		
		return frontend;
	}
	
	@Override
	public HttpServer createBackendHttpsServer(HttpSvrConfig config, StreamListener httpListener, SSLEngineFactory factory) {
		preconditionCheck(config);
		Layer1ServerListener listener = buildDatalListener(httpListener, true);
		AsyncServer tcpServer = svrManager.createTcpServer(config.asyncServerConfig, listener, factory);
		HttpServerImpl frontend = new HttpServerImpl(tcpServer, config, listener);
		
		return frontend;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy