|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.web.reactive;
|
18 | 18 |
|
| 19 | +import java.io.File; |
19 | 20 | import java.util.Collections;
|
20 | 21 | import java.util.Map;
|
21 | 22 |
|
| 23 | +import org.apache.catalina.Valve; |
| 24 | +import org.apache.catalina.valves.AccessLogValve; |
| 25 | +import org.eclipse.jetty.server.CustomRequestLog; |
| 26 | +import org.eclipse.jetty.server.RequestLog; |
| 27 | +import org.eclipse.jetty.server.RequestLogWriter; |
| 28 | +import org.eclipse.jetty.server.Server; |
| 29 | + |
22 | 30 | import org.springframework.beans.factory.ListableBeanFactory;
|
23 | 31 | import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
|
24 | 32 | import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
|
25 | 33 | import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
|
26 | 34 | import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer;
|
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
27 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
28 | 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
| 38 | +import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; |
| 39 | +import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; |
| 40 | +import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory; |
29 | 41 | import org.springframework.boot.web.server.ConfigurableWebServerFactory;
|
| 42 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
30 | 43 | import org.springframework.context.ApplicationContext;
|
31 | 44 | import org.springframework.context.annotation.Bean;
|
| 45 | +import org.springframework.core.Ordered; |
32 | 46 | import org.springframework.http.server.reactive.ContextPathCompositeHandler;
|
33 | 47 | import org.springframework.http.server.reactive.HttpHandler;
|
34 | 48 | import org.springframework.util.StringUtils;
|
@@ -65,4 +79,125 @@ public HttpHandler httpHandler(ApplicationContext applicationContext, Management
|
65 | 79 | return httpHandler;
|
66 | 80 | }
|
67 | 81 |
|
| 82 | + @Bean |
| 83 | + @ConditionalOnClass(name = "io.undertow.Undertow") |
| 84 | + UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer(ManagementServerProperties properties) { |
| 85 | + return new UndertowAccessLogCustomizer(properties); |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + @ConditionalOnClass(name = "org.apache.catalina.valves.AccessLogValve") |
| 90 | + TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer(ManagementServerProperties properties) { |
| 91 | + return new TomcatAccessLogCustomizer(properties); |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + @ConditionalOnClass(name = "org.eclipse.jetty.server.Server") |
| 96 | + JettyAccessLogCustomizer jettyManagementAccessLogCustomizer(ManagementServerProperties properties) { |
| 97 | + return new JettyAccessLogCustomizer(properties); |
| 98 | + } |
| 99 | + |
| 100 | + abstract static class AccessLogCustomizer implements Ordered { |
| 101 | + |
| 102 | + private final ManagementServerProperties properties; |
| 103 | + |
| 104 | + AccessLogCustomizer(ManagementServerProperties properties) { |
| 105 | + this.properties = properties; |
| 106 | + } |
| 107 | + |
| 108 | + protected String customizePrefix(String prefix) { |
| 109 | + prefix = (prefix != null) ? prefix : ""; |
| 110 | + if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) { |
| 111 | + return prefix; |
| 112 | + } |
| 113 | + return this.properties.getAccesslog().getPrefix() + prefix; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int getOrder() { |
| 118 | + return 1; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + static class TomcatAccessLogCustomizer extends AccessLogCustomizer |
| 124 | + implements WebServerFactoryCustomizer<TomcatReactiveWebServerFactory> { |
| 125 | + |
| 126 | + TomcatAccessLogCustomizer(ManagementServerProperties properties) { |
| 127 | + super(properties); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void customize(TomcatReactiveWebServerFactory factory) { |
| 132 | + System.out.println("Looking for access log valve in " + factory); |
| 133 | + AccessLogValve accessLogValve = findAccessLogValve(factory); |
| 134 | + if (accessLogValve == null) { |
| 135 | + System.out.println("Did not find it"); |
| 136 | + return; |
| 137 | + } |
| 138 | + accessLogValve.setPrefix(customizePrefix(accessLogValve.getPrefix())); |
| 139 | + System.out.println("Customized " + factory); |
| 140 | + } |
| 141 | + |
| 142 | + private AccessLogValve findAccessLogValve(TomcatReactiveWebServerFactory factory) { |
| 143 | + for (Valve engineValve : factory.getEngineValves()) { |
| 144 | + if (engineValve instanceof AccessLogValve accessLogValve) { |
| 145 | + return accessLogValve; |
| 146 | + } |
| 147 | + } |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + static class UndertowAccessLogCustomizer extends AccessLogCustomizer |
| 154 | + implements WebServerFactoryCustomizer<UndertowReactiveWebServerFactory> { |
| 155 | + |
| 156 | + UndertowAccessLogCustomizer(ManagementServerProperties properties) { |
| 157 | + super(properties); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void customize(UndertowReactiveWebServerFactory factory) { |
| 162 | + factory.setAccessLogPrefix(customizePrefix(factory.getAccessLogPrefix())); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + static class JettyAccessLogCustomizer extends AccessLogCustomizer |
| 168 | + implements WebServerFactoryCustomizer<JettyReactiveWebServerFactory> { |
| 169 | + |
| 170 | + JettyAccessLogCustomizer(ManagementServerProperties properties) { |
| 171 | + super(properties); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void customize(JettyReactiveWebServerFactory factory) { |
| 176 | + factory.addServerCustomizers(this::customizeServer); |
| 177 | + } |
| 178 | + |
| 179 | + private void customizeServer(Server server) { |
| 180 | + RequestLog requestLog = server.getRequestLog(); |
| 181 | + if (requestLog instanceof CustomRequestLog customRequestLog) { |
| 182 | + customizeRequestLog(customRequestLog); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private void customizeRequestLog(CustomRequestLog requestLog) { |
| 187 | + if (requestLog.getWriter() instanceof RequestLogWriter requestLogWriter) { |
| 188 | + customizeRequestLogWriter(requestLogWriter); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private void customizeRequestLogWriter(RequestLogWriter writer) { |
| 193 | + String filename = writer.getFileName(); |
| 194 | + if (StringUtils.hasLength(filename)) { |
| 195 | + File file = new File(filename); |
| 196 | + file = new File(file.getParentFile(), customizePrefix(file.getName())); |
| 197 | + writer.setFilename(file.getPath()); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
68 | 203 | }
|
0 commit comments