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.Connector;
22  import org.eclipse.jetty.server.HttpConfiguration;
23  import org.eclipse.jetty.server.HttpConnectionFactory;
24  import org.eclipse.jetty.server.SecureRequestCustomizer;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.ServerConnector;
27  import org.eclipse.jetty.server.SslConnectionFactory;
28  import org.eclipse.jetty.util.ssl.SslContextFactory;
29  
30  /* ------------------------------------------------------------ */
31  /**
32   * A Jetty server with multiple connectors.
33   */
34  public class ManyConnectors
35  {
36      public static void main(String[] args) throws Exception
37      {
38          String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution");
39          System.setProperty("jetty.home", jetty_home);
40  
41          // The Server
42          Server server = new Server();
43  
44          // HTTP Configuration
45          HttpConfiguration http_config = new HttpConfiguration();
46          http_config.setSecureScheme("https");
47          http_config.setSecurePort(8443);
48          http_config.setOutputBufferSize(32768);
49  
50          // HTTP connector
51          ServerConnector http = new ServerConnector(server,new HttpConnectionFactory(http_config));        
52          http.setPort(8080);
53          http.setIdleTimeout(30000);
54          
55          // SSL Context Factory for HTTPS and SPDY
56          SslContextFactory sslContextFactory = new SslContextFactory();
57          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
58          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
59          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
60  
61          // HTTPS Configuration
62          HttpConfiguration https_config = new HttpConfiguration(http_config);
63          https_config.addCustomizer(new SecureRequestCustomizer());
64  
65          // HTTPS connector
66          ServerConnector https = new ServerConnector(server,
67              new SslConnectionFactory(sslContextFactory,"http/1.1"),
68              new HttpConnectionFactory(https_config));
69          https.setPort(8443);
70  
71          // Set the connectors
72          server.setConnectors(new Connector[] { http, https });
73  
74          // Set a handler
75          server.setHandler(new HelloHandler());
76  
77          // Start the server
78          server.start();
79          server.join();
80      }
81  }