Java tutorial
/** * <h1>HttpServer.java</h1> <p> This program is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any later version; or, * at your choice, under the terms of the Mozilla Public License, v. 2.0. SPDX GPL-3.0+ or MPL-2.0+. * </p> <p> This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License and the Mozilla Public License for more details. </p> * <p> You should have received a copy of the GNU General Public License and the Mozilla Public * License along with this program. If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a> * and at <a href="http://mozilla.org/MPL/2.0">http://mozilla.org/MPL/2.0</a> . </p> <p> NB: for the * statement, include Easy Innova SL or other company/Person contributing the code. </p> <p> * 2015 Easy Innova, SL </p> * * @author Adri Llorens * @version 1.0 * @since 23/7/2015 */ package dpfmanager.shell.modules.server.core; import dpfmanager.shell.core.DPFManagerProperties; import dpfmanager.shell.core.config.BasicConfig; import dpfmanager.shell.core.context.DpfContext; import dpfmanager.shell.modules.messages.messages.LogMessage; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.SelfSignedCertificate; import org.apache.logging.log4j.Level; /** * A HTTP server showing how to use the HTTP multipart package for file uploads and decoding post * data. */ public final class HttpServer { private boolean SSL = false; private int PORT; private DpfContext context; public HttpServer(int port, DpfContext context) { this.PORT = port; this.context = context; } public void start() throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.handler(new LoggingHandler(LogLevel.INFO)); b.childHandler(new HttpServerInitializer(sslCtx, context)); Channel ch = b.bind(PORT).sync().channel(); context.send(BasicConfig.MODULE_MESSAGE, new LogMessage(getClass(), Level.DEBUG, DPFManagerProperties.getBundle().getString("startedServer").replace("%1", getServerUri()), true)); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public String getServerUri() { return (SSL ? "https" : "http") + "://127.0.0.1:" + PORT; } }