/** * Configures the embedded jetty server. The values are configured in * <b>application.yaml</b> file. * * @param port jetty server port * @param maxThreads thread pool min thread * @param minThreads thread pool max thread * @param idleTimeout maximum thread idle time * @param jmxEnabled true, if jetty jmx is enabled. * @return {@link JettyEmbeddedServletContainerFactory} */ @Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(@Value("${server.port:8443}") final int port, @Value("${jetty.thread-pool.max-threads:200}") final int maxThreads, @Value("${jetty.thread-pool.min-threads:8}") final int minThreads, @Value("${jetty.thread-pool.idle-timeout:60000}") final int idleTimeout, @Value("${jetty.jmx.enabled:true}") final boolean jmxEnabled) { log.info("Configuring Jetty server."); final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port); factory.addServerCustomizers(server -> { final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMinThreads(minThreads); threadPool.setMaxThreads(maxThreads); threadPool.setIdleTimeout(idleTimeout); log.info("Server thread pool config: " + server.getThreadPool()); // Jetty JMX config. if (jmxEnabled) { log.info("Exposing Jetty managed beans to the JMX platform server."); server.addBean(new MBeanContainer(ManagementFactory.getPlatformMBeanServer())); } }); return factory; }
@Bean public EmbeddedServletContainerFactory jettyConfigBean() { DynamicPropertyFactory propertyFactory = DynamicPropertyFactory.getInstance(); String accessLogFilePath = propertyFactory.getStringProperty("server.accessLog.config.file", ACCESS_LOG_FILE_PATH).getValue(); JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory(); jettyEmbeddedServletContainerFactory.addServerCustomizers(new org.springframework.boot.context.embedded.jetty.JettyServerCustomizer() { public void customize(Server server) { HandlerCollection handlers = new HandlerCollection(); for (Handler handler : server.getHandlers()) { handlers.addHandler(handler); } RequestLogHandler requestLogHandler = new RequestLogHandler(); RequestLogImpl requestLogImpl = new RequestLogImpl(); requestLogImpl.setFileName(accessLogFilePath); requestLogHandler.setRequestLog(requestLogImpl); handlers.addHandler(requestLogHandler); server.setHandler(handlers); LOGGER.info("Jetty Server Customized. Access Log Configuration File - {}", accessLogFilePath); } }); return jettyEmbeddedServletContainerFactory; }
@Bean @Autowired public EmbeddedServletContainerCustomizer servletContainerCustomizer(ServerProperties properties) { return container -> { if (container instanceof JettyEmbeddedServletContainerFactory) { JettyEmbeddedServletContainerFactory jettyContainer = (JettyEmbeddedServletContainerFactory) container; jettyContainer.addConfigurations(new AbstractConfiguration() { @Override public void configure(WebAppContext context) throws Exception { properties.getContextParameters().forEach((k, v) -> context.setInitParameter(k, v)); } }); } }; }
void customizeJetty(final ServerProperties serverProperties, JettyEmbeddedServletContainerFactory factory) { factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders()); if (this.acceptors != null) { factory.setAcceptors(this.acceptors); } if (this.selectors != null) { factory.setSelectors(this.selectors); } if (serverProperties.getMaxHttpHeaderSize() > 0) { customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize()); } if (serverProperties.getMaxHttpPostSize() > 0) { customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize()); } if (serverProperties.getConnectionTimeout() != null) { customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout()); } }
private void customizeConnectionTimeout( JettyEmbeddedServletContainerFactory factory, final int connectionTimeout) { factory.addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { for (org.eclipse.jetty.server.Connector connector : server .getConnectors()) { if (connector instanceof AbstractConnector) { ((AbstractConnector) connector) .setIdleTimeout(connectionTimeout); } } } }); }
void customizeJetty(ServerProperties serverProperties, JettyEmbeddedServletContainerFactory factory) { factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders()); if (this.acceptors != null) { factory.setAcceptors(this.acceptors); } if (this.selectors != null) { factory.setSelectors(this.selectors); } if (serverProperties.getMaxHttpHeaderSize() > 0) { customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize()); } if (serverProperties.getMaxHttpPostSize() > 0) { customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize()); } }
@Bean public EmbeddedServletContainerCustomizer disableServerHeader() { return container -> { if (container instanceof JettyEmbeddedServletContainerFactory) { ((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector) { HttpConnectionFactory connectionFactory = connector.getConnectionFactory(HttpConnectionFactory.class); connectionFactory.getHttpConfiguration().setSendServerVersion(false); } } } }); } }; }
@Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory( @Value("${server.port:8080}") final String port, @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads, @Value("${jetty.threadPool.minThreads:8}") final String minThreads, @Value("${jetty.threadPool.idleTimeout:60000}") final String idleTimeout) { final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(Integer.valueOf(port)); factory.addServerCustomizers((JettyServerCustomizer) server -> { final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMaxThreads(Integer.valueOf(maxThreads)); threadPool.setMinThreads(Integer.valueOf(minThreads)); threadPool.setIdleTimeout(Integer.valueOf(idleTimeout)); final GzipHandler gzipHandler = new GzipHandler(); gzipHandler.setHandler(server.getHandler()); gzipHandler.setSyncFlush(true); server.setHandler(gzipHandler); }); return factory; }
private void addStatisticHandler(JettyEmbeddedServletContainerFactory jetty) { jetty.addServerCustomizers(server -> { HandlerCollection handlerCollection = new HandlerCollection(server.getHandlers()); statisticsHandler.setHandler(handlerCollection); server.setHandler(statisticsHandler); }); }
@Override void customizeJetty(JettyEmbeddedServletContainerFactory container) { container.addServerCustomizers(server -> { RequestLogHandler requestLogsHandler = new RequestLogHandler(); requestLogsHandler.setServer(server); Slf4jRequestLog log = new Slf4jRequestLog(); requestLogsHandler.setRequestLog(log); HandlerCollection handlers = new HandlerCollection(server.getHandlers()); handlers.prependHandler(requestLogsHandler); server.setHandler(handlers); }); }
@Bean public JettyCustomizer jettyCustomizer() { return new JettyCustomizer() { @Override void customizeJetty(JettyEmbeddedServletContainerFactory container) { String serverName = "jetty"; container.setServerHeader(serverName); container.setDisplayName(serverName); container.setUseForwardHeaders(true); } }; }
@Override void customizeJetty(JettyEmbeddedServletContainerFactory container) { container.addServerCustomizers(server -> { MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); server.addEventListener(mbContainer); server.addBean(mbContainer); }); }
/** * @return Jetty config bean */ @Bean public EmbeddedServletContainerFactory jettyConfigBean() { JettyEmbeddedServletContainerFactory jef = new JettyEmbeddedServletContainerFactory(); jef.setPort(getServerPort()); logger.info("Listening on port {}...", jef.getPort()); return jef; }
@Bean public EmbeddedServletContainerFactory containerFactory() { JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory( 0); factory.setContextPath(this.env.getProperty("context")); return factory; }
@Override protected void doCustomize(JettyEmbeddedServletContainerFactory container) { container.addConfigurations(new AbstractConfiguration() { @Override public void configure(WebAppContext context) throws Exception { ServerContainer serverContainer = WebSocketServerContainerInitializer .configureContext(context); ShutdownThread.deregister(serverContainer); } }); }
@Test public void defaultUseForwardHeadersJetty() throws Exception { JettyEmbeddedServletContainerFactory container = spy( new JettyEmbeddedServletContainerFactory()); this.properties.customize(container); verify(container).setUseForwardHeaders(false); }
@Test public void setUseForwardHeadersJetty() throws Exception { this.properties.setUseForwardHeaders(true); JettyEmbeddedServletContainerFactory container = spy( new JettyEmbeddedServletContainerFactory()); this.properties.customize(container); verify(container).setUseForwardHeaders(true); }
@Test public void deduceUseForwardHeadersJetty() throws Exception { this.properties.setEnvironment(new MockEnvironment().withProperty("DYNO", "-")); JettyEmbeddedServletContainerFactory container = spy( new JettyEmbeddedServletContainerFactory()); this.properties.customize(container); verify(container).setUseForwardHeaders(true); }
@Test public void sessionStoreDir() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("server.session.store-dir", "myfolder"); bindProperties(map); JettyEmbeddedServletContainerFactory container = spy( new JettyEmbeddedServletContainerFactory()); this.properties.customize(container); verify(container).setSessionStoreDir(new File("myfolder")); }
@Primary @Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(MetricRegistry metricRegistry, JettyThreadPoolSettings jettyThreadPoolSettings, JettyLowResourceMonitorSettings jettyLowResourceMonitorSettings, @Value("${server.port:8080}") final int port) { final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port); factory.addServerCustomizers( new InstrumentedJettyServer(metricRegistry, jettyThreadPoolSettings, jettyLowResourceMonitorSettings)); return factory; }
@Bean public EmbeddedServletContainerFactory servletContainer() { JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); factory.addServerCustomizers(new JettyServerCustomizer() { public void customize(final Server server) { // Tweak the connection pool used by Jetty to handle incoming // HTTP connections Integer localServerMaxThreads = deriveValue(serverMaxThreads, DEFAULT_SERVER_MAX_THREADS); Integer localServerMinThreads = deriveValue(serverMinThreads, DEFAULT_SERVER_MIN_THREADS); Integer localServerIdleTimeout = deriveValue(serverIdleTimeout, DEFAULT_SERVER_IDLE_TIMEOUT); logger.info("Container Max Threads={}", localServerMaxThreads); logger.info("Container Min Threads={}", localServerMinThreads); logger.info("Container Idle Timeout={}", localServerIdleTimeout); final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMaxThreads(Integer.valueOf(localServerMaxThreads)); threadPool.setMinThreads(Integer.valueOf(localServerMinThreads)); threadPool.setIdleTimeout(Integer.valueOf(localServerIdleTimeout)); } }); return factory; }
@Bean EmbeddedServletContainerCustomizer containerCustomizer() throws Exception { return (ConfigurableEmbeddedServletContainer container) -> { JettyEmbeddedServletContainerFactory jetty = (JettyEmbeddedServletContainerFactory) container; jetty.setServerCustomizers(Collections.singleton(this)); }; }
@Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory( @Value("${server.port:8080}") final String port, // @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads, // @Value("${jetty.threadPool.minThreads:8}") final String minThreads, // @Value("${jetty.threadPool.idleTimeout:-1}") final String idleTimeout) { return new JettyContainerConfiguration(port, maxThreads, minThreads, idleTimeout); }
@Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(Environment environment) { return (container) -> { container.addInitializers(SERVLET_CONTEXT_INITIALIZER); //container.setRegisterJspServlet(false); Map<String, String> mimeMappings = new HashMap<>(); mimeMappings.put("eot", "application/vnd.ms-fontobject"); mimeMappings.put("otf", "font/opentype"); mimeMappings.put("ttf", "application/x-font-ttf"); mimeMappings.put("woff", "application/x-font-woff"); mimeMappings.put("svg", "image/svg+xml"); container.setSessionTimeout(2, TimeUnit.HOURS); container.setMimeMappings(new MimeMappings(mimeMappings)); if(!environment.acceptsProfiles("dev")) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404-not-found"), new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500-internal-server-error"), new ErrorPage("/session-expired")); } Optional.ofNullable(System.getProperty("alfio.worker.name")).ifPresent(workerName -> { ((JettyEmbeddedServletContainerFactory)container).addServerCustomizers(server -> { log.info("Configuring session manager using worker name {}", workerName); DefaultSessionIdManager sessionIdManager = new DefaultSessionIdManager(server); sessionIdManager.setWorkerName(workerName); server.setSessionIdManager(sessionIdManager); }); }); }; }
@Bean public EmbeddedServletContainerFactory servletContainer() { JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); // Port number that server will run. factory.setPort(8888); return factory; }
@Override void customizeJetty(JettyEmbeddedServletContainerFactory jetty) { addStatisticHandler(jetty); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { if (container instanceof JettyEmbeddedServletContainerFactory) { customizeJetty((JettyEmbeddedServletContainerFactory) container); } }