Skip to content

Commit b229d54

Browse files
committed
Consistent evaluation of empty theme names to default theme name
Issue: SPR-11128 (cherry picked from commit cc81aae)
1 parent c77832b commit b229d54

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/theme/CookieThemeResolver.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,12 +20,13 @@
2020
import javax.servlet.http.HttpServletRequest;
2121
import javax.servlet.http.HttpServletResponse;
2222

23+
import org.springframework.util.StringUtils;
2324
import org.springframework.web.servlet.ThemeResolver;
2425
import org.springframework.web.util.CookieGenerator;
2526
import org.springframework.web.util.WebUtils;
2627

2728
/**
28-
* Implementation of ThemeResolver that uses a cookie sent back to the user
29+
* {@link ThemeResolver} implementation that uses a cookie sent back to the user
2930
* in case of a custom setting, with a fallback to the default theme.
3031
* This is particularly useful for stateless applications without user sessions.
3132
*
@@ -78,28 +79,34 @@ public String getDefaultThemeName() {
7879

7980
public String resolveThemeName(HttpServletRequest request) {
8081
// Check request for preparsed or preset theme.
81-
String theme = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME);
82-
if (theme != null) {
83-
return theme;
82+
String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME);
83+
if (themeName != null) {
84+
return themeName;
8485
}
8586

8687
// Retrieve cookie value from request.
8788
Cookie cookie = WebUtils.getCookie(request, getCookieName());
8889
if (cookie != null) {
89-
return cookie.getValue();
90+
String value = cookie.getValue();
91+
if (StringUtils.hasText(value)) {
92+
themeName = value;
93+
}
9094
}
9195

9296
// Fall back to default theme.
93-
return getDefaultThemeName();
97+
if (themeName == null) {
98+
themeName = getDefaultThemeName();
99+
}
100+
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
101+
return themeName;
94102
}
95103

96104
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
97-
if (themeName != null) {
105+
if (StringUtils.hasText(themeName)) {
98106
// Set request attribute and add cookie.
99107
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
100108
addCookie(response, themeName);
101109
}
102-
103110
else {
104111
// Set request attribute to fallback theme and remove cookie.
105112
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName());

spring-webmvc/src/main/java/org/springframework/web/servlet/theme/SessionThemeResolver.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,14 @@
1919
import javax.servlet.http.HttpServletRequest;
2020
import javax.servlet.http.HttpServletResponse;
2121

22+
import org.springframework.util.StringUtils;
2223
import org.springframework.web.util.WebUtils;
2324

2425
/**
25-
* Implementation of ThemeResolver that uses a theme attribute in the user's
26-
* session in case of a custom setting, with a fallback to the default theme.
27-
* This is most appropriate if the application needs user sessions anyway.
26+
* {@link org.springframework.web.servlet.ThemeResolver} implementation that
27+
* uses a theme attribute in the user's session in case of a custom setting,
28+
* with a fallback to the default theme. This is most appropriate if the
29+
* application needs user sessions anyway.
2830
*
2931
* <p>Custom controllers can override the user's theme by calling
3032
* {@code setThemeName}, e.g. responding to a theme change request.
@@ -46,14 +48,16 @@ public class SessionThemeResolver extends AbstractThemeResolver {
4648
*/
4749
public static final String THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME";
4850

51+
4952
public String resolveThemeName(HttpServletRequest request) {
50-
String theme = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
51-
// specific theme, or fallback to default?
52-
return (theme != null ? theme : getDefaultThemeName());
53+
String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
54+
// A specific theme indicated, or do we need to fallback to the default?
55+
return (themeName != null ? themeName : getDefaultThemeName());
5356
}
5457

5558
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
56-
WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, themeName);
59+
WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
60+
(StringUtils.hasText(themeName) ? themeName : null));
5761
}
5862

5963
}

0 commit comments

Comments
 (0)