View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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 java.io.File;
22  import java.io.FileNotFoundException;
23  
24  import org.eclipse.jetty.server.HttpConfiguration;
25  import org.eclipse.jetty.server.HttpConnectionFactory;
26  import org.eclipse.jetty.server.SecureRequestCustomizer;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.server.ServerConnector;
29  import org.eclipse.jetty.server.SslConnectionFactory;
30  import org.eclipse.jetty.spdy.server.NPNServerConnectionFactory;
31  import org.eclipse.jetty.spdy.server.SPDYServerConnectionFactory;
32  import org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory;
33  import org.eclipse.jetty.spdy.server.http.ReferrerPushStrategy;
34  import org.eclipse.jetty.util.ssl.SslContextFactory;
35  
36  /**
37   * A Jetty server with HTTP and SPDY connectors.
38   */
39  public class SpdyConnector
40  {
41      public static void main(String[] args) throws Exception
42      {
43          // Path to as-built jetty-distribution directory
44          String jettyHomeBuild = "../../jetty-distribution/target/distribution";
45          
46          // Find jetty home directories
47          String homePath = System.getProperty("jetty.home", jettyHomeBuild);
48          File homeDir = new File(homePath);
49          if (!homeDir.exists())
50          {
51              throw new FileNotFoundException(homeDir.getAbsolutePath());
52          }
53          String jetty_home = homeDir.getAbsolutePath();
54          System.setProperty("jetty.home", jetty_home);
55  
56          // The Server
57          Server server = new Server();
58  
59          // HTTP Configuration
60          HttpConfiguration http_config = new HttpConfiguration();
61          http_config.setSecureScheme("https");
62          http_config.setSecurePort(8443);
63  
64          // HTTP connector
65          ServerConnector http = new ServerConnector(server,
66                  new HttpConnectionFactory(http_config));        
67          http.setPort(8080);
68          server.addConnector(http);
69   
70          // SSL Context Factory for HTTPS and SPDY
71          SslContextFactory sslContextFactory = new SslContextFactory();
72          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
73          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
74          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
75  
76          // HTTPS Configuration
77          HttpConfiguration https_config = new HttpConfiguration(http_config);
78          https_config.addCustomizer(new SecureRequestCustomizer());
79          
80          // SPDY versions
81          HTTPSPDYServerConnectionFactory spdy2 = 
82                  new HTTPSPDYServerConnectionFactory(2, https_config);
83  
84          HTTPSPDYServerConnectionFactory spdy3 = 
85                  new HTTPSPDYServerConnectionFactory(3, https_config, 
86                          new ReferrerPushStrategy());
87  
88          // NPN Factory
89          SPDYServerConnectionFactory.checkProtocolNegotiationAvailable();
90          NPNServerConnectionFactory npn = new NPNServerConnectionFactory(
91                  spdy3.getProtocol(), 
92                  spdy2.getProtocol(),
93                  http.getDefaultProtocol());
94          npn.setDefaultProtocol(http.getDefaultProtocol());
95  
96          // SSL Factory
97          SslConnectionFactory ssl = new SslConnectionFactory(
98                  sslContextFactory, npn.getProtocol());
99  
100         // SPDY Connector
101         ServerConnector spdyConnector = new ServerConnector(server, ssl, 
102                 npn, spdy3, spdy2, 
103                 new HttpConnectionFactory(https_config));
104         spdyConnector.setPort(8443);
105         server.addConnector(spdyConnector);
106         
107         // Set a handler
108         server.setHandler(new HelloHandler());
109 
110         // Start the server
111         server.start();
112         server.join();
113     }
114 }