org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor Java Examples

The following examples show how to use org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor. 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 File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test(expected = TypeMismatchException.class)
public void testCustomConversionService() throws Exception {
	loadBeanDefinitions("mvc-config-custom-conversion-service.xml");

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	// default web binding initializer behavior test
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setRequestURI("/accounts/12345");
	request.addParameter("date", "2009-10-31");
	MockHttpServletResponse response = new MockHttpServletResponse();

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(1, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
	interceptor.preHandle(request, response, handler);
	assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName()));

	RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
	assertNotNull(adapter);
	adapter.handle(request, response, handlerMethod);
}
 
Example #2
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestMappingHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ScopedController.class, ScopedProxyController.class);
	RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
	assertEquals(0, handlerMapping.getOrder());

	HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass());

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scoped"));
	assertNotNull("HandlerExecutionChain for '/scoped' mapping should not be null.", chain);

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scopedProxy"));
	assertNotNull("HandlerExecutionChain for '/scopedProxy' mapping should not be null.", chain);
}
 
Example #3
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanDecoration() throws Exception {
	loadBeanDefinitions("mvc-config-bean-decoration.xml", 16);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
	LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1];
	assertEquals("lang", interceptor.getParamName());
	ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2];
	assertEquals("style", interceptor2.getParamName());
}
 
Example #4
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test(expected = TypeMismatchException.class)
public void testCustomConversionService() throws Exception {
	loadBeanDefinitions("mvc-config-custom-conversion-service.xml", 14);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	// default web binding initializer behavior test
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setRequestURI("/accounts/12345");
	request.addParameter("date", "2009-10-31");
	MockHttpServletResponse response = new MockHttpServletResponse();

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(1, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
	interceptor.preHandle(request, response, handler);
	assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName()));

	RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
	assertNotNull(adapter);
	adapter.handle(request, response, handlerMethod);
}
 
Example #5
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestMappingHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ScopedController.class, ScopedProxyController.class);
	RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
	assertEquals(0, handlerMapping.getOrder());

	HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass());

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scoped"));
	assertNotNull("HandlerExecutionChain for '/scoped' mapping should not be null.", chain);

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scopedProxy"));
	assertNotNull("HandlerExecutionChain for '/scopedProxy' mapping should not be null.", chain);
}
 
Example #6
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBeanDecoration() throws Exception {
	loadBeanDefinitions("mvc-config-bean-decoration.xml");

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
	LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1];
	assertEquals("lang", interceptor.getParamName());
	ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2];
	assertEquals("style", interceptor2.getParamName());
}
 
Example #7
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test(expected = TypeMismatchException.class)
public void testCustomConversionService() throws Exception {
	loadBeanDefinitions("mvc-config-custom-conversion-service.xml");

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	// default web binding initializer behavior test
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setRequestURI("/accounts/12345");
	request.addParameter("date", "2009-10-31");
	MockHttpServletResponse response = new MockHttpServletResponse();

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(1, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
	interceptor.preHandle(request, response, handler);
	assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName()));

	RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
	assertNotNull(adapter);
	adapter.handle(request, response, handlerMethod);
}
 
Example #8
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestMappingHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ScopedController.class, ScopedProxyController.class);
	RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
	assertEquals(0, handlerMapping.getOrder());

	HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass());

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scoped"));
	assertNotNull("HandlerExecutionChain for '/scoped' mapping should not be null.", chain);

	chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scopedProxy"));
	assertNotNull("HandlerExecutionChain for '/scopedProxy' mapping should not be null.", chain);
}
 
Example #9
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBeanDecoration() throws Exception {
	loadBeanDefinitions("mvc-config-bean-decoration.xml");

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
	LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1];
	assertEquals("lang", interceptor.getParamName());
	ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2];
	assertEquals("style", interceptor2.getParamName());
}
 
Example #10
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #11
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #12
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Provide access to the shared handler interceptors used to configure
 * {@link HandlerMapping} instances with.
 * <p>This method cannot be overridden; use {@link #addInterceptors} instead.
 */
protected final Object[] getInterceptors(
		FormattingConversionService mvcConversionService,
		ResourceUrlProvider mvcResourceUrlProvider) {
	if (this.interceptors == null) {
		InterceptorRegistry registry = new InterceptorRegistry();
		addInterceptors(registry);
		registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService));
		registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider));
		this.interceptors = registry.getInterceptors();
	}
	return this.interceptors.toArray();
}
 
