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