org.springframework.boot.web.embedded.jetty.JettyServerCustomizer Java Examples

The following examples show how to use org.springframework.boot.web.embedded.jetty.JettyServerCustomizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebContainerAccessLogAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer accessWebServerFactoryCustomizer() {
    return factory -> {
        if (factory instanceof JettyServletWebServerFactory) {
            ((JettyServletWebServerFactory) factory).addServerCustomizers((JettyServerCustomizer) server -> {
                HandlerCollection handlers = new HandlerCollection();
                for (Handler handler : server.getHandlers()) {
                    handlers.addHandler(handler);
                }
                RequestLogHandler reqLogs = new RequestLogHandler();
                Slf4jRequestLog requestLog = new Slf4jRequestLog();
                requestLog.setLoggerName("access-log");
                requestLog.setLogLatency(false);

                reqLogs.setRequestLog(requestLog);
                handlers.addHandler(reqLogs);
                server.setHandler(handlers);
            });
        }
    };
}
 
Example #2
Source File: DSSSpringApplication.java    From DataSphereStudio with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyFactoryCustomizer() {
    return new WebServerFactoryCustomizer<JettyServletWebServerFactory>() {
        public void customize(JettyServletWebServerFactory jettyServletWebServerFactory) {
            jettyServletWebServerFactory.addServerCustomizers(new JettyServerCustomizer() {
                public void customize(Server server) {
                    Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
                    final WebAppContext webApp = (WebAppContext) childHandlersByClass[0];
                    FilterHolder filterHolder = new FilterHolder(CharacterEncodingFilter.class);
                    filterHolder.setInitParameter("encoding", Configuration.BDP_ENCODING().getValue());
                    filterHolder.setInitParameter("forceEncoding", "true");
                    webApp.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
                    BDPJettyServerHelper.setupRestApiContextHandler(webApp);
                    if(ServerConfiguration.BDP_SERVER_SOCKET_MODE().getValue()) {
                        BDPJettyServerHelper.setupControllerServer(webApp);
                    }
                    if(!ServerConfiguration.BDP_SERVER_DISTINCT_MODE().getValue()) {
                        BDPJettyServerHelper.setupWebAppContext(webApp);
                    }
                }
            });
        }
    };
}
 
Example #3
Source File: JettyAutoConfiguration.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jsfJettyFactoryCustomizer() {
	return factory -> factory.addServerCustomizers(new JettyServerCustomizer() {
		@Override
		@SneakyThrows(IOException.class)
		public void customize(Server server) {
			Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
			final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];

			String classPathResourceString = JettyAutoConfiguration.this.jettyProperties.getClassPathResource();

			webAppContext.setBaseResource(new ResourceCollection(
				Resource.newResource(new ClassPathResource(classPathResourceString).getURI()),
				webAppContext.getBaseResource()));

			log.info("Setting Jetty classLoader to {} directory", classPathResourceString);
		}
	});
}
 
Example #4
Source File: LogSearchWebServerCustomizer.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(JettyServletWebServerFactory webServerFactory) {
  serverProperties.getServlet().getSession().setTimeout(Duration.ofMinutes(logSearchHttpConfig.getSessionTimeout()));
  serverProperties.getServlet().getSession().getCookie().setName(LOGSEARCH_SESSION_ID);

  if ("https".equals(logSearchHttpConfig.getProtocol())) {
    sslConfigurer.ensureStorePasswords();
    sslConfigurer.loadKeystore();
    webServerFactory.addServerCustomizers((JettyServerCustomizer) server -> {
      SslContextFactory sslContextFactory = sslConfigurer.getSslContextFactory();
      ServerConnector sslConnector = new ServerConnector(server, sslContextFactory);
      sslConnector.setPort(logSearchHttpConfig.getHttpsPort());
      server.setConnectors(new Connector[]{sslConnector});
    });
  } else {
    webServerFactory.setPort(logSearchHttpConfig.getHttpPort());
  }
  if (logSearchHttpConfig.isUseAccessLogs()) {
    webServerFactory.addServerCustomizers((JettyServerCustomizer) server -> {
      LoggerContext context = (LoggerContext) LogManager.getContext(false);
      Configuration configuration = context.getConfiguration();
      String logDir = configuration.getStrSubstitutor().getVariableResolver().lookup("log-path");
      String logFileNameSuffix = "logsearch-jetty-yyyy_mm_dd.request.log";
      String logFileName = logDir == null ? logFileNameSuffix : Paths.get(logDir, logFileNameSuffix).toString();
      NCSARequestLog requestLog = new NCSARequestLog(logFileName);
      requestLog.setAppend(true);
      requestLog.setExtended(false);
      requestLog.setLogTimeZone("GMT");
      server.setRequestLog(requestLog);
    });
  }
}
 
