View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.spdy.server;
17  
18  import io.netty.bootstrap.ServerBootstrap;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.EventLoopGroup;
22  import io.netty.channel.MultiThreadIoEventLoopGroup;
23  import io.netty.channel.nio.NioIoHandler;
24  import io.netty.channel.socket.nio.NioServerSocketChannel;
25  import io.netty.handler.logging.LogLevel;
26  import io.netty.handler.logging.LoggingHandler;
27  import io.netty.handler.ssl.ApplicationProtocolConfig;
28  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
29  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
30  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
31  import io.netty.handler.ssl.ApplicationProtocolNames;
32  import io.netty.handler.ssl.SslContext;
33  import io.netty.handler.ssl.SslContextBuilder;
34  import io.netty.handler.ssl.util.SelfSignedCertificate;
35  
36  /**
37   * A SPDY Server that responds to a GET request with a Hello World.
38   * <p>
39   * This class must be run with the JVM parameter: {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}.
40   * The "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
41   * Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
42   * See <a href="https://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more
43   * information.
44   * <p>
45   * You may also use the {@code run-example.sh} script to start the server from the command line:
46   * <pre>
47   *     ./run-example.sh spdy-server
48   * </pre>
49   * <p>
50   * Once started, you can test the server with your
51   * <a href="https://en.wikipedia.org/wiki/SPDY#Browser_support_and_usage">SPDY enabled web browser</a> by navigating
52   * to <a href="https://localhost:8443/">https://localhost:8443/</a>
53   */
54  public final class SpdyServer {
55  
56      static final int PORT = Integer.parseInt(System.getProperty("port", "8443"));
57  
58      public static void main(String[] args) throws Exception {
59          // Configure SSL.
60          SelfSignedCertificate ssc = new SelfSignedCertificate();
61          SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
62              .applicationProtocolConfig(new ApplicationProtocolConfig(
63                          Protocol.NPN,
64                          // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
65                          SelectorFailureBehavior.NO_ADVERTISE,
66                          // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
67                          SelectedListenerFailureBehavior.ACCEPT,
68                          ApplicationProtocolNames.SPDY_3_1,
69                          ApplicationProtocolNames.HTTP_1_1))
70              .build();
71  
72          // Configure the server.
73          EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
74          EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
75          try {
76              ServerBootstrap b = new ServerBootstrap();
77              b.option(ChannelOption.SO_BACKLOG, 1024);
78              b.group(bossGroup, workerGroup)
79               .channel(NioServerSocketChannel.class)
80               .handler(new LoggingHandler(LogLevel.INFO))
81               .childHandler(new SpdyServerInitializer(sslCtx));
82  
83              Channel ch = b.bind(PORT).sync().channel();
84  
85              System.err.println("Open your SPDY-enabled web browser and navigate to https://127.0.0.1:" + PORT + '/');
86              System.err.println("If using Chrome browser, check your SPDY sessions at chrome://net-internals/#spdy");
87  
88              ch.closeFuture().sync();
89          } finally {
90              bossGroup.shutdownGracefully();
91              workerGroup.shutdownGracefully();
92          }
93      }
94  }