Search in sources :

Example 1 with LocaleChangeInterceptor

use of org.springframework.web.servlet.i18n.LocaleChangeInterceptor in project spring-framework by spring-projects.

the class ComplexWebApplicationContext method refresh.

@Override
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", 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");
    List<RuntimeBeanReference> mappedHandlers = new ManagedList<>();
    mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
    pvs.add("mappedHandlers", mappedHandlers);
    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) ManagedList(org.springframework.beans.factory.support.ManagedList) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 2 with LocaleChangeInterceptor

use of org.springframework.web.servlet.i18n.LocaleChangeInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testInterceptors.

@Test
public void testInterceptors() throws Exception {
    loadBeanDefinitions("mvc-config-interceptors.xml", 18);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    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);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof UserRoleAuthorizationInterceptor);
    request.setRequestURI("/admin/users");
    chain = mapping.getHandler(request);
    assertEquals(2, chain.getInterceptors().length);
    request.setRequestURI("/logged/accounts/12345");
    chain = mapping.getHandler(request);
    assertEquals(3, chain.getInterceptors().length);
    request.setRequestURI("/foo/logged");
    chain = mapping.getHandler(request);
    assertEquals(3, chain.getInterceptors().length);
}
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.mock.web.test.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.Test)

Example 3 with LocaleChangeInterceptor

use of org.springframework.web.servlet.i18n.LocaleChangeInterceptor in project cas by apereo.

the class CasManagementWebAppConfiguration method localeChangeInterceptor.

@RefreshScope
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    final LocaleChangeInterceptor bean = new LocaleChangeInterceptor();
    bean.setParamName(this.casProperties.getLocale().getParamName());
    return bean;
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with LocaleChangeInterceptor

use of org.springframework.web.servlet.i18n.LocaleChangeInterceptor in project nikita-noark5-core by HiOA-ABI.

the class AppWebMvcConfiguration method localeChangeInterceptor.

/**
     *  Set interceptor so that GUI portion of the applications
     *  can return pages with different languages. Take a look
     *  for a files called message_LANG.properties under the
     *  /src/main/resources/ folder to see which languages are
     *  supported at runtime.
     *
     *  TODO: Check how this deals with accept-language in HTTP
     *  header
     *  lang=no, lang=en
     * @return LocaleChangeInterceptor
     */
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 5 with LocaleChangeInterceptor

use of org.springframework.web.servlet.i18n.LocaleChangeInterceptor in project cas by apereo.

the class CasWebflowContextConfiguration method localeChangeInterceptor.

@RefreshScope
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    final LocaleChangeInterceptor bean = new LocaleChangeInterceptor();
    bean.setParamName(this.casProperties.getLocale().getParamName());
    return bean;
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) CipherBean(org.cryptacular.bean.CipherBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)13 Bean (org.springframework.context.annotation.Bean)7 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)5 Test (org.junit.Test)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)4 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)4 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 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)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 ArrayList (java.util.ArrayList)1 CipherBean (org.cryptacular.bean.CipherBean)1 Before (org.junit.Before)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1