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

com.hibegin.http.server.WebServerBuilder Maven / Gradle / Ivy

Go to download

Simple, flexible, less dependent, more extended. Less memory footprint, can quickly build Web project. Can quickly run embedded, Android devices

There is a newer version: 0.3.162
Show newest version
package com.hibegin.http.server;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.config.AbstractServerConfig;
import com.hibegin.http.server.config.RequestConfig;
import com.hibegin.http.server.config.ResponseConfig;
import com.hibegin.http.server.config.ServerConfig;
import com.hibegin.http.server.web.MethodInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WebServerBuilder {

    private static final Logger LOGGER = LoggerUtil.getLogger(WebServerBuilder.class);

    private final RequestConfig requestConfig;

    private final ResponseConfig responseConfig;

    private ServerConfig serverConfig;

    private SimpleWebServer webServer;

    private final List> onCreateErrorHandles = new ArrayList<>();
    private final List> onCreateSuccessHandles = new ArrayList<>();
    private final List> onStartedSuccessHandles = new ArrayList<>();

    public void addCreateErrorHandle(Callable callable) {
        onCreateErrorHandles.add(callable);
    }

    public void addStartedSuccessHandle(Callable callable) {
        onStartedSuccessHandles.add(callable);
    }

    public void addCreateSuccessHandle(Callable callable) {
        onCreateSuccessHandles.add(callable);
    }

    private WebServerBuilder(Builder builder) {
        this.serverConfig = builder.serverConfig;
        this.responseConfig = builder.responseConfig;
        this.requestConfig = builder.requestConfig;
    }

    public SimpleWebServer startInBackground() {
        startWithThread();
        return webServer;
    }

    public SimpleWebServer startInBackground(ThreadFactory threadFactory) {
        startWithThread(threadFactory);
        return webServer;
    }

    public void start() {
        if (create()) {
            startListen();
        }
    }

    private boolean create() {
        if (serverConfig == null) {
            serverConfig = new ServerConfig();
        }
        if (serverConfig.getInterceptors().isEmpty()) {
            serverConfig.addInterceptor(MethodInterceptor.class);
        }
        if (Objects.isNull(this.webServer)) {
            if (serverConfig.isSsl()) {
                this.webServer = new SimpleHttpsWebServer(serverConfig, requestConfig, responseConfig);
            } else {
                this.webServer = new SimpleWebServer(serverConfig, requestConfig, responseConfig);
            }
        }
        boolean createSuccess;
        if (Objects.nonNull(serverConfig.getPort()) && serverConfig.getPort() >= 0) {
            createSuccess = webServer.create(serverConfig.getHost(), serverConfig.getPort());
        } else {
            createSuccess = webServer.create();
        }
        if (!createSuccess) {
            onCreateErrorHandles.forEach(e -> {
                try {
                    e.call();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
        }
        return createSuccess;
    }

    public boolean startWithThread() {
        startWithThread(runnable -> {
            Thread thread = new Thread(runnable);
            thread.setName(serverConfig.getApplicationName().toLowerCase() + "-main-thread");
            return thread;
        });
        return false;
    }

    private void startListen() {
        onCreateSuccessHandles.forEach(e -> {
            try {
                e.call();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        this.webServer.listener();
        onStartedSuccessHandles.forEach(e -> {
            try {
                e.call();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    public boolean startWithThread(ThreadFactory threadFactory) {
        try {
            boolean created = create();
            if (created) {
                threadFactory.newThread(this::startListen).start();
            }
            return created;
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "", e);
        }
        return false;
    }

    public SimpleWebServer getWebServer() {
        return webServer;
    }

    public static class Builder {

        private RequestConfig requestConfig;

        private ResponseConfig responseConfig;

        private ServerConfig serverConfig;

        public Builder() {
        }

        public Builder requestConfig(RequestConfig requestConfig) {
            this.requestConfig = requestConfig;
            return this;
        }

        public Builder responseConfig(ResponseConfig responseConfig) {
            this.responseConfig = responseConfig;
            return this;
        }

        public Builder serverConfig(ServerConfig serverConfig) {
            this.serverConfig = serverConfig;
            return this;
        }

        public Builder config(AbstractServerConfig config) {
            serverConfig(config.getServerConfig());
            responseConfig(config.getResponseConfig());
            requestConfig(config.getRequestConfig());
            return this;
        }

        public WebServerBuilder build() {
            return new WebServerBuilder(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy