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