HttpsService extends AbstractIdleService { @Override protected void startUp() throws Exception { LOGGER.info("starting {}", serviceName()); if (server != null) { LOGGER.error("Server is already stopped"); return; } else { server = new Server(); } File keystoreFile = new File(keyStorePath); if (!keystoreFile.exists()) { throw new FileNotFoundException(keystoreFile.getAbsolutePath()); } HttpConfiguration httpConfiguration = new HttpConfiguration(); httpConfiguration.setSecureScheme("https"); httpConfiguration.setSecurePort(httpsPort); httpConfiguration.setOutputBufferSize(httpConfigOutputBufferSize); SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath()); sslContextFactory.setKeyStorePassword(sslKeyStorePassword); sslContextFactory.setKeyManagerPassword(sslKeyManagerPassword); HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration); httpsConfiguration.addCustomizer(new SecureRequestCustomizer()); ServerConnector https = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfiguration)); https.setPort(httpsPort); https.setIdleTimeout(httpsIdleTimeout); server.setConnectors(new Connector[]{https}); HandlerCollection handlers = new HandlerCollection(false); handlers.addHandler(makeMonitorHandler()); handlers.addHandler(makeContextHandler(guiceFilter)); server.setHandler(handlers); server.start(); LOGGER.info("started {}", serviceName()); } @Inject  HttpsService(
            @Named("application.https.port") int httpsPort,
            @Named("application.https.idletimeout") int httpsIdleTimeout,
            @Named("application.httpconfig.output.buffersize") int httpConfigOutputBufferSize,
            @Named("application.ssl.keystore.path") String keyStorePath,
            @Named("application.ssl.keystore.password") String sslKeyStorePassword,
            @Named("application.ssl.keymanager.password") String sslKeyManagerPassword,
            @Named("application.http.context.path") String contextPath,
            GuiceFilter guiceFilter);   }
@Test public void startable() throws Exception { Server server = createMockAndExpectNew(Server.class); setInternalState(server, "_lock", new Object()); setInternalState(server, "_state", 2); expect(server.getThreadPool()).andReturn(threadPool); expect(server.getBean(Scheduler.class)).andReturn(scheduler); expect(server.getBean(ByteBufferPool.class)).andReturn(byteBufferPool); server.setConnectors(isA(Connector[].class)); server.setHandler(isA(Handler.class)); server.start(); replay(server, Server.class); String path = "/foo/bar/bop.txt"; File file = createMockAndExpectNew(File.class, path); expect(file.exists()).andReturn(true); expect(file.getAbsolutePath()).andReturn(path); replay(file, File.class); HttpsService httpsService = new HttpsService(8443, 500000, 32768, path, "ThisIsATest", "ThisIsAnotherTest", "/", guiceFilter); httpsService.startUp(); verify(server, Server.class); verify(file, File.class); }
@Test public void startableWithEmptyContextPath() throws Exception { Server server = createMockAndExpectNew(Server.class); setInternalState(server, "_lock", new Object()); setInternalState(server, "_state", 2); expect(server.getThreadPool()).andReturn(threadPool); expect(server.getBean(Scheduler.class)).andReturn(scheduler); expect(server.getBean(ByteBufferPool.class)).andReturn(byteBufferPool); server.setConnectors(isA(Connector[].class)); server.setHandler(isA(Handler.class)); server.start(); replay(server, Server.class); String path = "/foo/bar/bop.txt"; File file = createMockAndExpectNew(File.class, path); expect(file.exists()).andReturn(true); expect(file.getAbsolutePath()).andReturn(path); replay(file, File.class); HttpsService httpsService = new HttpsService(8443, 500000, 32768, path, "ThisIsATest", "ThisIsAnotherTest", "", guiceFilter); httpsService.startUp(); verify(server, Server.class); verify(file, File.class); }
@Test public void notStartableAlreadyStarted() throws Exception { Server server = createMock(Server.class); String path = "/foo/bar/bop.txt"; HttpsService httpsService = new HttpsService(8443, 500000, 32768, path, "ThisIsATest", "ThisIsAnotherTest", "/", guiceFilter); setInternalState(httpsService, "server", server); replay(server, Server.class); httpsService.startUp(); verify(server, Server.class); }
@Test(expected = FileNotFoundException.class) public void notStartableKeyStoreDoesNotExist() throws Exception { Server server = createMockAndExpectNew(Server.class); setInternalState(server, "_lock", new Object()); setInternalState(server, "_state", 2); expect(server.getThreadPool()).andReturn(threadPool); expect(server.getBean(Scheduler.class)).andReturn(scheduler); expect(server.getBean(ByteBufferPool.class)).andReturn(byteBufferPool); server.setConnectors(isA(Connector[].class)); server.setHandler(isA(Handler.class)); server.start(); replay(server, Server.class); String path = "/foo/bar/bop.txt"; File file = createMockAndExpectNew(File.class, path); expect(file.exists()).andReturn(false); expect(file.getAbsolutePath()).andReturn(path); replay(file, File.class); HttpsService httpsService = new HttpsService(8443, 500000, 32768, path, "ThisIsATest", "ThisIsAnotherTest", "/", guiceFilter); httpsService.startUp(); verify(server, Server.class); verify(file, File.class); }