/** * Retrieves the current theme from the given request, using the ThemeResolver * and ThemeSource bound to the request by the DispatcherServlet. * @param request current HTTP request * @return the current theme, or {@code null} if not found * @see #getThemeResolver */ @Nullable public static Theme getTheme(HttpServletRequest request) { ThemeResolver themeResolver = getThemeResolver(request); ThemeSource themeSource = getThemeSource(request); if (themeResolver != null && themeSource != null) { String themeName = themeResolver.resolveThemeName(request); return themeSource.getTheme(themeName); } else { return null; } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newTheme = request.getParameter(this.paramName); if (newTheme != null) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request); if (themeResolver == null) { throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?"); } themeResolver.setThemeName(request, response, newTheme); } // Proceed in any case. return true; }
private void internalTest(ThemeResolver themeResolver, boolean shouldSet, String defaultName) { // create mocks MockServletContext context = new MockServletContext(); MockHttpServletRequest request = new MockHttpServletRequest(context); MockHttpServletResponse response = new MockHttpServletResponse(); // check original theme String themeName = themeResolver.resolveThemeName(request); assertEquals(themeName, defaultName); // set new theme name try { themeResolver.setThemeName(request, response, TEST_THEME_NAME); if (!shouldSet) fail("should not be able to set Theme name"); // check new theme namelocale themeName = themeResolver.resolveThemeName(request); assertEquals(TEST_THEME_NAME, themeName); themeResolver.setThemeName(request, response, null); themeName = themeResolver.resolveThemeName(request); assertEquals(themeName, defaultName); } catch (UnsupportedOperationException ex) { if (shouldSet) fail("should be able to set Theme name"); } }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { Locale locale = localeResolver.resolveLocale(request); userSettingsHolder.setLocale(locale); localeResolver.setLocale(request, response, locale); String themeName = themeResolver.resolveThemeName(request); userSettingsHolder.setTheme(themeName); themeResolver.setThemeName(request, response, themeName); final String timezone = timezoneResolver.resolveSetting(request); userSettingsHolder.setTimezone(timezone); timezoneResolver.setSetting(timezone); }
/** * Retrieves the current theme from the given request, using the ThemeResolver * and ThemeSource bound to the request by the DispatcherServlet. * @param request current HTTP request * @return the current theme, or {@code null} if not found * @see #getThemeResolver */ @Nullable public static Theme getTheme(HttpServletRequest request) { ThemeResolver themeResolver = getThemeResolver(request); ThemeSource themeSource = getThemeSource(request); if (themeResolver != null && themeSource != null) { String themeName = themeResolver.resolveThemeName(request); return themeSource.getTheme(themeName); } else { return null; } }
/** * Change the current theme to the specified theme by name, * storing the new theme name through the configured {@link ThemeResolver}. * @param themeName the name of the new theme * @see ThemeResolver#setThemeName */ public void changeTheme(String themeName) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request); if (themeResolver == null) { throw new IllegalStateException("Cannot change theme if no ThemeResolver configured"); } themeResolver.setThemeName(this.request, this.response, themeName); // Ask for re-resolution on next getTheme call. this.theme = null; }
throw new ServletException("Incorrect ThemeResolver"); if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name"); throw new ServletException("Incorrect TimeZone"); if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name");
/** * Change the current theme to the specified one, * storing the new theme name through the configured {@link ThemeResolver}. * @param theme the new theme * @see ThemeResolver#setThemeName */ public void changeTheme(@Nullable Theme theme) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request); if (themeResolver == null) { throw new IllegalStateException("Cannot change theme if no ThemeResolver configured"); } themeResolver.setThemeName(this.request, this.response, (theme != null ? theme.getName() : null)); this.theme = theme; }
@Override public String resolve(String staticPrefix) { String themeName = themeResolver.resolveThemeName(getRequest()); String path = new File(staticPrefix, themeName).getPath(); if (staticPrefix.endsWith(File.separator)) { return path + File.separator; } else { return path; } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newTheme = request.getParameter(this.paramName); if (newTheme != null) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request); if (themeResolver == null) { throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?"); } themeResolver.setThemeName(request, response, newTheme); } // Proceed in any case. return true; }
@Override public String resolveThemeName(HttpServletRequest request) { String themeName = getThemeName(request); Iterator<ThemeResolver> iter = themeResolvers.iterator(); while (iter.hasNext() && !StringUtils.hasText(themeName)) { themeName = iter.next().resolveThemeName(request); } cacheThemeName(request, themeName); return StringUtils.hasText(themeName) ? themeName : defaultTheme; }
/** * Change the current theme to the specified theme by name, * storing the new theme name through the configured {@link ThemeResolver}. * @param themeName the name of the new theme * @see ThemeResolver#setThemeName */ public void changeTheme(String themeName) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request); if (themeResolver == null) { throw new IllegalStateException("Cannot change theme if no ThemeResolver configured"); } themeResolver.setThemeName(this.request, this.response, themeName); // Ask for re-resolution on next getTheme call. this.theme = null; }
@Override public String resolveThemeName(final HttpServletRequest httpServletRequest) { val it = chain.iterator(); while (it.hasNext()) { val r = it.next(); LOGGER.trace("Attempting to resolve theme via [{}]", r.getClass().getSimpleName()); val resolverTheme = r.resolveThemeName(httpServletRequest); if (!resolverTheme.equalsIgnoreCase(getDefaultThemeName())) { LOGGER.trace("Resolved theme [{}]", resolverTheme); return resolverTheme; } } LOGGER.trace("No specific theme could be found. Using default theme [{}}", getDefaultThemeName()); return getDefaultThemeName(); }
/** * Change the current theme to the specified one, * storing the new theme name through the configured {@link ThemeResolver}. * @param theme the new theme * @see ThemeResolver#setThemeName */ public void changeTheme(@Nullable Theme theme) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request); if (themeResolver == null) { throw new IllegalStateException("Cannot change theme if no ThemeResolver configured"); } themeResolver.setThemeName(this.request, this.response, (theme != null ? theme.getName() : null)); this.theme = theme; }
@ModelAttribute("currentTheme") public Theme currentTheme(HttpServletRequest request) { return themeRegistry.getPluginFor(themeResolver.resolveThemeName(request), themeRegistry.getPluginFor(DEFAULT_THEME_NAME)); }
@Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { themeName = StringUtils.hasText(themeName) ? themeName : defaultTheme; for (ThemeResolver themeResolver : themeResolvers) { themeResolver.setThemeName(request, response, themeName); } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String themeName = themeResolver.resolveThemeName(httpRequest); Theme theme = ThemeRegistry.getInstance().get(themeName); if (theme != null) { String originalPath = httpRequest.getPathInfo(); String requestPath = originalPath == null ? null : theme.translatePath(originalPath); if (requestPath != null) { requestPath = requestPath.isEmpty() ? "empty/" + originalPath : requestPath; httpRequest.getRequestDispatcher(requestPath).forward(httpRequest, response); return; } } chain.doFilter(request, response); }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newTheme = request.getParameter(this.paramName); if (newTheme != null) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request); if (themeResolver == null) { throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?"); } themeResolver.setThemeName(request, response, newTheme); } // Proceed in any case. return true; }
private ResourceUrlResolver urlResolver() { if (themeResolver == null || !useThemeInViewPath) { return IdentityUrlResolver.INSTANCE; } else { return new ThemedResourceUrlResolver(themeResolver.resolveThemeName(getServletRequest())); } } }
/** * Change the current theme to the specified one, * storing the new theme name through the configured {@link ThemeResolver}. * @param theme the new theme * @see ThemeResolver#setThemeName */ public void changeTheme(Theme theme) { ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request); if (themeResolver == null) { throw new IllegalStateException("Cannot change theme if no ThemeResolver configured"); } themeResolver.setThemeName(this.request, this.response, (theme != null ? theme.getName() : null)); this.theme = theme; }