Search in sources :

Example 1 with Accesslog

use of org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog in project spring-boot by spring-projects.

the class TomcatWebServerFactoryCustomizer method customizeAccessLog.

private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
    ServerProperties.Tomcat tomcatProperties = this.serverProperties.getTomcat();
    AccessLogValve valve = new AccessLogValve();
    PropertyMapper map = PropertyMapper.get();
    Accesslog accessLogConfig = tomcatProperties.getAccesslog();
    map.from(accessLogConfig.getConditionIf()).to(valve::setConditionIf);
    map.from(accessLogConfig.getConditionUnless()).to(valve::setConditionUnless);
    map.from(accessLogConfig.getPattern()).to(valve::setPattern);
    map.from(accessLogConfig.getDirectory()).to(valve::setDirectory);
    map.from(accessLogConfig.getPrefix()).to(valve::setPrefix);
    map.from(accessLogConfig.getSuffix()).to(valve::setSuffix);
    map.from(accessLogConfig.getEncoding()).whenHasText().to(valve::setEncoding);
    map.from(accessLogConfig.getLocale()).whenHasText().to(valve::setLocale);
    map.from(accessLogConfig.isCheckExists()).to(valve::setCheckExists);
    map.from(accessLogConfig.isRotate()).to(valve::setRotatable);
    map.from(accessLogConfig.isRenameOnRotate()).to(valve::setRenameOnRotate);
    map.from(accessLogConfig.getMaxDays()).to(valve::setMaxDays);
    map.from(accessLogConfig.getFileDateFormat()).to(valve::setFileDateFormat);
    map.from(accessLogConfig.isIpv6Canonical()).to(valve::setIpv6Canonical);
    map.from(accessLogConfig.isRequestAttributesEnabled()).to(valve::setRequestAttributesEnabled);
    map.from(accessLogConfig.isBuffered()).to(valve::setBuffered);
    factory.addEngineValves(valve);
}
Also used : Accesslog(org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog) ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) PropertyMapper(org.springframework.boot.context.properties.PropertyMapper) AccessLogValve(org.apache.catalina.valves.AccessLogValve)

Example 2 with Accesslog

use of org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog in project spring-boot by spring-projects.

the class ServerPropertiesTests method testTomcatBinding.

@Test
void testTomcatBinding() {
    Map<String, String> map = new HashMap<>();
    map.put("server.tomcat.accesslog.conditionIf", "foo");
    map.put("server.tomcat.accesslog.conditionUnless", "bar");
    map.put("server.tomcat.accesslog.pattern", "%h %t '%r' %s %b");
    map.put("server.tomcat.accesslog.prefix", "foo");
    map.put("server.tomcat.accesslog.suffix", "-bar.log");
    map.put("server.tomcat.accesslog.encoding", "UTF-8");
    map.put("server.tomcat.accesslog.locale", "en-AU");
    map.put("server.tomcat.accesslog.checkExists", "true");
    map.put("server.tomcat.accesslog.rotate", "false");
    map.put("server.tomcat.accesslog.rename-on-rotate", "true");
    map.put("server.tomcat.accesslog.ipv6Canonical", "true");
    map.put("server.tomcat.accesslog.request-attributes-enabled", "true");
    map.put("server.tomcat.remoteip.protocol-header", "X-Forwarded-Protocol");
    map.put("server.tomcat.remoteip.remote-ip-header", "Remote-Ip");
    map.put("server.tomcat.remoteip.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
    map.put("server.tomcat.reject-illegal-header", "false");
    map.put("server.tomcat.background-processor-delay", "10");
    map.put("server.tomcat.relaxed-path-chars", "|,<");
    map.put("server.tomcat.relaxed-query-chars", "^  ,  | ");
    map.put("server.tomcat.use-relative-redirects", "true");
    bind(map);
    ServerProperties.Tomcat tomcat = this.properties.getTomcat();
    Accesslog accesslog = tomcat.getAccesslog();
    assertThat(accesslog.getConditionIf()).isEqualTo("foo");
    assertThat(accesslog.getConditionUnless()).isEqualTo("bar");
    assertThat(accesslog.getPattern()).isEqualTo("%h %t '%r' %s %b");
    assertThat(accesslog.getPrefix()).isEqualTo("foo");
    assertThat(accesslog.getSuffix()).isEqualTo("-bar.log");
    assertThat(accesslog.getEncoding()).isEqualTo("UTF-8");
    assertThat(accesslog.getLocale()).isEqualTo("en-AU");
    assertThat(accesslog.isCheckExists()).isEqualTo(true);
    assertThat(accesslog.isRotate()).isFalse();
    assertThat(accesslog.isRenameOnRotate()).isTrue();
    assertThat(accesslog.isIpv6Canonical()).isTrue();
    assertThat(accesslog.isRequestAttributesEnabled()).isTrue();
    assertThat(tomcat.getRemoteip().getRemoteIpHeader()).isEqualTo("Remote-Ip");
    assertThat(tomcat.getRemoteip().getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
    assertThat(tomcat.getRemoteip().getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
    assertThat(tomcat.isRejectIllegalHeader()).isFalse();
    assertThat(tomcat.getBackgroundProcessorDelay()).hasSeconds(10);
    assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');
    assertThat(tomcat.getRelaxedQueryChars()).containsExactly('^', '|');
    assertThat(tomcat.isUseRelativeRedirects()).isTrue();
}
Also used : Accesslog(org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

Accesslog (org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog)2 HashMap (java.util.HashMap)1 AccessLogValve (org.apache.catalina.valves.AccessLogValve)1 Test (org.junit.jupiter.api.Test)1 ServerProperties (org.springframework.boot.autoconfigure.web.ServerProperties)1 PropertyMapper (org.springframework.boot.context.properties.PropertyMapper)1