Search in sources :

Example 11 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project cas by apereo.

the class CasThemesConfiguration method themeResolver.

@ConditionalOnMissingBean(name = "casThemeResolver")
@Bean
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public ThemeResolver themeResolver(@Qualifier("serviceThemeResolverSupportedBrowsers") final Supplier<Map<String, String>> serviceThemeResolverSupportedBrowsers, final CasConfigurationProperties casProperties, @Qualifier(AuthenticationServiceSelectionPlan.BEAN_NAME) final AuthenticationServiceSelectionPlan authenticationRequestServiceSelectionStrategies, @Qualifier(ServicesManager.BEAN_NAME) final ServicesManager servicesManager) {
    val defaultThemeName = casProperties.getTheme().getDefaultThemeName();
    val fixedResolver = new FixedThemeResolver();
    fixedResolver.setDefaultThemeName(defaultThemeName);
    val sessionThemeResolver = new SessionThemeResolver();
    sessionThemeResolver.setDefaultThemeName(defaultThemeName);
    val tgc = casProperties.getTgc();
    val cookieThemeResolver = new CookieThemeResolver();
    cookieThemeResolver.setDefaultThemeName(defaultThemeName);
    cookieThemeResolver.setCookieDomain(tgc.getDomain());
    cookieThemeResolver.setCookieHttpOnly(tgc.isHttpOnly());
    cookieThemeResolver.setCookieMaxAge(tgc.getMaxAge());
    cookieThemeResolver.setCookiePath(tgc.getPath());
    cookieThemeResolver.setCookieSecure(tgc.isSecure());
    val serviceThemeResolver = new RegisteredServiceThemeResolver(servicesManager, authenticationRequestServiceSelectionStrategies, casProperties, serviceThemeResolverSupportedBrowsers.get().entrySet().stream().collect(Collectors.toMap(entry -> Pattern.compile(entry.getKey()), Map.Entry::getValue)));
    serviceThemeResolver.setDefaultThemeName(defaultThemeName);
    val header = new RequestHeaderThemeResolver(casProperties.getTheme().getParamName());
    header.setDefaultThemeName(defaultThemeName);
    val chainingThemeResolver = new ChainingThemeResolver();
    chainingThemeResolver.addResolver(cookieThemeResolver).addResolver(sessionThemeResolver).addResolver(header).addResolver(serviceThemeResolver).addResolver(fixedResolver);
    chainingThemeResolver.setDefaultThemeName(defaultThemeName);
    return chainingThemeResolver;
}
Also used : lombok.val(lombok.val) ChainingThemeResolver(org.apereo.cas.services.web.ChainingThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) RequestHeaderThemeResolver(org.apereo.cas.services.web.RequestHeaderThemeResolver) RegisteredServiceThemeResolver(org.apereo.cas.services.web.RegisteredServiceThemeResolver) CookieThemeResolver(org.springframework.web.servlet.theme.CookieThemeResolver) SessionThemeResolver(org.springframework.web.servlet.theme.SessionThemeResolver) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 12 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.

the class ViewResolverTests method xmlViewResolver.

@Test
@SuppressWarnings("deprecation")
public void xmlViewResolver() throws Exception {
    this.wac.registerSingleton("testBean", TestBean.class);
    this.wac.refresh();
    TestBean testBean = (TestBean) this.wac.getBean("testBean");
    org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
    vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
    vr.setApplicationContext(this.wac);
    View view1 = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(TestView.class.equals(view1.getClass())).as("Correct view class").isTrue();
    assertThat("/example1.jsp".equals(((InternalResourceView) view1).getUrl())).as("Correct URL").isTrue();
    View view2 = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(JstlView.class.equals(view2.getClass())).as("Correct view class").isTrue();
    assertThat("/example2new.jsp".equals(((InternalResourceView) view2).getUrl())).as("Correct URL").isTrue();
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view1.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
    assertThat(testBean.equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
    this.request.clearAttributes();
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view2.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
    assertThat("testvalue2".equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
}
Also used : HashMap(java.util.HashMap) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) ClassPathResource(org.springframework.core.io.ClassPathResource) TestBean(org.springframework.beans.testfixture.beans.TestBean) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) Test(org.junit.jupiter.api.Test)

Example 13 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.

the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.

private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
    this.wac.refresh();
    vr.setApplicationContext(this.wac);
    vr.setContentType("myContentType");
    vr.setRequestContextAttribute("rc");
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
    assertThat(((InternalResourceView) view).getContentType()).as("Correct textContentType").isEqualTo("myContentType");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
    assertThat(((InternalResourceView) view).getContentType()).as("Correct textContentType").isEqualTo("myContentType");
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    boolean condition = this.request.getAttribute("rc") instanceof RequestContext;
    assertThat(condition).as("Correct rc attribute").isTrue();
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    assertThat(((RedirectView) view).getApplicationContext()).as("View not initialized as bean").isSameAs(this.wac);
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) RequestContext(org.springframework.web.servlet.support.RequestContext) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)

Aggregations

FixedThemeResolver (org.springframework.web.servlet.theme.FixedThemeResolver)13 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 lombok.val (lombok.val)5 Test (org.junit.jupiter.api.Test)4 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)4 HashMap (java.util.HashMap)3 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 ServletContext (jakarta.servlet.ServletContext)2 ChainingThemeResolver (org.apereo.cas.services.web.ChainingThemeResolver)2 RegisteredServiceThemeResolver (org.apereo.cas.services.web.RegisteredServiceThemeResolver)2 RequestHeaderThemeResolver (org.apereo.cas.services.web.RequestHeaderThemeResolver)2 MockWebServer (org.apereo.cas.util.MockWebServer)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 Bean (org.springframework.context.annotation.Bean)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2