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.Connector;
25  import org.eclipse.jetty.server.HttpConfiguration;
26  import org.eclipse.jetty.server.HttpConnectionFactory;
27  import org.eclipse.jetty.server.SecureRequestCustomizer;
28  import org.eclipse.jetty.server.Server;
29  import org.eclipse.jetty.server.ServerConnector;
30  import org.eclipse.jetty.server.SslConnectionFactory;
31  import org.eclipse.jetty.util.ssl.SslContextFactory;
32  
33  /**
34   * A Jetty server with multiple connectors.
35   */
36  public class ManyConnectors
37  {
38      public static void main( String[] args ) throws Exception
39      {
40          // Since this example shows off SSL configuration, we need a keystore
41          // with the appropriate key. These lookup of jetty.home is purely a hack
42          // to get access to a keystore that we use in many unit tests and should
43          // probably be a direct path to your own keystore.
44  
45          String jettyDistKeystore = "../../jetty-distribution/target/distribution/etc/keystore";
46          String keystorePath = System.getProperty(
47                  "example.keystore", jettyDistKeystore);
48          File keystoreFile = new File(keystorePath);
49          if (!keystoreFile.exists())
50          {
51              throw new FileNotFoundException(keystoreFile.getAbsolutePath());
52          }
53  
54          // Create a basic jetty server object without declaring the port. Since
55          // we are configuring connectors directly we'll be setting ports on
56          // those connectors.
57          Server server = new Server();
58  
59          // HTTP Configuration
60          // HttpConfiguration is a collection of configuration information
61          // appropriate for http and https. The default scheme for http is
62          // <code>http</code> of course, as the default for secured http is
63          // <code>https</code> but we show setting the scheme to show it can be
64          // done. The port for secured communication is also set here.
65          HttpConfiguration http_config = new HttpConfiguration();
66          http_config.setSecureScheme("https");
67          http_config.setSecurePort(8443);
68          http_config.setOutputBufferSize(32768);
69  
70          // HTTP connector
71          // The first server connector we create is the one for http, passing in
72          // the http configuration we configured above so it can get things like
73          // the output buffer size, etc. We also set the port (8080) and
74          // configure an idle timeout.
75          ServerConnector http = new ServerConnector(server,
76                  new HttpConnectionFactory(http_config));
77          http.setPort(8080);
78          http.setIdleTimeout(30000);
79  
80          // SSL Context Factory for HTTPS and SPDY
81          // SSL requires a certificate so we configure a factory for ssl contents
82          // with information pointing to what keystore the ssl connection needs
83          // to know about. Much more configuration is available the ssl context,
84          // including things like choosing the particular certificate out of a
85          // keystore to be used.
86          SslContextFactory sslContextFactory = new SslContextFactory();
87          sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
88          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
89          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
90  
91          // HTTPS Configuration
92          // A new HttpConfiguration object is needed for the next connector and
93          // you can pass the old one as an argument to effectively clone the
94          // contents. On this HttpConfiguration object we add a
95          // SecureRequestCustomizer which is how a new connector is able to
96          // resolve the https connection before handing control over to the Jetty
97          // Server.
98          HttpConfiguration https_config = new HttpConfiguration(http_config);
99          https_config.addCustomizer(new SecureRequestCustomizer());
100 
101         // HTTPS connector
102         // We create a second ServerConnector, passing in the http configuration
103         // we just made along with the previously created ssl context factory.
104         // Next we set the port and a longer idle timeout.
105         ServerConnector https = new ServerConnector(server,
106                 new SslConnectionFactory(sslContextFactory, "http/1.1"),
107                 new HttpConnectionFactory(https_config));
108         https.setPort(8443);
109         https.setIdleTimeout(500000);
110 
111         // Here you see the server having multiple connectors registered with
112         // it, now requests can flow into the server from both http and https
113         // urls to their respective ports and be processed accordingly by jetty.
114         // A simple handler is also registered with the server so the example
115         // has something to pass requests off to.
116 
117         // Set the connectors
118         server.setConnectors(new Connector[] { http, https });
119 
120         // Set a handler
121         server.setHandler(new HelloHandler());
122 
123         // Start the server
124         server.start();
125         server.join();
126     }
127 }