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