diff --git a/.gitignore b/.gitignore index bcb4fe394c8..58733e5100b 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ mainframer # exclude file created by the flatten plugin .flattened-pom.xml +.java-version diff --git a/example/src/main/java/io/netty/example/discard/DiscardClient.java b/example/src/main/java/io/netty/example/discard/DiscardClient.java index 160c317aa4e..32562f89ae5 100644 --- a/example/src/main/java/io/netty/example/discard/DiscardClient.java +++ b/example/src/main/java/io/netty/example/discard/DiscardClient.java @@ -23,29 +23,21 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; /** * Keeps sending random data to the specified address. */ public final class DiscardClient { - static final boolean SSL = System.getProperty("ssl") != null; static final String HOST = System.getProperty("host", "127.0.0.1"); static final int PORT = Integer.parseInt(System.getProperty("port", "8009")); static final int SIZE = Integer.parseInt(System.getProperty("size", "256")); public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - sslCtx = SslContextBuilder.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup group = new NioEventLoopGroup(); try { diff --git a/example/src/main/java/io/netty/example/discard/DiscardServer.java b/example/src/main/java/io/netty/example/discard/DiscardServer.java index 64a671a22d8..33c7ff19920 100644 --- a/example/src/main/java/io/netty/example/discard/DiscardServer.java +++ b/example/src/main/java/io/netty/example/discard/DiscardServer.java @@ -23,29 +23,21 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * Discards any incoming data. */ public final class DiscardServer { - static final boolean SSL = System.getProperty("ssl") != null; static final int PORT = Integer.parseInt(System.getProperty("port", "8009")); public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/echo/EchoClient.java b/example/src/main/java/io/netty/example/echo/EchoClient.java index 20ec30d2e2a..c7ccc484ad7 100644 --- a/example/src/main/java/io/netty/example/echo/EchoClient.java +++ b/example/src/main/java/io/netty/example/echo/EchoClient.java @@ -24,9 +24,8 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; /** * Sends one message when a connection is open and echoes back any received @@ -36,20 +35,13 @@ */ public final class EchoClient { - static final boolean SSL = System.getProperty("ssl") != null; static final String HOST = System.getProperty("host", "127.0.0.1"); static final int PORT = Integer.parseInt(System.getProperty("port", "8007")); static final int SIZE = Integer.parseInt(System.getProperty("size", "256")); public static void main(String[] args) throws Exception { // Configure SSL.git - final SslContext sslCtx; - if (SSL) { - sslCtx = SslContextBuilder.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/echo/EchoServer.java b/example/src/main/java/io/netty/example/echo/EchoServer.java index 65ef83539dd..69625458be0 100644 --- a/example/src/main/java/io/netty/example/echo/EchoServer.java +++ b/example/src/main/java/io/netty/example/echo/EchoServer.java @@ -24,29 +24,21 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * Echoes back any received data from a client. */ public final class EchoServer { - static final boolean SSL = System.getProperty("ssl") != null; static final int PORT = Integer.parseInt(System.getProperty("port", "8007")); public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); diff --git a/example/src/main/java/io/netty/example/factorial/FactorialClient.java b/example/src/main/java/io/netty/example/factorial/FactorialClient.java index 3fcee681a31..85123d4fc51 100644 --- a/example/src/main/java/io/netty/example/factorial/FactorialClient.java +++ b/example/src/main/java/io/netty/example/factorial/FactorialClient.java @@ -20,9 +20,8 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; /** * Sends a sequence of integers to a {@link FactorialServer} to calculate @@ -30,20 +29,13 @@ */ public final class FactorialClient { - static final boolean SSL = System.getProperty("ssl") != null; static final String HOST = System.getProperty("host", "127.0.0.1"); static final int PORT = Integer.parseInt(System.getProperty("port", "8322")); static final int COUNT = Integer.parseInt(System.getProperty("count", "1000")); public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - sslCtx = SslContextBuilder.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup group = new NioEventLoopGroup(); try { diff --git a/example/src/main/java/io/netty/example/factorial/FactorialServer.java b/example/src/main/java/io/netty/example/factorial/FactorialServer.java index 3ac08f9bf7b..3ac93e53acf 100644 --- a/example/src/main/java/io/netty/example/factorial/FactorialServer.java +++ b/example/src/main/java/io/netty/example/factorial/FactorialServer.java @@ -19,11 +19,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * Receives a sequence of integers from a {@link FactorialClient} to calculate @@ -31,18 +30,11 @@ */ public final class FactorialServer { - static final boolean SSL = System.getProperty("ssl") != null; static final int PORT = Integer.parseInt(System.getProperty("port", "8322")); public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/file/FileServer.java b/example/src/main/java/io/netty/example/file/FileServer.java index 7a1c7dd762c..8b2eb7c319d 100644 --- a/example/src/main/java/io/netty/example/file/FileServer.java +++ b/example/src/main/java/io/netty/example/file/FileServer.java @@ -24,14 +24,13 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; 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 io.netty.handler.stream.ChunkedWriteHandler; import io.netty.util.CharsetUtil; @@ -46,13 +45,7 @@ public final class FileServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); diff --git a/example/src/main/java/io/netty/example/http/cors/HttpCorsServer.java b/example/src/main/java/io/netty/example/http/cors/HttpCorsServer.java index 1ee5aad6d8d..f9908ea2a99 100644 --- a/example/src/main/java/io/netty/example/http/cors/HttpCorsServer.java +++ b/example/src/main/java/io/netty/example/http/cors/HttpCorsServer.java @@ -19,11 +19,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * This example server aims to demonstrate @@ -78,13 +77,7 @@ public final class HttpCorsServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServer.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServer.java index d9f495e02ff..adeca2ada59 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServer.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServer.java @@ -20,12 +20,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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.SslProvider; -import io.netty.handler.ssl.util.SelfSignedCertificate; public final class HttpStaticFileServer { @@ -34,14 +32,7 @@ public final class HttpStaticFileServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) - .sslProvider(SslProvider.JDK).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/http/helloworld/HttpHelloWorldServer.java b/example/src/main/java/io/netty/example/http/helloworld/HttpHelloWorldServer.java index 903e2eecaff..23ce5a747d7 100644 --- a/example/src/main/java/io/netty/example/http/helloworld/HttpHelloWorldServer.java +++ b/example/src/main/java/io/netty/example/http/helloworld/HttpHelloWorldServer.java @@ -21,11 +21,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * An HTTP server that sends back the content of the received HTTP request @@ -38,13 +37,7 @@ public final class HttpHelloWorldServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); diff --git a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServer.java b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServer.java index 2079d359d37..b6f6a239724 100644 --- a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServer.java +++ b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServer.java @@ -20,11 +20,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * An HTTP server that sends back the content of the received HTTP request @@ -37,13 +36,7 @@ public final class HttpSnoopServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadServer.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadServer.java index b24689f6d57..1c1abaa1e53 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadServer.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadServer.java @@ -20,11 +20,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * An HTTP server showing how to use the HTTP multipart package for file uploads and decoding post data. @@ -36,13 +35,7 @@ public final class HttpUploadServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServer.java b/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServer.java index 6a7cd6b61f1..a344df8aa6a 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServer.java +++ b/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServer.java @@ -20,9 +20,8 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.SelfSignedCertificate; /** * A Benchmark application for websocket which is served at: @@ -39,13 +38,7 @@ public final class WebSocketServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServer.java b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServer.java index 239c5c09e12..ad1e8efab4f 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServer.java +++ b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServer.java @@ -20,11 +20,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * An HTTP server which serves Web Socket requests at: @@ -52,13 +51,7 @@ public final class WebSocketServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/objectecho/ObjectEchoServer.java b/example/src/main/java/io/netty/example/objectecho/ObjectEchoServer.java index 3e72156fc4a..b8778696bcc 100644 --- a/example/src/main/java/io/netty/example/objectecho/ObjectEchoServer.java +++ b/example/src/main/java/io/netty/example/objectecho/ObjectEchoServer.java @@ -23,14 +23,13 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.example.echo.EchoServer; +import io.netty.example.util.ServerUtil; import io.netty.handler.codec.serialization.ClassResolvers; import io.netty.handler.codec.serialization.ObjectDecoder; import io.netty.handler.codec.serialization.ObjectEncoder; 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; /** * Modification of {@link EchoServer} which utilizes Java object serialization. @@ -42,13 +41,7 @@ public final class ObjectEchoServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/telnet/TelnetClient.java b/example/src/main/java/io/netty/example/telnet/TelnetClient.java index 04395767621..f75354cfc0b 100644 --- a/example/src/main/java/io/netty/example/telnet/TelnetClient.java +++ b/example/src/main/java/io/netty/example/telnet/TelnetClient.java @@ -21,9 +21,8 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -39,13 +38,7 @@ public final class TelnetClient { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - sslCtx = SslContextBuilder.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup group = new NioEventLoopGroup(); try { diff --git a/example/src/main/java/io/netty/example/telnet/TelnetServer.java b/example/src/main/java/io/netty/example/telnet/TelnetServer.java index 66854bc167e..5d0674542ad 100644 --- a/example/src/main/java/io/netty/example/telnet/TelnetServer.java +++ b/example/src/main/java/io/netty/example/telnet/TelnetServer.java @@ -19,11 +19,10 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * Simplistic telnet server. @@ -35,13 +34,7 @@ public final class TelnetServer { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - SelfSignedCertificate ssc = new SelfSignedCertificate(); - sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/example/src/main/java/io/netty/example/util/ServerUtil.java b/example/src/main/java/io/netty/example/util/ServerUtil.java new file mode 100644 index 00000000000..1f811137b44 --- /dev/null +++ b/example/src/main/java/io/netty/example/util/ServerUtil.java @@ -0,0 +1,44 @@ +/* + * Copyright 2022 The Netty Project + * + * The Netty Project 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: + * + * https://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. + */ +package io.netty.example.util; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.SelfSignedCertificate; + +import javax.net.ssl.SSLException; +import java.security.cert.CertificateException; + +/** + * Some useful methods for server side. + */ +public final class ServerUtil { + + private static final boolean SSL = System.getProperty("ssl") != null; + + private ServerUtil() { + } + + public static SslContext buildSslContext() throws CertificateException, SSLException { + if (!SSL) { + return null; + } + SelfSignedCertificate ssc = new SelfSignedCertificate(); + return SslContextBuilder + .forServer(ssc.certificate(), ssc.privateKey()) + .build(); + } +} diff --git a/example/src/main/java/io/netty/example/util/package-info.java b/example/src/main/java/io/netty/example/util/package-info.java new file mode 100644 index 00000000000..61627f4cbb8 --- /dev/null +++ b/example/src/main/java/io/netty/example/util/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2022 The Netty Project + * + * The Netty Project 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: + * + * https://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. + */ + +/** + * Util class. + */ +package io.netty.example.util; + diff --git a/example/src/main/java/io/netty/example/worldclock/WorldClockClient.java b/example/src/main/java/io/netty/example/worldclock/WorldClockClient.java index 3d7a0843bf4..c1b977f8282 100644 --- a/example/src/main/java/io/netty/example/worldclock/WorldClockClient.java +++ b/example/src/main/java/io/netty/example/worldclock/WorldClockClient.java @@ -20,9 +20,8 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.example.util.ServerUtil; import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import java.util.Arrays; import java.util.List; @@ -33,7 +32,6 @@ */ public final class WorldClockClient { - static final boolean SSL = System.getProperty("ssl") != null; static final String HOST = System.getProperty("host", "127.0.0.1"); static final int PORT = Integer.parseInt(System.getProperty("port", "8463")); static final List CITIES = Arrays.asList(System.getProperty( @@ -41,13 +39,7 @@ public final class WorldClockClient { public static void main(String[] args) throws Exception { // Configure SSL. - final SslContext sslCtx; - if (SSL) { - sslCtx = SslContextBuilder.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); - } else { - sslCtx = null; - } + final SslContext sslCtx = ServerUtil.buildSslContext(); EventLoopGroup group = new NioEventLoopGroup(); try { diff --git a/example/src/main/java/io/netty/example/worldclock/WorldClockServer.java b/example/src/main/java/io/netty/example/worldclock/WorldClockServer.java index ba45b1518d4..9ac24a5002c 100644 --- a/example/src/main/java/io/netty/example/worldclock/WorldClockServer.java +++ b/example/src/main/java/io/netty/example/worldclock/WorldClockServer.java @@ -19,11 +19,9 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.example.util.ServerUtil; 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; /** * Receives a list of continent/city pairs from a {@link WorldClockClient} to @@ -31,18 +29,9 @@ */ public final class WorldClockServer { - static final boolean SSL = System.getProperty("ssl") != null; static final int PORT = Integer.parseInt(System.getProperty("port", "8463")); public static void main(String[] args) 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(); @@ -51,7 +40,7 @@ public static void main(String[] args) throws Exception { b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) - .childHandler(new WorldClockServerInitializer(sslCtx)); + .childHandler(new WorldClockServerInitializer(ServerUtil.buildSslContext())); b.bind(PORT).sync().channel().closeFuture().sync(); } finally {