Example usage for org.springframework.web.servlet ThemeResolver setThemeName

List of usage examples for org.springframework.web.servlet ThemeResolver setThemeName

Introduction

In this page you can find the example usage for org.springframework.web.servlet ThemeResolver setThemeName.

Prototype

void setThemeName(HttpServletRequest request, @Nullable HttpServletResponse response,
        @Nullable String themeName);

Source Link

Document

Set the current theme name to the given one.

Usage

From source file:org.dspace.webmvc.theme.SpringThemeContextUtils.java

/**
 * Set the current theme/*from ww w  .  j  a  v  a 2s . com*/
 *
 * @param themeName
 * @param request
 * @param response
 */
public static void setThemeName(String themeName, HttpServletRequest request, HttpServletResponse response) {
    ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request);
    themeResolver.setThemeName(request, response, themeName);
    SpringThemeHolder themeHolder = SpringThemeHolder.getCurrentTheme();
    if (!themeName.equals(themeHolder.getName())) {
        SpringThemeHolder.resetCurrentTheme();
    }
}

From source file:org.dspace.webmvc.theme.ThemeChangeInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request);
    if (themeResolver == null) {
        throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?");
    }/*from   w  w w .  j  ava 2  s. co m*/

    String newTheme = request.getParameter(this.paramName);
    if (newTheme != null) {
        themeResolver.setThemeName(request, response, newTheme);
        response.addCookie(new Cookie("themeName", newTheme));
    } else {
        ThemeMapEntry bestMatch = null;

        for (ThemeMapEntry entry : themeMappings) {

            if (entry.mapType == MapType.VIEW || entry.mapType == MapType.ANY) {
                if (modelAndView != null && pathMatcher.match(entry.path, modelAndView.getViewName())) {
                    if (entry.isBestMatch(bestMatch)) {
                        bestMatch = entry;
                    }
                }
            }

            if (entry.mapType == MapType.URL || entry.mapType == MapType.ANY) {
                String path = urlPathHelper.getLookupPathForRequest(request);
                if (pathMatcher.match(entry.path, path)) {
                    if (entry.isBestMatch(bestMatch)) {
                        bestMatch = entry;
                    }
                }
            }

            if (entry.mapType == MapType.CONTROLLER || entry.mapType == MapType.ANY) {

            }
        }

        if (bestMatch != null) {
            themeResolver.setThemeName(request, response, bestMatch.themeName);
        } else if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if ("themeName".equals(cookie.getName())) {
                    themeResolver.setThemeName(request, response, cookie.getValue());
                }
            }
        }
    }

    super.postHandle(request, response, handler, modelAndView);
}