Skip to content

Commit

Permalink
avniproject/avni-infra#45 | Customize tomcat to respect access log ma…
Browse files Browse the repository at this point in the history
…x-days config
  • Loading branch information
1t5j0y committed Oct 29, 2024
1 parent eb8b76b commit 9f2c164
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package org.avni.server.framework.tomcat;

import org.apache.catalina.Valve;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.http.SameSiteCookies;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class TomcatContainerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

private static final Logger LOGGER = LoggerFactory.getLogger(TomcatContainerCustomizer.class);

@Value("${server.tomcat.accesslog.max-days}")
int accessLogMaxDays;

@Override
public void customize(TomcatServletWebServerFactory factory) {
if (factory != null) {
Expand All @@ -23,6 +31,14 @@ public void customize(TomcatServletWebServerFactory factory) {
});
factory.addContextCustomizers(sameSiteCookiesConfig());
LOGGER.info("Enabled secure scheme (https).");

Collection<Valve> engineValves = factory.getEngineValves();
for (Valve tempObject : engineValves) {
if (tempObject instanceof AccessLogValve) {
AccessLogValve accessLogValve = (AccessLogValve) tempObject;
accessLogValve.setMaxDays(accessLogMaxDays);
}
}
} else {
LOGGER.warn("Could not change protocol scheme because Tomcat is not used as servlet container.");
}
Expand Down

0 comments on commit 9f2c164

Please sign in to comment.