View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.embedded;
20  
21  import org.eclipse.jetty.server.HttpConfiguration;
22  import org.eclipse.jetty.server.HttpConnectionFactory;
23  import org.eclipse.jetty.server.SecureRequestCustomizer;
24  import org.eclipse.jetty.server.Server;
25  import org.eclipse.jetty.server.ServerConnector;
26  import org.eclipse.jetty.server.SslConnectionFactory;
27  import org.eclipse.jetty.spdy.server.NPNServerConnectionFactory;
28  import org.eclipse.jetty.spdy.server.SPDYServerConnectionFactory;
29  import org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory;
30  import org.eclipse.jetty.spdy.server.http.ReferrerPushStrategy;
31  import org.eclipse.jetty.util.ssl.SslContextFactory;
32  
33  /* ------------------------------------------------------------ */
34  /**
35   * A Jetty server with HTTP and SPDY connectors.
36   */
37  public class SpdyConnector
38  {
39      public static void main(String[] args) throws Exception
40      {
41          String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution");
42          System.setProperty("jetty.home", jetty_home);
43  
44          // The Server
45          Server server = new Server();
46  
47          // HTTP Configuration
48          HttpConfiguration http_config = new HttpConfiguration();
49          http_config.setSecureScheme("https");
50          http_config.setSecurePort(8443);
51  
52          // HTTP connector
53          ServerConnector http = new ServerConnector(server,new HttpConnectionFactory(http_config));        
54          http.setPort(8080);
55          server.addConnector(http);
56   
57          // SSL Context Factory for HTTPS and SPDY
58          SslContextFactory sslContextFactory = new SslContextFactory();
59          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
60          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
61          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
62  
63          // HTTPS Configuration
64          HttpConfiguration https_config = new HttpConfiguration(http_config);
65          https_config.addCustomizer(new SecureRequestCustomizer());
66          
67          // SPDY versions
68          HTTPSPDYServerConnectionFactory spdy2 = 
69              new HTTPSPDYServerConnectionFactory(2,https_config);
70  
71          HTTPSPDYServerConnectionFactory spdy3 = 
72              new HTTPSPDYServerConnectionFactory(3,https_config,new ReferrerPushStrategy());
73  
74          // NPN Factory
75          SPDYServerConnectionFactory.checkNPNAvailable();
76          NPNServerConnectionFactory npn = 
77              new NPNServerConnectionFactory(spdy3.getProtocol(),spdy2.getProtocol(),http.getDefaultProtocol());
78          npn.setDefaultProtocol(http.getDefaultProtocol());
79          
80          // SSL Factory
81          SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory,npn.getProtocol());
82          
83          // SPDY Connector
84          ServerConnector spdyConnector = 
85              new ServerConnector(server,ssl,npn,spdy3,spdy2,new HttpConnectionFactory(https_config));
86          spdyConnector.setPort(8443);
87          server.addConnector(spdyConnector);
88          
89          // Set a handler
90          server.setHandler(new HelloHandler());
91  
92          // Start the server
93          server.start();
94          server.join();
95      }
96  }