Java Code Examples for org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

The following examples show how to use org.springframework.web.servlet.handler.SimpleUrlHandlerMapping. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: ResourceUrlProvider.java    License: MIT License 6 votes vote down vote up
protected void detectResourceHandlers(ApplicationContext appContext) {
	Map<String, SimpleUrlHandlerMapping> beans = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
	List<SimpleUrlHandlerMapping> mappings = new ArrayList<>(beans.values());
	AnnotationAwareOrderComparator.sort(mappings);

	for (SimpleUrlHandlerMapping mapping : mappings) {
		for (String pattern : mapping.getHandlerMap().keySet()) {
			Object handler = mapping.getHandlerMap().get(pattern);
			if (handler instanceof ResourceHttpRequestHandler) {
				ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
				this.handlerMap.put(pattern, resourceHandler);
			}
		}
	}

	if (this.handlerMap.isEmpty()) {
		logger.trace("No resource handling mappings found");
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandler() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 3
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 4
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml");

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example 5
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testPathMatchingHandlerMappings() throws Exception {
	loadBeanDefinitions("mvc-config-path-matching-mappings.xml");

	RequestMappingHandlerMapping requestMapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(requestMapping);
	assertEquals(TestPathHelper.class, requestMapping.getUrlPathHelper().getClass());
	assertEquals(TestPathMatcher.class, requestMapping.getPathMatcher().getClass());

	SimpleUrlHandlerMapping viewController = appContext.getBean(VIEWCONTROLLER_BEAN_NAME, SimpleUrlHandlerMapping.class);
	assertNotNull(viewController);
	assertEquals(TestPathHelper.class, viewController.getUrlPathHelper().getClass());
	assertEquals(TestPathMatcher.class, viewController.getPathMatcher().getClass());

	for (SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) {
		assertNotNull(handlerMapping);
		assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
		assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: ResourceUrlProvider.java    License: MIT License 6 votes vote down vote up
protected void detectResourceHandlers(ApplicationContext appContext) {
	Map<String, SimpleUrlHandlerMapping> beans = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
	List<SimpleUrlHandlerMapping> mappings = new ArrayList<>(beans.values());
	AnnotationAwareOrderComparator.sort(mappings);

	for (SimpleUrlHandlerMapping mapping : mappings) {
		for (String pattern : mapping.getHandlerMap().keySet()) {
			Object handler = mapping.getHandlerMap().get(pattern);
			if (handler instanceof ResourceHttpRequestHandler) {
				ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
				this.handlerMap.put(pattern, resourceHandler);
			}
		}
	}

	if (this.handlerMap.isEmpty()) {
		logger.trace("No resource handling mappings found");
	}
}
 
Example 7
@Test
public void enable() throws Exception {
	configurer.enable();
	SimpleUrlHandlerMapping getHandlerMapping = getHandlerMapping();
	SimpleUrlHandlerMapping handlerMapping = getHandlerMapping;
	DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");

	assertNotNull(handler);
	assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());

	handler.handleRequest(new MockHttpServletRequest(), response);

	String expected = "default";
	assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
	assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
 
Example 8
private BeanDefinition registerHandlerMapping(ParserContext context, @Nullable Object source) {
	if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
		return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
	}
	RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
	context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));

	beanDef.setSource(source);
	beanDef.getPropertyValues().add("order", "1");
	beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
	beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
	RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
	beanDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);

	return beanDef;
}
 
Example 9
Source Project: java-technology-stack   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandler() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 10
Source Project: java-technology-stack   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 11
Source Project: java-technology-stack   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml");

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example 12
Source Project: java-technology-stack   Source File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testPathMatchingHandlerMappings() throws Exception {
	loadBeanDefinitions("mvc-config-path-matching-mappings.xml");

	RequestMappingHandlerMapping requestMapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(requestMapping);
	assertEquals(TestPathHelper.class, requestMapping.getUrlPathHelper().getClass());
	assertEquals(TestPathMatcher.class, requestMapping.getPathMatcher().getClass());

	SimpleUrlHandlerMapping viewController = appContext.getBean(VIEWCONTROLLER_BEAN_NAME, SimpleUrlHandlerMapping.class);
	assertNotNull(viewController);
	assertEquals(TestPathHelper.class, viewController.getUrlPathHelper().getClass());
	assertEquals(TestPathMatcher.class, viewController.getPathMatcher().getClass());

	for (SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) {
		assertNotNull(handlerMapping);
		assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
		assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
	}
}
 