Example #13
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInterceptors() throws Exception {
	loadBeanDefinitions("mvc-config-interceptors.xml");

	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);
}
 
Example #14
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterceptors() throws Exception {
	loadBeanDefinitions("mvc-config-interceptors.xml", 21);

	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(5, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof WebRequestHandlerInterceptorAdapter);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	assertTrue(chain.getInterceptors()[4] instanceof UserRoleAuthorizationInterceptor);

	request.setRequestURI("/admin/users");
	chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);

	request.setRequestURI("/logged/accounts/12345");
	chain = mapping.getHandler(request);
	assertEquals(5, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);

	request.setRequestURI("/foo/logged");
	chain = mapping.getHandler(request);
	assertEquals(5, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);
}
 
Example #15
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Provide access to the shared handler interceptors used to configure
 * {@link HandlerMapping} instances with. This method cannot be overridden,
 * use {@link #addInterceptors(InterceptorRegistry)} instead.
 */
protected final Object[] getInterceptors() {
	if (this.interceptors == null) {
		InterceptorRegistry registry = new InterceptorRegistry();
		addInterceptors(registry);
		registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
		registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
		this.interceptors = registry.getInterceptors();
	}
	return this.interceptors.toArray();
}
 
Example #16
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Provide access to the shared handler interceptors used to configure
 * {@link HandlerMapping} instances with. This method cannot be overridden,
 * use {@link #addInterceptors(InterceptorRegistry)} instead.
 */
protected final Object[] getInterceptors() {
	if (this.interceptors == null) {
		InterceptorRegistry registry = new InterceptorRegistry();
		addInterceptors(registry);
		registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
		registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
		this.interceptors = registry.getInterceptors();
	}
	return this.interceptors.toArray();
}
 
Example #17
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #18
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInterceptors() throws Exception {
	loadBeanDefinitions("mvc-config-interceptors.xml");

	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);
}
 
Example #19
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Provide access to the shared handler interceptors used to configure
 * {@link HandlerMapping} instances with.
 * <p>This method cannot be overridden; use {@link #addInterceptors} instead.
 */
protected final Object[] getInterceptors() {
	if (this.interceptors == null) {
		InterceptorRegistry registry = new InterceptorRegistry();
		addInterceptors(registry);
		registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
		registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
		this.interceptors = registry.getInterceptors();
	}
	return this.interceptors.toArray();
}
 
Example #20
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
	loadBeanDefinitions("mvc-config-view-controllers.xml");

	SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
	SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setRequestURI("/myapp/app/bar");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
	HandlerExecutionChain chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("baz", mv2.getViewName());

	request.setRequestURI("/myapp/app/");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());

	request.setRequestURI("/myapp/");
	request.setContextPath("/myapp");
	request.setServletPath("/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());
}
 
Example #21
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

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

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());

	SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(resourceMapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
	List<Class<?>> interceptors = beans.values().stream()
			.map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass())
			.collect(Collectors.toList());
	assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class,
			ResourceUrlProviderExposingInterceptor.class));

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

	HandlerExecutionChain chain = resourceMapping.getHandler(request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #22
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
	loadBeanDefinitions("mvc-config-view-controllers.xml");

	SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
	SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setRequestURI("/myapp/app/bar");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
	HandlerExecutionChain chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("baz", mv2.getViewName());

	request.setRequestURI("/myapp/app/");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());

	request.setRequestURI("/myapp/");
	request.setContextPath("/myapp");
	request.setServletPath("/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());
}
 
Example #23
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

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

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());

	SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(resourceMapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
	List<Class<?>> interceptors = beans.values().stream()
			.map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass())
			.collect(Collectors.toList());
	assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class,
			ResourceUrlProviderExposingInterceptor.class));

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

	HandlerExecutionChain chain = resourceMapping.getHandler(request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #24
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
	loadBeanDefinitions("mvc-config-view-controllers.xml", 19);

	SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
	SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setRequestURI("/myapp/app/bar");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
	HandlerExecutionChain chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("baz", mv2.getViewName());

	request.setRequestURI("/myapp/app/");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());

	request.setRequestURI("/myapp/");
	request.setContextPath("/myapp");
	request.setServletPath("/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());
}