View Javadoc

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 java.io.File;
22  import java.io.FileNotFoundException;
23  import java.lang.management.ManagementFactory;
24  
25  import org.eclipse.jetty.deploy.DeploymentManager;
26  import org.eclipse.jetty.deploy.providers.WebAppProvider;
27  import org.eclipse.jetty.jmx.MBeanContainer;
28  import org.eclipse.jetty.security.HashLoginService;
29  import org.eclipse.jetty.server.AsyncNCSARequestLog;
30  import org.eclipse.jetty.server.ForwardedRequestCustomizer;
31  import org.eclipse.jetty.server.Handler;
32  import org.eclipse.jetty.server.HttpConfiguration;
33  import org.eclipse.jetty.server.HttpConnectionFactory;
34  import org.eclipse.jetty.server.NCSARequestLog;
35  import org.eclipse.jetty.server.SecureRequestCustomizer;
36  import org.eclipse.jetty.server.Server;
37  import org.eclipse.jetty.server.ServerConnector;
38  import org.eclipse.jetty.server.SslConnectionFactory;
39  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
40  import org.eclipse.jetty.server.handler.DefaultHandler;
41  import org.eclipse.jetty.server.handler.HandlerCollection;
42  import org.eclipse.jetty.server.handler.RequestLogHandler;
43  import org.eclipse.jetty.server.handler.StatisticsHandler;
44  import org.eclipse.jetty.spdy.server.NPNServerConnectionFactory;
45  import org.eclipse.jetty.spdy.server.SPDYServerConnectionFactory;
46  import org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory;
47  import org.eclipse.jetty.spdy.server.http.PushStrategy;
48  import org.eclipse.jetty.spdy.server.http.ReferrerPushStrategy;
49  import org.eclipse.jetty.util.ssl.SslContextFactory;
50  import org.eclipse.jetty.util.thread.QueuedThreadPool;
51  
52  public class SpdyServer
53  {
54      public static void main( String[] args ) throws Exception
55      {
56          // Path to as-built jetty-distribution directory
57          String jettyHomeBuild = "../../jetty-distribution/target/distribution";
58  
59          // Find jetty home directories
60          String homePath = System.getProperty("jetty.home", jettyHomeBuild);
61          File homeDir = new File(homePath);
62          if (!homeDir.exists())
63          {
64              throw new FileNotFoundException(homeDir.getAbsolutePath());
65          }
66          String jetty_home = homeDir.getAbsolutePath();
67          System.setProperty("jetty.home", jetty_home);
68  
69          // Setup Threadpool
70          QueuedThreadPool threadPool = new QueuedThreadPool(512);
71  
72          // Setup Jetty Server instance
73          Server server = new Server(threadPool);
74          server.manage(threadPool);
75          server.setDumpAfterStart(false);
76          server.setDumpBeforeStop(false);
77  
78          // Setup JMX
79          MBeanContainer mbContainer = new MBeanContainer(
80                  ManagementFactory.getPlatformMBeanServer());
81          server.addBean(mbContainer);
82  
83          // Common HTTP configuration
84          HttpConfiguration config = new HttpConfiguration();
85          config.setSecurePort(8443);
86          config.addCustomizer(new ForwardedRequestCustomizer());
87          config.addCustomizer(new SecureRequestCustomizer());
88          config.setSendServerVersion(true);
89  
90          // Http Connector Setup
91  
92          // A plain HTTP connector listening on port 8080. Note that it's also
93          // possible to have port 8080 configured as a non SSL SPDY connector.
94          // But the specification and most browsers do not allow to use SPDY
95          // without SSL encryption. However some browsers allow it to be
96          // configured.
97          HttpConnectionFactory http = new HttpConnectionFactory(config);
98          ServerConnector httpConnector = new ServerConnector(server, http);
99          httpConnector.setPort(8080);
100         httpConnector.setIdleTimeout(10000);
101         server.addConnector(httpConnector);
102 
103         // SSL configurations
104 
105         // We need a SSLContextFactory for the SSL encryption. That
106         // SSLContextFactory will be used by the SPDY
107         // connector.
108         SslContextFactory sslContextFactory = new SslContextFactory();
109         sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
110         sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
111         sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
112         sslContextFactory.setTrustStorePath(jetty_home + "/etc/keystore");
113         sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
114         sslContextFactory.setExcludeCipherSuites(
115                 "SSL_RSA_WITH_DES_CBC_SHA",
116                 "SSL_DHE_RSA_WITH_DES_CBC_SHA", 
117                 "SSL_DHE_DSS_WITH_DES_CBC_SHA",
118                 "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
119                 "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
120                 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
121                 "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
122 
123         // Spdy Connector
124 
125         // Make sure that the required NPN implementations are available.
126         SPDYServerConnectionFactory.checkProtocolNegotiationAvailable();
127 
128         // A ReferrerPushStrategy is being initialized.
129         // See:
130         // http://www.eclipse.org/jetty/documentation/current/spdy-configuring-push.html
131         // for more details.
132         PushStrategy push = new ReferrerPushStrategy();
133         HTTPSPDYServerConnectionFactory spdy2 = 
134                 new HTTPSPDYServerConnectionFactory(2, config, push);
135         spdy2.setInputBufferSize(8192);
136         spdy2.setInitialWindowSize(32768);
137 
138         // We need a connection factory per protocol that our server is supposed
139         // to support on the NPN port. We then
140         // create a ServerConnector and pass in the supported factories. NPN
141         // will then be used to negotiate the
142         // protocol with the client.
143         HTTPSPDYServerConnectionFactory spdy3 = 
144                 new HTTPSPDYServerConnectionFactory(3, config, push);
145         spdy3.setInputBufferSize(8192);
146 
147         NPNServerConnectionFactory npn = new NPNServerConnectionFactory(
148                 spdy3.getProtocol(), spdy2.getProtocol(), http.getProtocol());
149         npn.setDefaultProtocol(http.getProtocol());
150         npn.setInputBufferSize(1024);
151 
152         SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory,
153                 npn.getProtocol());
154 
155         // Setup the npn connector on port 8443
156         ServerConnector spdyConnector = new ServerConnector(server, ssl, 
157                 npn, spdy3, spdy2, http);
158         spdyConnector.setPort(8443);
159 
160         server.addConnector(spdyConnector);
161 
162         // The following section adds some handlers, deployers and webapp
163         // providers. See
164         // http://www.eclipse.org/jetty/documentation/current/advanced-embedding.html
165         // for details.
166 
167         // Setup handlers
168         HandlerCollection handlers = new HandlerCollection();
169         ContextHandlerCollection contexts = new ContextHandlerCollection();
170         RequestLogHandler requestLogHandler = new RequestLogHandler();
171 
172         handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(),
173                 requestLogHandler });
174 
175         StatisticsHandler stats = new StatisticsHandler();
176         stats.setHandler(handlers);
177 
178         server.setHandler(stats);
179 
180         // Setup deployers
181         DeploymentManager deployer = new DeploymentManager();
182         deployer.setContexts(contexts);
183         server.addBean(deployer);
184 
185         WebAppProvider webapp_provider = new WebAppProvider();
186         webapp_provider.setMonitoredDirName(jetty_home + "/webapps");
187         webapp_provider.setParentLoaderPriority(false);
188         webapp_provider.setExtractWars(true);
189         webapp_provider.setScanInterval(2);
190         webapp_provider.setDefaultsDescriptor(jetty_home
191                 + "/etc/webdefault.xml");
192         deployer.addAppProvider(webapp_provider);
193 
194         HashLoginService login = new HashLoginService();
195         login.setName("Test Realm");
196         login.setConfig(jetty_home + "/etc/realm.properties");
197         server.addBean(login);
198 
199         NCSARequestLog requestLog = new AsyncNCSARequestLog();
200         requestLog.setFilename(jetty_home + "/logs/jetty-yyyy_mm_dd.log");
201         requestLog.setExtended(false);
202         requestLogHandler.setRequestLog(requestLog);
203 
204         server.setStopAtShutdown(true);
205 
206         server.start();
207         server.dumpStdErr();
208         server.join();
209     }
210 }