use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method handlerMappings.
@Test
public void handlerMappings() throws Exception {
RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
rmHandlerMapping.setApplicationContext(this.context);
rmHandlerMapping.afterPropertiesSet();
assertThat(rmHandlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(rmHandlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
assertThat(chain).isNotNull();
HandlerInterceptor[] interceptors = chain.getInterceptors();
assertThat(interceptors).isNotNull();
assertThat(interceptors.length).isEqualTo(4);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
assertThat(interceptors[1].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[2].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
Map<RequestMappingInfo, HandlerMethod> map = rmHandlerMapping.getHandlerMethods();
assertThat(map.size()).isEqualTo(2);
RequestMappingInfo info = map.entrySet().stream().filter(entry -> entry.getValue().getBeanType().equals(UserController.class)).findFirst().orElseThrow(() -> new AssertionError("UserController bean not found")).getKey();
assertThat(info.getPatternValues()).isEqualTo(Collections.singleton("/api/user/{id}"));
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping(this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(1);
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo((Integer.MAX_VALUE - 1));
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
interceptors = chain.getInterceptors();
assertThat(interceptors.length).as(Arrays.toString(interceptors)).isEqualTo(5);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
// PathExposingHandlerInterceptor at interceptors[1]
assertThat(interceptors[2].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[4].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(Integer.MAX_VALUE);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
}
use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.
the class MvcNamespaceTests method testCors.
@Test
public void testCors() {
loadBeanDefinitions("mvc-config-cors.xml");
String[] beanNames = appContext.getBeanNamesForType(AbstractHandlerMapping.class);
assertThat(beanNames.length).isEqualTo(2);
for (String beanName : beanNames) {
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) appContext.getBean(beanName);
assertThat(handlerMapping).isNotNull();
DirectFieldAccessor accessor = new DirectFieldAccessor(handlerMapping);
Map<String, CorsConfiguration> configs = ((UrlBasedCorsConfigurationSource) accessor.getPropertyValue("corsConfigurationSource")).getCorsConfigurations();
assertThat(configs).isNotNull();
assertThat(configs.size()).isEqualTo(2);
CorsConfiguration config = configs.get("/api/**");
assertThat(config).isNotNull();
assertThat(config.getAllowedOrigins().toArray()).isEqualTo(new String[] { "https://domain1.com", "https://domain2.com" });
assertThat(config.getAllowedOriginPatterns().toArray()).isEqualTo(new String[] { "http://*.domain.com" });
assertThat(config.getAllowedMethods().toArray()).isEqualTo(new String[] { "GET", "PUT" });
assertThat(config.getAllowedHeaders().toArray()).isEqualTo(new String[] { "header1", "header2", "header3" });
assertThat(config.getExposedHeaders().toArray()).isEqualTo(new String[] { "header1", "header2" });
assertThat(config.getAllowCredentials()).isFalse();
assertThat(config.getMaxAge()).isEqualTo(Long.valueOf(123));
config = configs.get("/resources/**");
assertThat(config.getAllowedOrigins().toArray()).isEqualTo(new String[] { "https://domain1.com" });
assertThat(config.getAllowedMethods().toArray()).isEqualTo(new String[] { "GET", "HEAD", "POST" });
assertThat(config.getAllowedHeaders().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getExposedHeaders()).isNull();
assertThat(config.getAllowCredentials()).isNull();
assertThat(config.getMaxAge()).isEqualTo(1800L);
}
}
use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.
the class MvcNamespaceTests method testCorsMinimal.
@Test
public void testCorsMinimal() throws Exception {
loadBeanDefinitions("mvc-config-cors-minimal.xml");
String[] beanNames = appContext.getBeanNamesForType(AbstractHandlerMapping.class);
assertThat(beanNames.length).isEqualTo(2);
for (String beanName : beanNames) {
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) appContext.getBean(beanName);
assertThat(handlerMapping).isNotNull();
DirectFieldAccessor accessor = new DirectFieldAccessor(handlerMapping);
Map<String, CorsConfiguration> configs = ((UrlBasedCorsConfigurationSource) accessor.getPropertyValue("corsConfigurationSource")).getCorsConfigurations();
assertThat(configs).isNotNull();
assertThat(configs.size()).isEqualTo(1);
CorsConfiguration config = configs.get("/**");
assertThat(config).isNotNull();
assertThat(config.getAllowedOrigins().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getAllowedMethods().toArray()).isEqualTo(new String[] { "GET", "HEAD", "POST" });
assertThat(config.getAllowedHeaders().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getExposedHeaders()).isNull();
assertThat(config.getAllowCredentials()).isNull();
assertThat(config.getMaxAge()).isEqualTo(Long.valueOf(1800));
}
}
use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.
the class DelegatingWebMvcConfigurationIntegrationTests method resourceHandlerMappingUsesMvcInfrastructureByDefault.
@Test
void resourceHandlerMappingUsesMvcInfrastructureByDefault() {
load(context -> context.registerBean(ResourceHandlerConfiguration.class));
AbstractHandlerMapping handlerMapping = this.context.getBean("resourceHandlerMapping", AbstractHandlerMapping.class);
assertThat(handlerMapping.getPathMatcher()).isSameAs(this.context.getBean("mvcPathMatcher"));
assertThat(handlerMapping.getUrlPathHelper()).isSameAs(this.context.getBean("mvcUrlPathHelper"));
}
use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method viewControllerHandlerMapping.
/**
* Return a handler mapping ordered at 1 to map URL paths directly to
* view names. To configure view controllers, override
* {@link #addViewControllers}.
*/
@Bean
public HandlerMapping viewControllerHandlerMapping() {
ViewControllerRegistry registry = new ViewControllerRegistry();
registry.setApplicationContext(this.applicationContext);
addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
return handlerMapping;
}
Aggregations