Consistent evaluation of empty theme names to default theme name

Issue: SPR-11128
This commit is contained in:
Juergen Hoeller 2014-01-20 21:49:28 +01:00
parent 88730bdaa5
commit cc81aae8c1
2 changed files with 28 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,12 +20,13 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ThemeResolver; import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.util.CookieGenerator; import org.springframework.web.util.CookieGenerator;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
* Implementation of ThemeResolver that uses a cookie sent back to the user * {@link ThemeResolver} implementation that uses a cookie sent back to the user
* in case of a custom setting, with a fallback to the default theme. * in case of a custom setting, with a fallback to the default theme.
* This is particularly useful for stateless applications without user sessions. * This is particularly useful for stateless applications without user sessions.
* *
@ -79,29 +80,35 @@ public class CookieThemeResolver extends CookieGenerator implements ThemeResolve
@Override @Override
public String resolveThemeName(HttpServletRequest request) { public String resolveThemeName(HttpServletRequest request) {
// Check request for preparsed or preset theme. // Check request for preparsed or preset theme.
String theme = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME);
if (theme != null) { if (themeName != null) {
return theme; return themeName;
} }
// Retrieve cookie value from request. // Retrieve cookie value from request.
Cookie cookie = WebUtils.getCookie(request, getCookieName()); Cookie cookie = WebUtils.getCookie(request, getCookieName());
if (cookie != null) { if (cookie != null) {
return cookie.getValue(); String value = cookie.getValue();
if (StringUtils.hasText(value)) {
themeName = value;
}
} }
// Fall back to default theme. // Fall back to default theme.
return getDefaultThemeName(); if (themeName == null) {
themeName = getDefaultThemeName();
}
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
return themeName;
} }
@Override @Override
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
if (themeName != null) { if (StringUtils.hasText(themeName)) {
// Set request attribute and add cookie. // Set request attribute and add cookie.
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
addCookie(response, themeName); addCookie(response, themeName);
} }
else { else {
// Set request attribute to fallback theme and remove cookie. // Set request attribute to fallback theme and remove cookie.
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName()); request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,12 +19,14 @@ package org.springframework.web.servlet.theme;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
* Implementation of ThemeResolver that uses a theme attribute in the user's * {@link org.springframework.web.servlet.ThemeResolver} implementation that
* session in case of a custom setting, with a fallback to the default theme. * uses a theme attribute in the user's session in case of a custom setting,
* This is most appropriate if the application needs user sessions anyway. * with a fallback to the default theme. This is most appropriate if the
* application needs user sessions anyway.
* *
* <p>Custom controllers can override the user's theme by calling * <p>Custom controllers can override the user's theme by calling
* {@code setThemeName}, e.g. responding to a theme change request. * {@code setThemeName}, e.g. responding to a theme change request.
@ -46,16 +48,18 @@ public class SessionThemeResolver extends AbstractThemeResolver {
*/ */
public static final String THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME"; public static final String THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME";
@Override @Override
public String resolveThemeName(HttpServletRequest request) { public String resolveThemeName(HttpServletRequest request) {
String theme = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME); String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
// specific theme, or fallback to default? // A specific theme indicated, or do we need to fallback to the default?
return (theme != null ? theme : getDefaultThemeName()); return (themeName != null ? themeName : getDefaultThemeName());
} }
@Override @Override
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, themeName); WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
(StringUtils.hasText(themeName) ? themeName : null));
} }
} }