|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2013 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import javax.servlet.http.HttpServletRequest;
|
21 | 21 | import javax.servlet.http.HttpServletResponse;
|
22 | 22 |
|
| 23 | +import org.springframework.util.StringUtils; |
23 | 24 | import org.springframework.web.servlet.ThemeResolver;
|
24 | 25 | import org.springframework.web.util.CookieGenerator;
|
25 | 26 | import org.springframework.web.util.WebUtils;
|
26 | 27 |
|
27 | 28 | /**
|
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 |
29 | 30 | * in case of a custom setting, with a fallback to the default theme.
|
30 | 31 | * This is particularly useful for stateless applications without user sessions.
|
31 | 32 | *
|
@@ -78,28 +79,34 @@ public String getDefaultThemeName() {
|
78 | 79 |
|
79 | 80 | public String resolveThemeName(HttpServletRequest request) {
|
80 | 81 | // 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; |
84 | 85 | }
|
85 | 86 |
|
86 | 87 | // Retrieve cookie value from request.
|
87 | 88 | Cookie cookie = WebUtils.getCookie(request, getCookieName());
|
88 | 89 | if (cookie != null) {
|
89 |
| - return cookie.getValue(); |
| 90 | + String value = cookie.getValue(); |
| 91 | + if (StringUtils.hasText(value)) { |
| 92 | + themeName = value; |
| 93 | + } |
90 | 94 | }
|
91 | 95 |
|
92 | 96 | // 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; |
94 | 102 | }
|
95 | 103 |
|
96 | 104 | public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
|
97 |
| - if (themeName != null) { |
| 105 | + if (StringUtils.hasText(themeName)) { |
98 | 106 | // Set request attribute and add cookie.
|
99 | 107 | request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
|
100 | 108 | addCookie(response, themeName);
|
101 | 109 | }
|
102 |
| - |
103 | 110 | else {
|
104 | 111 | // Set request attribute to fallback theme and remove cookie.
|
105 | 112 | request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName());
|
|
0 commit comments