Example usage for io.netty.handler.ssl SslContext newServerContext

List of usage examples for io.netty.handler.ssl SslContext newServerContext

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslContext newServerContext.

Prototype

@Deprecated
public static SslContext newServerContext(File certChainFile, File keyFile, String keyPassword,
        Iterable<String> ciphers, Iterable<String> nextProtocols, long sessionCacheSize, long sessionTimeout)
        throws SSLException 

Source Link

Document

Creates a new server-side SslContext .

Usage

From source file:Http2Server.java

License:Apache License

public static void main(String... args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*  w  w  w.j  a  v a2s. c  om*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null, null,
                Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()),
                0, 0);
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(1);
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new Http2ServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.err.println("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http")
                + "://127.0.0.1:" + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.sangupta.swift.netty.spdy.SpdyStaticFileServer.java

License:Apache License

public SpdyStaticFileServer(SwiftServer server) {
    if (server.isSpdyEnabled() && !server.isSslEnabled()) {
        throw new IllegalStateException("SPDY can only be enabled along with SSL");
    }//from   w  ww. ja  v a2s.  c o  m

    if (server.isSslEnabled()) {
        if (server.isSelfSignedSSL()) {
            SelfSignedCertificate ssc;

            if (server.isSpdyEnabled()) {
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null,
                            null, Arrays.asList(SelectedProtocol.SPDY_3_1.protocolName(),
                                    SelectedProtocol.HTTP_1_1.protocolName()),
                            0, 0);
                } catch (CertificateException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            } else {
                // basic self signed cert
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(),
                            ssc.privateKey());
                } catch (CertificateException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            }
        }
    } else {
        this.sslContext = null;
    }

    this.bossGroup = new NioEventLoopGroup(1);
    this.workerGroup = new NioEventLoopGroup();

    try {
        this.serverBootstrap = new ServerBootstrap();
        if (server.isSpdyEnabled()) {
            this.serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
        }

        SpdyStaticFileServerHandler fileServerHandler = new SpdyStaticFileServerHandler(server);

        this.serverBootstrap.group(this.bossGroup, this.workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SpdyStaticFileServerInitializer(this.sslContext, fileServerHandler));

        if (AssertUtils.isNotEmpty(server.getServerName())) {
            this.channel = this.serverBootstrap.bind(server.getServerName(), server.getListenPort()).sync()
                    .channel();
        } else {
            this.channel = this.serverBootstrap.bind(server.getListenPort()).sync().channel();
        }

        System.out.println("Listening on port " + server.getListenPort());

        this.channel.closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO: think what we can do with this
    } finally {
        this.shutdownGracefully();
    }
}

From source file:io.aos.netty5.http2.server.Http2Server.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//w  ww.j a va2s.c  o  m
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null, null,
                Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()),
                0, 0);
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new Http2ServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.err.println("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http")
                + "://127.0.0.1:" + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}