Search in sources :

Example 1 with FixedThemeResolver

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

the class ViewResolverTests method testXmlViewResolver.

@Test
public void testXmlViewResolver() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("testBean", TestBean.class);
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    TestBean testBean = (TestBean) wac.getBean("testBean");
    XmlViewResolver vr = new XmlViewResolver();
    vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
    vr.setApplicationContext(wac);
    View view1 = vr.resolveViewName("example1", Locale.getDefault());
    assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
    assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
    View view2 = vr.resolveViewName("example2", Locale.getDefault());
    assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
    assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
    ServletContext sc = new MockServletContext();
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    HttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view1.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
    assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
    request = new MockHttpServletRequest(sc);
    response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view2.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
    assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) TestBean(org.springframework.tests.sample.beans.TestBean) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with FixedThemeResolver

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

the class FreeMarkerMacroTests method setUp.

@Before
public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    // final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
    fc.afterPropertiesSet();
    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();
    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
Also used : FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 3 with FixedThemeResolver

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

the class CasThemesConfiguration method themeResolver.

@ConditionalOnMissingBean(name = "themeResolver")
@Bean
public ThemeResolver themeResolver() {
    final String defaultThemeName = casProperties.getTheme().getDefaultThemeName();
    final FixedThemeResolver fixedResolver = new FixedThemeResolver();
    fixedResolver.setDefaultThemeName(defaultThemeName);
    final SessionThemeResolver sessionThemeResolver = new SessionThemeResolver();
    sessionThemeResolver.setDefaultThemeName(defaultThemeName);
    final TicketGrantingCookieProperties tgc = casProperties.getTgc();
    final CookieThemeResolver 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());
    final RegisteredServiceThemeResolver serviceThemeResolver = new RegisteredServiceThemeResolver(servicesManager, serviceThemeResolverSupportedBrowsers(), authenticationRequestServiceSelectionStrategies, this.resourceLoader, new CasConfigurationProperties());
    serviceThemeResolver.setDefaultThemeName(defaultThemeName);
    final RequestHeaderThemeResolver header = new RequestHeaderThemeResolver();
    header.setDefaultThemeName(defaultThemeName);
    final ChainingThemeResolver chainingThemeResolver = new ChainingThemeResolver();
    chainingThemeResolver.addResolver(cookieThemeResolver).addResolver(sessionThemeResolver).addResolver(header).addResolver(serviceThemeResolver).addResolver(fixedResolver);
    chainingThemeResolver.setDefaultThemeName(defaultThemeName);
    return chainingThemeResolver;
}
Also used : ChainingThemeResolver(org.apereo.cas.services.web.ChainingThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) RequestHeaderThemeResolver(org.apereo.cas.services.web.RequestHeaderThemeResolver) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RegisteredServiceThemeResolver(org.apereo.cas.services.web.RegisteredServiceThemeResolver) TicketGrantingCookieProperties(org.apereo.cas.configuration.model.support.cookie.TicketGrantingCookieProperties) CookieThemeResolver(org.springframework.web.servlet.theme.CookieThemeResolver) SessionThemeResolver(org.springframework.web.servlet.theme.SessionThemeResolver) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with FixedThemeResolver

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

the class FreeMarkerMacroTests method setup.

@BeforeEach
public void setup() throws Exception {
    ServletContext sc = new MockServletContext();
    wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    // final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
    fc.afterPropertiesSet();
    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();
    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
Also used : FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with FixedThemeResolver

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

the class AbstractTagTests method createPageContext.

protected MockPageContext createPageContext() {
    MockServletContext sc = new MockServletContext();
    SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
    wac.setServletContext(sc);
    wac.setNamespace("test");
    wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (inDispatcherServlet()) {
        request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
        LocaleResolver lr = new AcceptHeaderLocaleResolver();
        request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
        ThemeResolver tr = new FixedThemeResolver();
        request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
        request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
    } else {
        sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    }
    return new MockPageContext(sc, request, response);
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) ThemeResolver(org.springframework.web.servlet.ThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

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