Search in sources :

Example 1 with AbstractHandlerMapping

use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method resourceHandlerMapping.

/**
	 * Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
	 * resource handlers. To configure resource handling, override
	 * {@link #addResourceHandlers}.
	 */
@Bean
public HandlerMapping resourceHandlerMapping() {
    ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext, mvcContentNegotiationManager());
    addResourceHandlers(registry);
    AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
    if (handlerMapping != null) {
        handlerMapping.setPathMatcher(mvcPathMatcher());
        handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
        handlerMapping.setInterceptors(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
        handlerMapping.setCorsConfigurations(getCorsConfigurations());
    } else {
        handlerMapping = new EmptyHandlerMapping();
    }
    return handlerMapping;
}
Also used : ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Bean(org.springframework.context.annotation.Bean)

Example 2 with AbstractHandlerMapping

use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method defaultServletHandlerMapping.

/**
	 * Return a handler mapping ordered at Integer.MAX_VALUE with a mapped
	 * default servlet handler. To configure "default" Servlet handling,
	 * override {@link #configureDefaultServletHandling}.
	 */
@Bean
public HandlerMapping defaultServletHandlerMapping() {
    DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(servletContext);
    configureDefaultServletHandling(configurer);
    AbstractHandlerMapping handlerMapping = configurer.getHandlerMapping();
    handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
    return handlerMapping;
}
Also used : AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Bean(org.springframework.context.annotation.Bean)

Example 3 with AbstractHandlerMapping

use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method emptyViewControllerHandlerMapping.

@Test
public void emptyViewControllerHandlerMapping() {
    ApplicationContext context = initContext(WebConfig.class);
    String name = "viewControllerHandlerMapping";
    AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
    assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Test(org.junit.Test)

Example 4 with AbstractHandlerMapping

use of org.springframework.web.servlet.handler.AbstractHandlerMapping in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method emptyDefaultServletHandlerMapping.

@Test
public void emptyDefaultServletHandlerMapping() {
    ApplicationContext context = initContext(WebConfig.class);
    String name = "defaultServletHandlerMapping";
    AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
    assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Test(org.junit.Test)

Example 5 with AbstractHandlerMapping

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);
    }
}
Also used : UrlBasedCorsConfigurationSource(org.springframework.web.cors.UrlBasedCorsConfigurationSource) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractHandlerMapping (org.springframework.web.servlet.handler.AbstractHandlerMapping)17 Test (org.junit.jupiter.api.Test)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Mockito.mock (org.mockito.Mockito.mock)5 Bean (org.springframework.context.annotation.Bean)5 Consumer (java.util.function.Consumer)4 AfterEach (org.junit.jupiter.api.AfterEach)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4 AnnotationConfigUtils (org.springframework.context.annotation.AnnotationConfigUtils)4 Configuration (org.springframework.context.annotation.Configuration)4 ConversionService (org.springframework.core.convert.ConversionService)4 PathMatcher (org.springframework.util.PathMatcher)4 Validator (org.springframework.validation.Validator)4 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)4 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)4 UrlPathHelper (org.springframework.web.util.UrlPathHelper)4 Test (org.junit.Test)3