Example 13
private BeanDefinition registerHandlerMapping(ParserContext context, Object source) {
	if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
		return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
	}
	RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
	context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));

	beanDef.setSource(source);
	beanDef.getPropertyValues().add("order", "1");
	beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
	beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
	RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
	beanDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);

	return beanDef;
}
 
Example 14
Source Project: spring4-understanding   Source File: ResourceUrlProvider.java    License: Apache License 2.0 6 votes vote down vote up
protected void detectResourceHandlers(ApplicationContext appContext) {
	logger.debug("Looking for resource handler mappings");

	Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
	List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<SimpleUrlHandlerMapping>(map.values());
	AnnotationAwareOrderComparator.sort(handlerMappings);

	for (SimpleUrlHandlerMapping hm : handlerMappings) {
		for (String pattern : hm.getHandlerMap().keySet()) {
			Object handler = hm.getHandlerMap().get(pattern);
			if (handler instanceof ResourceHttpRequestHandler) {
				ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
				if (logger.isDebugEnabled()) {
					logger.debug("Found resource handler mapping: URL pattern=\"" + pattern + "\", " +
							"locations=" + resourceHandler.getLocations() + ", " +
							"resolvers=" + resourceHandler.getResourceResolvers());
				}
				this.handlerMap.put(pattern, resourceHandler);
			}
		}
	}
}
 
Example 15
private BeanDefinition registerHandlerMapping(ParserContext context, Object source) {
	if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
		return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
	}
	RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
	context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));

	beanDef.setSource(source);
	beanDef.getPropertyValues().add("order", "1");
	beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
	beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
	RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
	beanDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);

	return beanDef;
}
 
Example 16
Source Project: spring4-understanding   Source File: MvcNamespaceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultServletHandler() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet.xml", 6);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 17
Source Project: spring4-understanding   Source File: MvcNamespaceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml", 6);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 18
Source Project: spring4-understanding   Source File: MvcNamespaceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml", 7);

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example 19
@Bean
public SimpleUrlHandlerMapping podcastMapping(PodcastController podcastController) {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();

    mapping.setAlwaysUseFullPath(true);

    Properties properties = new Properties();
    properties.put("/podcast/**", podcastController);
    mapping.setMappings(properties);

    return mapping;
}
 
Example 20
/**
 * Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
 * {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
 * or {@code null} if default servlet handling was not been enabled.
 * @since 4.3.12
 */
@Nullable
protected SimpleUrlHandlerMapping buildHandlerMapping() {
	if (this.handler == null) {
		return null;
	}

	SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
	handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
	handlerMapping.setOrder(Integer.MAX_VALUE);
	return handlerMapping;
}
 
Example 21
Source Project: tutorials   Source File: FaviconConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public SimpleUrlHandlerMapping myFaviconHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Integer.MIN_VALUE);
    mapping.setUrlMap(Collections.singletonMap("/favicon.ico", faviconRequestHandler()));
    return mapping;
}
 
Example 22
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
	Object source = parserContext.extractSource(element);

	String defaultServletName = element.getAttribute("default-servlet-name");
	RootBeanDefinition defaultServletHandlerDef = new RootBeanDefinition(DefaultServletHttpRequestHandler.class);
	defaultServletHandlerDef.setSource(source);
	defaultServletHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	if (StringUtils.hasText(defaultServletName)) {
		defaultServletHandlerDef.getPropertyValues().add("defaultServletName", defaultServletName);
	}
	String defaultServletHandlerName = parserContext.getReaderContext().generateBeanName(defaultServletHandlerDef);
	parserContext.getRegistry().registerBeanDefinition(defaultServletHandlerName, defaultServletHandlerDef);
	parserContext.registerComponent(new BeanComponentDefinition(defaultServletHandlerDef, defaultServletHandlerName));

	Map<String, String> urlMap = new ManagedMap<>();
	urlMap.put("/**", defaultServletHandlerName);

	RootBeanDefinition handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	handlerMappingDef.setSource(source);
	handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	handlerMappingDef.getPropertyValues().add("urlMap", urlMap);

	String handlerMappingBeanName = parserContext.getReaderContext().generateBeanName(handlerMappingDef);
	parserContext.getRegistry().registerBeanDefinition(handlerMappingBeanName, handlerMappingDef);
	parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingBeanName));

	// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
	MvcNamespaceUtils.registerDefaultComponents(parserContext, source);

	return null;
}
 