Example #5
Source File: BeanConfiguration.java    From sagemaker-sparkml-serving-container with Apache License 2.0 5 votes vote down vote up
@Bean
public JettyServletWebServerFactory provideJettyServletWebServerFactory() {
    final JettyServletWebServerFactory jettyServlet = new JettyServletWebServerFactory(
        new Integer(this.getHttpListenerPort()));
    final List<JettyServerCustomizer> serverCustomizerList = Lists.newArrayList();
    final JettyServerCustomizer serverCustomizer = server -> {
        final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
        threadPool.setMinThreads(SystemUtils.getNumberOfThreads(MIN_CORE_TO_THREAD_RATIO));
        threadPool.setMaxThreads(SystemUtils.getNumberOfThreads(MAX_CORE_TO_THREAD_RATIO));
    };
    serverCustomizerList.add(serverCustomizer);
    jettyServlet.setServerCustomizers(serverCustomizerList);
    return jettyServlet;
}
 
Example #6
Source File: ParaServer.java    From para with Apache License 2.0 4 votes vote down vote up
/**
 * @return Jetty config bean
 */
@Bean
public ServletWebServerFactory jettyConfigBean() {
	JettyServletWebServerFactory jef = new JettyServletWebServerFactory();
	jef.addServerCustomizers((JettyServerCustomizer) (Server server) -> {
		if (Config.getConfigBoolean("access_log_enabled", true)) {
			// enable access log via Logback
			HandlerCollection handlers = new HandlerCollection();
			for (Handler handler : server.getHandlers()) {
				handlers.addHandler(handler);
			}
			RequestLogHandler reqLogs = new RequestLogHandler();
			reqLogs.setServer(server);
			RequestLogImpl rli = new RequestLogImpl();
			rli.setResource("/logback-access.xml");
			rli.setQuiet(true);
			rli.start();
			reqLogs.setRequestLog(rli);
			handlers.addHandler(reqLogs);
			server.setHandler(handlers);
		}

		for (Connector y : server.getConnectors()) {
			for (ConnectionFactory cf : y.getConnectionFactories()) {
				if (cf instanceof HttpConnectionFactory) {
					HttpConnectionFactory dcf = (HttpConnectionFactory) cf;
					// support for X-Forwarded-Proto
					// redirect back to https if original request uses it
					if (Config.IN_PRODUCTION) {
						ForwardedRequestCustomizer frc = new ForwardedRequestCustomizer() {
							public void customize(Connector connector, HttpConfiguration config, Request request) {
								super.customize(connector, config, request);
								String cfProto = request.getHeader("CloudFront-Forwarded-Proto");
								if (StringUtils.equalsIgnoreCase(cfProto, config.getSecureScheme())) {
									request.setScheme(cfProto);
									request.setSecure(true);
								}
							}
						};
						HttpConfiguration httpConfiguration = dcf.getHttpConfiguration();
						httpConfiguration.addCustomizer(frc);
					}
					// Disable Jetty version header
					dcf.getHttpConfiguration().setSendServerVersion(false);
					// Increase idle timeout
					dcf.getHttpConfiguration().setIdleTimeout(TimeUnit.MINUTES.toMillis(5));
				}
			}
		}
	});
	String contextPath = Config.getConfigParam("context_path", "");
	if (StringUtils.length(contextPath) > 1 && contextPath.charAt(0) == '/') {
		jef.setContextPath(contextPath);
	}
	jef.setPort(getServerPort());
	logger.info("Listening on port {}...", jef.getPort());
	return jef;
}
 
Example #7
Source File: KeycloakAutoConfiguration.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnClass(name = {"org.eclipse.jetty.webapp.WebAppContext"})
public JettyServerCustomizer jettyKeycloakServerCustomizer() {
    return new KeycloakJettyServerCustomizer(keycloakProperties);
}