2222import java .io .IOException ;
2323import java .lang .management .ManagementFactory ;
2424import java .net .URL ;
25- import java .util .EnumSet ;
26- import java .util .HashMap ;
27- import java .util .Map ;
2825import java .util .Properties ;
2926
30- import javax .servlet .DispatcherType ;
31-
3227import org .apache .commons .daemon .Daemon ;
3328import org .apache .commons .daemon .DaemonContext ;
3429import org .eclipse .jetty .jmx .MBeanContainer ;
4237import org .eclipse .jetty .server .ServerConnector ;
4338import org .eclipse .jetty .server .SslConnectionFactory ;
4439import org .eclipse .jetty .server .handler .HandlerCollection ;
40+ import org .eclipse .jetty .server .handler .MovedContextHandler ;
4541import org .eclipse .jetty .server .handler .RequestLogHandler ;
46- import org .eclipse .jetty .servlet .FilterHolder ;
47- import org .eclipse .jetty .servlets .GzipFilter ;
42+ import org .eclipse .jetty .server .handler .gzip .GzipHandler ;
4843import org .eclipse .jetty .util .ssl .SslContextFactory ;
4944import org .eclipse .jetty .util .thread .QueuedThreadPool ;
5045import org .eclipse .jetty .util .thread .ScheduledExecutorScheduler ;
@@ -70,6 +65,7 @@ public class ServerDaemon implements Daemon {
7065
7166 private static final String BIND_INTERFACE = "bind.interface" ;
7267 private static final String CONTEXT_PATH = "context.path" ;
68+ private static final String SESSION_TIMEOUT = "session.timeout" ;
7369 private static final String HTTP_PORT = "http.port" ;
7470 private static final String HTTPS_ENABLE = "https.enable" ;
7571 private static final String HTTPS_PORT = "https.port" ;
@@ -86,6 +82,7 @@ public class ServerDaemon implements Daemon {
8682
8783 private int httpPort = 8080 ;
8884 private int httpsPort = 8443 ;
85+ private int sessionTimeout = 30 ;
8986 private boolean httpsEnable = false ;
9087 private String accessLogFile = "access.log" ;
9188 private String bindInterface = "" ;
@@ -129,6 +126,7 @@ public void init(final DaemonContext context) {
129126 setKeystorePassword (properties .getProperty (KEYSTORE_PASSWORD ));
130127 setWebAppLocation (properties .getProperty (WEBAPP_DIR ));
131128 setAccessLogFile (properties .getProperty (ACCESS_LOG , "access.log" ));
129+ setSessionTimeout (Integer .valueOf (properties .getProperty (SESSION_TIMEOUT , "30" )));
132130 } catch (final IOException e ) {
133131 LOG .warn ("Failed to load configuration from server.properties file" , e );
134132 }
@@ -221,28 +219,33 @@ private HandlerCollection createHandlers() {
221219 final WebAppContext webApp = new WebAppContext ();
222220 webApp .setContextPath (contextPath );
223221 webApp .setInitParameter ("org.eclipse.jetty.servlet.Default.dirAllowed" , "false" );
222+ webApp .getSessionHandler ().setMaxInactiveInterval (sessionTimeout * 60 );
224223
225- final FilterHolder filter = webApp . addFilter ( GzipFilter . class , "/*" , EnumSet . of ( DispatcherType . REQUEST ));
226- final Map < String , String > params = new HashMap <> ();
227- params . put ( "mimeTypes " , "text/html,text/ xml, text/css, text/plain, text/javascript, application/javascript, application/json, application/xml" );
228- params . put ( "methods " , "GET, POST" );
229- params . put ( "deflateCompressionLevel" , "9" );
230- filter . setInitParameters ( params );
224+ // GZIP handler
225+ final GzipHandler gzipHandler = new GzipHandler ();
226+ gzipHandler . addIncludedMimeTypes ( "text/html " , "text/xml" , " text/css" , " text/plain" , " text/javascript" , " application/javascript" , " application/json" , " application/xml" );
227+ gzipHandler . setIncludedMethods ( "GET " , "POST" );
228+ gzipHandler . setCompressionLevel ( 9 );
229+ gzipHandler . setHandler ( webApp );
231230
232231 if (Strings .isNullOrEmpty (webAppLocation )) {
233232 webApp .setWar (getShadedWarUrl ());
234233 } else {
235234 webApp .setWar (webAppLocation );
236235 }
237236
237+ // Request log handler
238238 final RequestLogHandler log = new RequestLogHandler ();
239239 log .setRequestLog (createRequestLog ());
240240
241- final HandlerCollection handlerCollection = new HandlerCollection ();
242- handlerCollection .addHandler (log );
243- handlerCollection .addHandler (webApp );
241+ // Redirect root context handler
242+ MovedContextHandler rootRedirect = new MovedContextHandler ();
243+ rootRedirect .setContextPath ("/" );
244+ rootRedirect .setNewContextURL (contextPath );
245+ rootRedirect .setPermanent (true );
244246
245- return handlerCollection ;
247+ // Put rootRedirect at the end!
248+ return new HandlerCollection (log , gzipHandler , rootRedirect );
246249 }
247250
248251 private RequestLog createRequestLog () {
@@ -307,4 +310,8 @@ public void setAccessLogFile(String accessLogFile) {
307310 public void setWebAppLocation (String webAppLocation ) {
308311 this .webAppLocation = webAppLocation ;
309312 }
313+
314+ public void setSessionTimeout (int sessionTimeout ) {
315+ this .sessionTimeout = sessionTimeout ;
316+ }
310317}
0 commit comments