Search in sources :

Example 1 with ThemeChangeInterceptor

use of org.springframework.web.servlet.theme.ThemeChangeInterceptor in project spring-thymeleaf-simplefinance by heitkergm.

the class SpringWebConfig method addInterceptors.

/*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
     * #addInterceptors(org.springframework.web.servlet.config.annotation.
     * InterceptorRegistry)
     */
@Override
public void addInterceptors(final InterceptorRegistry registry) {
    registry.addInterceptor(new LocaleChangeInterceptor());
    registry.addInterceptor(new ThemeChangeInterceptor());
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor)

Example 2 with ThemeChangeInterceptor

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

the class MvcNamespaceTests method testInterceptors.

@Test
public void testInterceptors() throws Exception {
    loadBeanDefinitions("mvc-config-interceptors.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    mapping.setDefaultHandler(handlerMethod);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    request.setRequestURI("/accounts/12345");
    request.addParameter("locale", "en");
    request.addParameter("theme", "green");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof UserRoleAuthorizationInterceptor).isTrue();
    request.setRequestURI("/admin/users");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(2);
    request.setRequestURI("/logged/accounts/12345");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
    request.setRequestURI("/foo/logged");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 3 with ThemeChangeInterceptor

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

the class ComplexWebApplicationContext method refresh.

@Override
@SuppressWarnings("deprecation")
public void refresh() throws BeansException {
    registerSingleton(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, SessionLocaleResolver.class);
    registerSingleton(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, SessionThemeResolver.class);
    LocaleChangeInterceptor interceptor1 = new LocaleChangeInterceptor();
    LocaleChangeInterceptor interceptor2 = new LocaleChangeInterceptor();
    interceptor2.setParamName("locale2");
    ThemeChangeInterceptor interceptor3 = new ThemeChangeInterceptor();
    ThemeChangeInterceptor interceptor4 = new ThemeChangeInterceptor();
    interceptor4.setParamName("theme2");
    UserRoleAuthorizationInterceptor interceptor5 = new UserRoleAuthorizationInterceptor();
    interceptor5.setAuthorizedRoles("role1", "role2");
    List<Object> interceptors = new ArrayList<>();
    interceptors.add(interceptor5);
    interceptors.add(interceptor1);
    interceptors.add(interceptor2);
    interceptors.add(interceptor3);
    interceptors.add(interceptor4);
    interceptors.add(new MyHandlerInterceptor1());
    interceptors.add(new MyHandlerInterceptor2());
    interceptors.add(new MyWebRequestInterceptor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("mappings", "/view.do=viewHandler\n/locale.do=localeHandler\nloc.do=anotherLocaleHandler");
    pvs.add("interceptors", interceptors);
    registerSingleton("myUrlMapping1", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/form.do=localeHandler\n/unknown.do=unknownHandler\nservlet.do=myServlet");
    pvs.add("order", "2");
    registerSingleton("myUrlMapping2", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/head.do=headController\n" + "body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
    pvs.add("order", "1");
    registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
    registerSingleton("myDummyAdapter", MyDummyAdapter.class);
    registerSingleton("myHandlerAdapter", MyHandlerAdapter.class);
    registerSingleton("standardHandlerAdapter", SimpleControllerHandlerAdapter.class);
    registerSingleton("noviewController", NoViewController.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", 0);
    pvs.add("basename", "org.springframework.web.servlet.complexviews");
    registerSingleton("viewResolver", org.springframework.web.servlet.view.ResourceBundleViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("suffix", ".jsp");
    registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("viewName", "form");
    registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
    registerSingleton("localeHandler", ComplexLocaleChecker.class);
    registerSingleton("anotherLocaleHandler", ComplexLocaleChecker.class);
    registerSingleton("unknownHandler", Object.class);
    registerSingleton("headController", HeadController.class);
    registerSingleton("bodyController", BodyController.class);
    registerSingleton("servletPostProcessor", SimpleServletPostProcessor.class);
    registerSingleton("handlerAdapter", SimpleServletHandlerAdapter.class);
    registerSingleton("myServlet", MyServlet.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", "1");
    pvs.add("exceptionMappings", "java.lang.IllegalAccessException=failed2\n" + "ServletRequestBindingException=failed3");
    pvs.add("defaultErrorView", "failed0");
    registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("order", "0");
    pvs.add("exceptionMappings", "java.lang.Exception=failed1");
    pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler")));
    pvs.add("defaultStatusCode", "500");
    pvs.add("defaultErrorView", "failed2");
    registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
    registerSingleton("multipartResolver", MockMultipartResolver.class);
    registerSingleton("testListener", TestApplicationListener.class);
    addMessage("test", Locale.ENGLISH, "test message");
    addMessage("test", Locale.CANADA, "Canadian & test message");
    super.refresh();
}
Also used : UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) ArrayList(java.util.ArrayList) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 4 with ThemeChangeInterceptor

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

the class CasWebAppConfiguration method themeChangeInterceptor.

@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
@Bean
public ThemeChangeInterceptor themeChangeInterceptor(final CasConfigurationProperties casProperties) {
    val bean = new ThemeChangeInterceptor();
    bean.setParamName(casProperties.getTheme().getParamName());
    return bean;
}
Also used : lombok.val(lombok.val) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 5 with ThemeChangeInterceptor

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

the class CasWebAppConfiguration method themeChangeInterceptor.

@RefreshScope
@Bean
public ThemeChangeInterceptor themeChangeInterceptor() {
    final ThemeChangeInterceptor bean = new ThemeChangeInterceptor();
    bean.setParamName(casProperties.getTheme().getParamName());
    return bean;
}
Also used : ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ServletListenerRegistrationBean(org.springframework.boot.web.servlet.ServletListenerRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)9 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)6 Test (org.junit.jupiter.api.Test)4 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)4 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 Bean (org.springframework.context.annotation.Bean)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 RefreshScope (org.springframework.cloud.context.config.annotation.RefreshScope)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 UserRoleAuthorizationInterceptor (org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor)2 SimpleControllerHandlerAdapter (org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 ArrayList (java.util.ArrayList)1 lombok.val (lombok.val)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 ServletListenerRegistrationBean (org.springframework.boot.web.servlet.ServletListenerRegistrationBean)1