Example 23
Source Project: spring-analysis-note   Source File: ResourceUrlProviderTests.java    License: MIT License 5 votes vote down vote up
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<>();
	handlerMap.put("/resources/**", handler);
	SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
	hm.setUrlMap(handlerMap);
	return hm;
}
 
Example 24
Source Project: gocd   Source File: InterceptorInjectorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotChangeHandler() throws Throwable {
    SimpleUrlHandlerMapping handler = new SimpleUrlHandlerMapping();

    ProceedingJoinPoint proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    when(proceedingJoinPoint.proceed()).thenReturn(new HandlerExecutionChain(handler, null));
    InterceptorInjector injector = new InterceptorInjector();

    HandlerExecutionChain handlers =
            injector.mergeInterceptorsToTabs(proceedingJoinPoint);

    Assert.assertSame(handler, handlers.getHandler());
}
 
Example 25
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testResourcesWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-resources-optional-attrs.xml");

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(5, mapping.getOrder());
	assertNotNull(mapping.getUrlMap().get("/resources/**"));

	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertEquals(3600, handler.getCacheSeconds());
}
 
Example 26
@Bean
public SimpleUrlHandlerMapping handlerMapping() {

    SockJsService sockJsService = new DefaultSockJsService(taskScheduler());

    Map<String, Object> urlMap = new HashMap<String, Object>();
    urlMap.put("/game/websocket/**", new SockJsHttpRequestHandler(sockJsService, gameHandler));

    SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
    hm.setUrlMap(urlMap);
    return hm;
}
 
Example 27
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml");

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertNotNull(mapping.getUrlMap().get("/resources/**"));
	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	assertThat(handler.getCacheControl().getHeaderValue(),
			Matchers.equalTo(CacheControl.maxAge(1, TimeUnit.HOURS)
					.sMaxAge(30, TimeUnit.MINUTES).cachePublic().getHeaderValue()));

	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers, Matchers.hasSize(3));
	assertThat(resolvers.get(0), Matchers.instanceOf(VersionResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.instanceOf(EncodedResourceResolver.class));
	assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class));

	VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(0);
	assertThat(versionResolver.getStrategyMap().get("/**/*.js"),
			Matchers.instanceOf(FixedVersionStrategy.class));
	assertThat(versionResolver.getStrategyMap().get("/**"),
			Matchers.instanceOf(ContentVersionStrategy.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(2));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
 
Example 28
@Test
public void enable() throws Exception {
	configurer.enable();
	SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
	DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");

	assertNotNull(handler);
	assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());

	handler.handleRequest(new MockHttpServletRequest(), response);

	String expected = "default";
	assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
	assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
 
Example 29
@Test
public void enableWithServletName() throws Exception {
	configurer.enable("defaultServlet");
	SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
	DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");

	assertNotNull(handler);
	assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());

	handler.handleRequest(new MockHttpServletRequest(), response);

	String expected = "defaultServlet";
	assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
	assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
 
Example 30
@Test
public void contentNegotiation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
	NativeWebRequest webRequest = new ServletWebRequest(request);

	RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping(
			this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
			this.config.mvcResourceUrlProvider());
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.xml");
	assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.rss");
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.atom");
	assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo");
	request.setParameter("f", "json");
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/resources/foo.gif");
	SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping(
			this.config.mvcUrlPathHelper(), this.config.mvcPathMatcher(),
			this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
			this.config.mvcResourceUrlProvider());
	handlerMapping.setApplicationContext(this.context);
	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);
	ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());
}