|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.web.server.jetty; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +import org.eclipse.jetty.server.CustomRequestLog; |
| 22 | +import org.eclipse.jetty.server.RequestLog; |
| 23 | +import org.eclipse.jetty.server.RequestLogWriter; |
| 24 | +import org.eclipse.jetty.server.Server; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 27 | +import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; |
| 28 | +import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType; |
| 29 | +import org.springframework.boot.actuate.autoconfigure.web.server.AccessLogCustomizer; |
| 30 | +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; |
| 31 | +import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementWebServerFactoryCustomizer; |
| 32 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 33 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; |
| 35 | +import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer; |
| 36 | +import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer; |
| 37 | +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer; |
| 38 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 39 | +import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; |
| 40 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.util.StringUtils; |
| 43 | + |
| 44 | +/** |
| 45 | + * {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jetty-based |
| 46 | + * servlet web endpoint infrastructure when a separate management context running on a |
| 47 | + * different port is required. |
| 48 | + * |
| 49 | + * @author Andy Wilkinson |
| 50 | + */ |
| 51 | +@ConditionalOnClass(Server.class) |
| 52 | +@ConditionalOnWebApplication(type = Type.SERVLET) |
| 53 | +@EnableConfigurationProperties(ManagementServerProperties.class) |
| 54 | +@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false) |
| 55 | +class JettyServletManagementChildContextConfiguration { |
| 56 | + |
| 57 | + @Bean |
| 58 | + JettyAccessLogCustomizer jettyManagementAccessLogCustomizer(ManagementServerProperties properties) { |
| 59 | + return new JettyAccessLogCustomizer(properties); |
| 60 | + } |
| 61 | + |
| 62 | + @Bean |
| 63 | + ServletManagementWebServerFactoryCustomizer servletManagementWebServerFactoryCustomizer( |
| 64 | + ListableBeanFactory beanFactory) { |
| 65 | + return new ServletManagementWebServerFactoryCustomizer(beanFactory, ServletWebServerFactoryCustomizer.class, |
| 66 | + JettyWebServerFactoryCustomizer.class, JettyVirtualThreadsWebServerFactoryCustomizer.class); |
| 67 | + } |
| 68 | + |
| 69 | + static class JettyAccessLogCustomizer extends AccessLogCustomizer<JettyServletWebServerFactory> |
| 70 | + implements WebServerFactoryCustomizer<JettyServletWebServerFactory> { |
| 71 | + |
| 72 | + JettyAccessLogCustomizer(ManagementServerProperties properties) { |
| 73 | + super(properties); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void customize(JettyServletWebServerFactory factory) { |
| 78 | + factory.addServerCustomizers(this::customizeServer); |
| 79 | + } |
| 80 | + |
| 81 | + private void customizeServer(Server server) { |
| 82 | + RequestLog requestLog = server.getRequestLog(); |
| 83 | + if (requestLog instanceof CustomRequestLog customRequestLog) { |
| 84 | + customizeRequestLog(customRequestLog); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void customizeRequestLog(CustomRequestLog requestLog) { |
| 89 | + if (requestLog.getWriter() instanceof RequestLogWriter requestLogWriter) { |
| 90 | + customizeRequestLogWriter(requestLogWriter); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void customizeRequestLogWriter(RequestLogWriter writer) { |
| 95 | + String filename = writer.getFileName(); |
| 96 | + if (StringUtils.hasLength(filename)) { |
| 97 | + File file = new File(filename); |
| 98 | + file = new File(file.getParentFile(), customizePrefix(file.getName())); |
| 99 | + writer.setFilename(file.getPath()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments