use of org.eclipse.jetty.server.CustomRequestLog in project kafka by apache.
the class RestServer method initializeResources.
public void initializeResources(Herder herder) {
log.info("Initializing REST resources");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(new JacksonJsonProvider());
resourceConfig.register(new RootResource(herder));
resourceConfig.register(new ConnectorsResource(herder, config));
resourceConfig.register(new ConnectorPluginsResource(herder));
resourceConfig.register(ConnectExceptionMapper.class);
resourceConfig.property(ServerProperties.WADL_FEATURE_DISABLE, true);
registerRestExtensions(herder, resourceConfig);
List<String> adminListeners = config.getList(WorkerConfig.ADMIN_LISTENERS_CONFIG);
ResourceConfig adminResourceConfig;
if (adminListeners == null) {
log.info("Adding admin resources to main listener");
adminResourceConfig = resourceConfig;
adminResourceConfig.register(new LoggingResource());
} else if (adminListeners.size() > 0) {
// TODO: we need to check if these listeners are same as 'listeners'
// TODO: the following code assumes that they are different
log.info("Adding admin resources to admin listener");
adminResourceConfig = new ResourceConfig();
adminResourceConfig.register(new JacksonJsonProvider());
adminResourceConfig.register(new LoggingResource());
adminResourceConfig.register(ConnectExceptionMapper.class);
} else {
log.info("Skipping adding admin resources");
// set up adminResource but add no handlers to it
adminResourceConfig = resourceConfig;
}
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder servletHolder = new ServletHolder(servletContainer);
List<Handler> contextHandlers = new ArrayList<>();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
contextHandlers.add(context);
ServletContextHandler adminContext = null;
if (adminResourceConfig != resourceConfig) {
adminContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder adminServletHolder = new ServletHolder(new ServletContainer(adminResourceConfig));
adminContext.setContextPath("/");
adminContext.addServlet(adminServletHolder, "/*");
adminContext.setVirtualHosts(new String[] { "@" + ADMIN_SERVER_CONNECTOR_NAME });
contextHandlers.add(adminContext);
}
String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
if (!Utils.isBlank(allowedOrigins)) {
FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
filterHolder.setName("cross-origin");
filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, allowedOrigins);
String allowedMethods = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG);
if (!Utils.isBlank(allowedMethods)) {
filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
}
context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
}
String headerConfig = config.getString(WorkerConfig.RESPONSE_HTTP_HEADERS_CONFIG);
if (!Utils.isBlank(headerConfig)) {
configureHttpResponsHeaderFilter(context);
}
RequestLogHandler requestLogHandler = new RequestLogHandler();
Slf4jRequestLogWriter slf4jRequestLogWriter = new Slf4jRequestLogWriter();
slf4jRequestLogWriter.setLoggerName(RestServer.class.getCanonicalName());
CustomRequestLog requestLog = new CustomRequestLog(slf4jRequestLogWriter, CustomRequestLog.EXTENDED_NCSA_FORMAT + " %{ms}T");
requestLogHandler.setRequestLog(requestLog);
contextHandlers.add(new DefaultHandler());
contextHandlers.add(requestLogHandler);
handlers.setHandlers(contextHandlers.toArray(new Handler[0]));
try {
context.start();
} catch (Exception e) {
throw new ConnectException("Unable to initialize REST resources", e);
}
if (adminResourceConfig != resourceConfig) {
try {
log.debug("Starting admin context");
adminContext.start();
} catch (Exception e) {
throw new ConnectException("Unable to initialize Admin REST resources", e);
}
}
log.info("REST resources initialized; server is started and ready to handle requests");
}
use of org.eclipse.jetty.server.CustomRequestLog in project spring-boot by spring-projects.
the class JettyWebServerFactoryCustomizerTests method getRequestLog.
private CustomRequestLog getRequestLog(JettyWebServer server) {
RequestLog requestLog = server.getServer().getRequestLog();
assertThat(requestLog).isInstanceOf(CustomRequestLog.class);
return (CustomRequestLog) requestLog;
}
use of org.eclipse.jetty.server.CustomRequestLog in project spring-boot by spring-projects.
the class JettyWebServerFactoryCustomizer method customizeAccessLog.
private void customizeAccessLog(ConfigurableJettyWebServerFactory factory, ServerProperties.Jetty.Accesslog properties) {
factory.addServerCustomizers((server) -> {
RequestLogWriter logWriter = new RequestLogWriter();
String format = getLogFormat(properties);
CustomRequestLog log = new CustomRequestLog(logWriter, format);
if (!CollectionUtils.isEmpty(properties.getIgnorePaths())) {
log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0]));
}
if (properties.getFilename() != null) {
logWriter.setFilename(properties.getFilename());
}
if (properties.getFileDateFormat() != null) {
logWriter.setFilenameDateFormat(properties.getFileDateFormat());
}
logWriter.setRetainDays(properties.getRetentionPeriod());
logWriter.setAppend(properties.isAppend());
server.setRequestLog(log);
});
}
use of org.eclipse.jetty.server.CustomRequestLog in project spring-boot by spring-projects.
the class JettyWebServerFactoryCustomizerTests method accessLogCanBeCustomized.
@Test
void accessLogCanBeCustomized() throws IOException {
File logFile = File.createTempFile("jetty_log", ".log");
bind("server.jetty.accesslog.enabled=true", "server.jetty.accesslog.format=extended_ncsa", "server.jetty.accesslog.filename=" + logFile.getAbsolutePath().replace("\\", "\\\\"), "server.jetty.accesslog.file-date-format=yyyy-MM-dd", "server.jetty.accesslog.retention-period=42", "server.jetty.accesslog.append=true", "server.jetty.accesslog.ignore-paths=/a/path,/b/path");
JettyWebServer server = customizeAndGetServer();
CustomRequestLog requestLog = getRequestLog(server);
assertThat(requestLog.getFormatString()).isEqualTo(CustomRequestLog.EXTENDED_NCSA_FORMAT);
assertThat(requestLog.getIgnorePaths()).hasSize(2);
assertThat(requestLog.getIgnorePaths()).containsExactly("/a/path", "/b/path");
RequestLogWriter logWriter = getLogWriter(requestLog);
assertThat(logWriter.getFileName()).isEqualTo(logFile.getAbsolutePath());
assertThat(logWriter.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
assertThat(logWriter.getRetainDays()).isEqualTo(42);
assertThat(logWriter.isAppend()).isTrue();
}
use of org.eclipse.jetty.server.CustomRequestLog in project spring-boot by spring-projects.
the class JettyWebServerFactoryCustomizerTests method accessLogCanBeEnabled.
@Test
void accessLogCanBeEnabled() {
bind("server.jetty.accesslog.enabled=true");
JettyWebServer server = customizeAndGetServer();
CustomRequestLog requestLog = getRequestLog(server);
assertThat(requestLog.getFormatString()).isEqualTo(CustomRequestLog.NCSA_FORMAT);
assertThat(requestLog.getIgnorePaths()).isNull();
RequestLogWriter logWriter = getLogWriter(requestLog);
assertThat(logWriter.getFileName()).isNull();
assertThat(logWriter.isAppend()).isFalse();
}
Aggregations