@@ -66,6 +66,7 @@ public class ServerDaemon implements Daemon {
6666 private static final String BIND_INTERFACE = "bind.interface" ;
6767 private static final String CONTEXT_PATH = "context.path" ;
6868 private static final String SESSION_TIMEOUT = "session.timeout" ;
69+ private static final String HTTP_ENABLE = "http.enable" ;
6970 private static final String HTTP_PORT = "http.port" ;
7071 private static final String HTTPS_ENABLE = "https.enable" ;
7172 private static final String HTTPS_PORT = "https.port" ;
@@ -80,6 +81,7 @@ public class ServerDaemon implements Daemon {
8081
8182 private Server server ;
8283
84+ private boolean httpEnable = true ;
8385 private int httpPort = 8080 ;
8486 private int httpsPort = 8443 ;
8587 private int sessionTimeout = 30 ;
@@ -105,8 +107,8 @@ public static void main(final String... anArgs) throws Exception {
105107 public void init (final DaemonContext context ) {
106108 final File confFile = PropertiesUtil .findConfigFile ("server.properties" );
107109 if (confFile == null ) {
108- LOG .warn (String .format ("Server configuration file not found. Initializing server daemon on %s: %s, with https.enabled =%s, https.port=%s, context.path=%s" ,
109- bindInterface , httpPort , httpsEnable , httpsPort , contextPath ));
110+ LOG .warn (String .format ("Server configuration file not found. Initializing server daemon on %s, with http.enable= %s, http.port=%s, https.enable =%s, https.port=%s, context.path=%s" ,
111+ bindInterface , httpEnable , httpPort , httpsEnable , httpsPort , contextPath ));
110112 return ;
111113 }
112114
@@ -119,6 +121,7 @@ public void init(final DaemonContext context) {
119121 }
120122 setBindInterface (properties .getProperty (BIND_INTERFACE , "" ));
121123 setContextPath (properties .getProperty (CONTEXT_PATH , "/client" ));
124+ setHttpEnable (Boolean .valueOf (properties .getProperty (HTTP_ENABLE , "true" )));
122125 setHttpPort (Integer .valueOf (properties .getProperty (HTTP_PORT , "8080" )));
123126 setHttpsEnable (Boolean .valueOf (properties .getProperty (HTTPS_ENABLE , "false" )));
124127 setHttpsPort (Integer .valueOf (properties .getProperty (HTTPS_PORT , "8443" )));
@@ -129,9 +132,15 @@ public void init(final DaemonContext context) {
129132 setSessionTimeout (Integer .valueOf (properties .getProperty (SESSION_TIMEOUT , "30" )));
130133 } catch (final IOException e ) {
131134 LOG .warn ("Failed to load configuration from server.properties file" , e );
135+ } finally {
136+ // make sure that at least HTTP is enabled if both of them are set to false (misconfiguration)
137+ if (!httpEnable && !httpsEnable ) {
138+ setHttpEnable (true );
139+ LOG .warn ("Server configuration malformed, neither http nor https is enabled, http will be enabled." );
140+ }
132141 }
133- LOG .info (String .format ("Initializing server daemon on %s: %s, with https.enabled =%s, https.port=%s, context.path=%s" ,
134- bindInterface , httpPort , httpsEnable , httpsPort , contextPath ));
142+ LOG .info (String .format ("Initializing server daemon on %s, with http.enable= %s, http.port=%s, https.enable =%s, https.port=%s, context.path=%s" ,
143+ bindInterface , httpEnable , httpPort , httpsEnable , httpsPort , contextPath ));
135144 }
136145
137146 @ Override
@@ -163,22 +172,51 @@ public void start() throws Exception {
163172 httpConfig .setSendDateHeader (false );
164173
165174 // HTTP Connector
166- final ServerConnector httpConnector = new ServerConnector (server , new HttpConnectionFactory (httpConfig ));
167- httpConnector .setPort (httpPort );
168- httpConnector .setHost (bindInterface );
169- httpConnector .setIdleTimeout (30000 );
170- server .addConnector (httpConnector );
175+ createHttpConnector (httpConfig );
171176
172177 // Setup handlers
173178 server .setHandler (createHandlers ());
174179
175180 // Extra config options
176181 server .setStopAtShutdown (true );
177182
183+ // HTTPS Connector
184+ createHttpsConnector (httpConfig );
185+
186+ server .start ();
187+ server .join ();
188+ }
189+
190+ @ Override
191+ public void stop () throws Exception {
192+ server .stop ();
193+ }
194+
195+ @ Override
196+ public void destroy () {
197+ server .destroy ();
198+ }
199+
200+ ///////////////////////////////////////////////////
201+ /////////////// Private methods ///////////////////
202+ ///////////////////////////////////////////////////
203+
204+ private void createHttpConnector (final HttpConfiguration httpConfig ) {
205+ if (httpEnable ) {
206+ final ServerConnector httpConnector = new ServerConnector (server , new HttpConnectionFactory (httpConfig ));
207+ httpConnector .setPort (httpPort );
208+ httpConnector .setHost (bindInterface );
209+ httpConnector .setIdleTimeout (30000 );
210+ server .addConnector (httpConnector );
211+ }
212+ }
213+
214+ private void createHttpsConnector (final HttpConfiguration httpConfig ) {
178215 // Configure SSL
179216 if (httpsEnable && !Strings .isNullOrEmpty (keystoreFile ) && new File (keystoreFile ).exists ()) {
180217 // SSL Context
181218 final SslContextFactory sslContextFactory = new SslContextFactory ();
219+
182220 // Define keystore path and passwords
183221 sslContextFactory .setKeyStorePath (keystoreFile );
184222 sslContextFactory .setKeyStorePassword (keystorePassword );
@@ -188,33 +226,16 @@ public void start() throws Exception {
188226 final HttpConfiguration httpsConfig = new HttpConfiguration (httpConfig );
189227 httpsConfig .addCustomizer (new SecureRequestCustomizer ());
190228
191- // HTTPS connector
229+ // HTTPS Connector
192230 final ServerConnector sslConnector = new ServerConnector (server ,
193231 new SslConnectionFactory (sslContextFactory , "http/1.1" ),
194232 new HttpConnectionFactory (httpsConfig ));
195233 sslConnector .setPort (httpsPort );
196234 sslConnector .setHost (bindInterface );
197235 server .addConnector (sslConnector );
198236 }
199-
200- server .start ();
201- server .join ();
202237 }
203238
204- @ Override
205- public void stop () throws Exception {
206- server .stop ();
207- }
208-
209- @ Override
210- public void destroy () {
211- server .destroy ();
212- }
213-
214- ///////////////////////////////////////////////////
215- /////////////// Private methods ///////////////////
216- ///////////////////////////////////////////////////
217-
218239 private HandlerCollection createHandlers () {
219240 final WebAppContext webApp = new WebAppContext ();
220241 webApp .setContextPath (contextPath );
@@ -283,6 +304,10 @@ public void setHttpPort(int httpPort) {
283304 this .httpPort = httpPort ;
284305 }
285306
307+ public void setHttpEnable (boolean httpEnable ) {
308+ this .httpEnable = httpEnable ;
309+ }
310+
286311 public void setHttpsPort (int httpsPort ) {
287312 this .httpsPort = httpsPort ;
288313 }
0 commit comments