Skip to content

Commit d986757

Browse files
committed
polish AbstractServletWebServerFactory
1 parent a623dab commit d986757

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.net.URL;
2121
import java.nio.charset.Charset;
22+
import java.time.Duration;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collections;
@@ -37,6 +38,7 @@
3738
import org.apache.commons.logging.Log;
3839
import org.apache.commons.logging.LogFactory;
3940

41+
import org.springframework.boot.context.properties.PropertyMapper;
4042
import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory;
4143
import org.springframework.boot.web.server.MimeMappings;
4244
import org.springframework.boot.web.servlet.ServletContextInitializer;
@@ -317,27 +319,14 @@ public void onStartup(ServletContext servletContext) throws ServletException {
317319

318320
private void configureSessionCookie(SessionCookieConfig config) {
319321
Session.Cookie cookie = this.session.getCookie();
320-
if (cookie.getName() != null) {
321-
config.setName(cookie.getName());
322-
}
323-
if (cookie.getDomain() != null) {
324-
config.setDomain(cookie.getDomain());
325-
}
326-
if (cookie.getPath() != null) {
327-
config.setPath(cookie.getPath());
328-
}
329-
if (cookie.getComment() != null) {
330-
config.setComment(cookie.getComment());
331-
}
332-
if (cookie.getHttpOnly() != null) {
333-
config.setHttpOnly(cookie.getHttpOnly());
334-
}
335-
if (cookie.getSecure() != null) {
336-
config.setSecure(cookie.getSecure());
337-
}
338-
if (cookie.getMaxAge() != null) {
339-
config.setMaxAge((int) cookie.getMaxAge().getSeconds());
340-
}
322+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
323+
map.from(cookie::getName).to(config::setName);
324+
map.from(cookie::getDomain).to(config::setDomain);
325+
map.from(cookie::getPath).to(config::setPath);
326+
map.from(cookie::getComment).to(config::setComment);
327+
map.from(cookie::getHttpOnly).to(config::setHttpOnly);
328+
map.from(cookie::getSecure).to(config::setSecure);
329+
map.from(cookie::getMaxAge).asInt(Duration::getSeconds).to(config::setMaxAge);
341330
}
342331

343332
private Set<javax.servlet.SessionTrackingMode> unwrap(Set<Session.SessionTrackingMode> modes) {

0 commit comments

Comments
 (0)