org.springframework.web.servlet.HandlerExecutionChain Java Examples

The following examples show how to use org.springframework.web.servlet.HandlerExecutionChain. 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: CrossOriginTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void ambiguousProducesPreFlightRequest() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setMethod("OPTIONS");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	this.request.setRequestURI("/ambiguous-produces");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[]{"*"}, config.getAllowedMethods().toArray());
	assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray());
	assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
	assertTrue(config.getAllowCredentials());
	assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
	assertNull(config.getMaxAge());
}
 
Example #2
Source File: CorsAbstractHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void preflightRequestWithMappedCorsConfiguration() throws Exception {
	CorsConfiguration config = new CorsConfiguration();
	config.addAllowedOrigin("*");
	this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
	this.request.setMethod(RequestMethod.OPTIONS.name());
	this.request.setRequestURI("/foo");
	this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
	assertNotNull(chain);
	assertNotNull(chain.getHandler());
	assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
	config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(config.getAllowedOrigins().toArray(), new String[]{"*"});
}
 
Example #3
Source File: HandlerMethodAnnotationDetectionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example #4
Source File: MvcNamespaceTests.java    From spring-analysis-note with 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 #5
Source File: SimpleUrlHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewlineInRequest() throws Exception {
	SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
	handlerMapping.setUrlDecode(false);
	Object controller = new Object();
	Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
	urlMap.put("/*/baz", controller);
	handlerMapping.setUrlMap(urlMap);
	handlerMapping.setApplicationContext(new StaticApplicationContext());

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");

	HandlerExecutionChain hec = handlerMapping.getHandler(request);
	assertNotNull(hec);
	assertSame(controller, hec.getHandler());
}
 
Example #6
Source File: MvcNamespaceTests.java    From spring-analysis-note with 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 #7
Source File: CrossOriginTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void ambiguousHeaderPreFlightRequest() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setMethod("OPTIONS");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "header1");
	this.request.setRequestURI("/ambiguous-header");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[] {"*"}, config.getAllowedMethods().toArray());
	assertArrayEquals(new String[] {"*"}, config.getAllowedOrigins().toArray());
	assertArrayEquals(new String[] {"*"}, config.getAllowedHeaders().toArray());
	assertTrue(config.getAllowCredentials());
	assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
	assertNull(config.getMaxAge());
}
 
Example #8
Source File: CrossOriginTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void ambiguousHeaderPreFlightRequest() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setMethod("OPTIONS");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "header1");
	this.request.setRequestURI("/ambiguous-header");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[]{"*"}, config.getAllowedMethods().toArray());
	assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray());
	assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
	assertTrue(config.getAllowCredentials());
	assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
	assertNull(config.getMaxAge());
}
 
Example #9
Source File: InterceptorInjectorTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldJustReturnInterceptorsOfFrameworkIfNoTabInterceptors() throws Throwable {
    HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
    HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] {interceptorOfFramework};

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

    HandlerExecutionChain handlers =
            injector.mergeInterceptorsToTabs(proceedingJoinPoint);

    Assert.assertEquals(1, handlers.getInterceptors().length);
    Assert.assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
}
 
Example #10
Source File: MvcNamespaceTests.java    From spring4-understanding with 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 #11
Source File: HandlerMethodAnnotationDetectionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example #12
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 #13
Source File: CorsAbstractHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void actualRequestWithMappedCorsConfiguration() throws Exception {
	CorsConfiguration config = new CorsConfiguration();
	config.addAllowedOrigin("*");
	this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
	this.request.setMethod(RequestMethod.GET.name());
	this.request.setRequestURI("/foo");
	this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof SimpleHandler);
	config = getCorsConfiguration(chain, false);
	assertNotNull(config);
	assertArrayEquals(config.getAllowedOrigins().toArray(), new String[]{"*"});
}
 
Example #14
Source File: CorsAbstractHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void actualRequestWithMappedCorsConfiguration() throws Exception {
	CorsConfiguration config = new CorsConfiguration();
	config.addAllowedOrigin("*");
	this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
	this.request.setMethod(RequestMethod.GET.name());
	this.request.setRequestURI("/foo");
	this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof SimpleHandler);
	config = getCorsConfiguration(chain, false);
	assertNotNull(config);
	assertArrayEquals(config.getAllowedOrigins().toArray(), new String[]{"*"});
}
 
Example #15
Source File: MvcNamespaceTests.java    From java-technology-stack with 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 #16
Source File: CrossOriginTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void preFlightRequest() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setMethod("OPTIONS");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	this.request.setRequestURI("/default");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[]{"GET"}, config.getAllowedMethods().toArray());
	assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray());
	assertTrue(config.getAllowCredentials());
	assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
	assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
	assertEquals(new Long(1800), config.getMaxAge());
}
 
Example #17
Source File: InterceptorInjectorTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldMergeInterceptors() throws Throwable {
    HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
    HandlerInterceptor interceptorOfTab = new HandlerInterceptorSub();
    HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] {interceptorOfFramework};
    HandlerInterceptor[] interceptorsOfTab = new HandlerInterceptor[] {interceptorOfTab};

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

    InterceptorInjector injector = new InterceptorInjector();
    injector.setInterceptors(interceptorsOfFramework);

    HandlerExecutionChain handlers =
            injector.mergeInterceptorsToTabs(proceedingJoinPoint);

    Assert.assertEquals(2, handlers.getInterceptors().length);
    Assert.assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
    Assert.assertSame(interceptorOfTab, handlers.getInterceptors()[1]);
}
 
Example #18
Source File: CorsAbstractHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void preflightRequestWithCorsConfigurationSource() throws Exception {
	this.handlerMapping.setCorsConfigurationSource(new CustomCorsConfigurationSource());
	this.request.setMethod(RequestMethod.OPTIONS.name());
	this.request.setRequestURI("/foo");
	this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
	assertNotNull(chain);
	assertNotNull(chain.getHandler());
	assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray());
	assertEquals(true, config.getAllowCredentials());
}
 
Example #19
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 #20
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 #21
Source File: HandlerMappingIntrospector.java    From foremast with Apache License 2.0 6 votes vote down vote up
/**
 * Find the {@link HandlerMapping} that would handle the given request and
 * return it as a {@link MatchableHandlerMapping} that can be used to test
 * request-matching criteria.
 * <p>If the matching HandlerMapping is not an instance of
 * {@link MatchableHandlerMapping}, an IllegalStateException is raised.
 * @param request the current request
 * @return the resolved matcher, or {@code null}
 * @throws Exception if any of the HandlerMapping's raise an exception
 */
public HandlerExecutionChain getHandlerExecutionChain(HttpServletRequest request) throws Exception {
    Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
    HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
    for (HandlerMapping handlerMapping : this.handlerMappings) {
        Object handler = handlerMapping.getHandler(wrapper);
        if (handler == null) {
            continue;
        }
        if (handler instanceof HandlerExecutionChain) {
            return (HandlerExecutionChain)handler;
        }
        throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
    }
    return null;
}
 
Example #22
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getHandlerMappedInterceptors() throws Exception {
	String path = "/foo";
	HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
	MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);

	TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
	mapping.registerHandler(new TestController());
	mapping.setInterceptors(new Object[] { mappedInterceptor });
	mapping.setApplicationContext(new StaticWebApplicationContext());

	HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertSame(interceptor, chain.getInterceptors()[0]);

	chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
	assertNull(chain);
}
 
Example #23
Source File: HandlerMethodAnnotationDetectionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example #24
Source File: CrossOriginTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void ambiguousProducesPreFlightRequest() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setMethod("OPTIONS");
	this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	this.request.setRequestURI("/ambiguous-produces");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, true);
	assertNotNull(config);
	assertArrayEquals(new String[] {"*"}, config.getAllowedMethods().toArray());
	assertArrayEquals(new String[] {"*"}, config.getAllowedOrigins().toArray());
	assertArrayEquals(new String[] {"*"}, config.getAllowedHeaders().toArray());
	assertTrue(config.getAllowCredentials());
	assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
	assertNull(config.getMaxAge());
}
 
Example #25
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 #26
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 #27
Source File: CrossOriginTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customOriginDefinedViaPlaceholder() throws Exception {
	this.handlerMapping.registerHandler(new MethodLevelController());
	this.request.setRequestURI("/someOrigin");
	HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
	CorsConfiguration config = getCorsConfiguration(chain, false);
	assertNotNull(config);
	assertEquals(Arrays.asList("http://example.com"), config.getAllowedOrigins());
	assertNull(config.getAllowCredentials());
}
 
Example #28
Source File: AuthUrlHandlerMapping.java    From seppb with MIT License 5 votes vote down vote up
public String mappingUrl(HttpServletRequest request) throws Exception {
	HandlerExecutionChain handler = getHandler(request);
	String[] value = null;
	//判断是否为注解@requestMapping()
	boolean isRequestMapping = ((HandlerMethod) handler.getHandler()).getMethod().isAnnotationPresent(RequestMapping.class);
	if (isRequestMapping) {
		value = ((HandlerMethod) handler.getHandler()).getMethod().getAnnotation(RequestMapping.class).value();
		return value[0];
	} else {
		if (GET.equalsIgnoreCase(request.getMethod())) {
			value = ((HandlerMethod) handler.getHandler()).getMethod().getAnnotation(GetMapping.class).value();
		}
		if (POST.equalsIgnoreCase(request.getMethod())) {
			value = ((HandlerMethod) handler.getHandler()).getMethod().getAnnotation(PostMapping.class).value();
		}
		if (DELETE.equalsIgnoreCase(request.getMethod())) {
			value = ((HandlerMethod) handler.getHandler()).getMethod().getAnnotation(DeleteMapping.class).value();
		}
		if (PUT.equalsIgnoreCase(request.getMethod())) {
			value = ((HandlerMethod) handler.getHandler()).getMethod().getAnnotation(PutMapping.class).value();
		}
		if (value != null && value.length != 0) {
			return value[0];
		}
	}
	return null;
}
 
Example #29
Source File: InterceptorInjectorTest.java    From gocd with 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 #30
Source File: StandaloneMockMvcBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-10825
public void placeHoldersInRequestMapping() throws Exception {
	TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
	builder.addPlaceholderValue("sys.login.ajax", "/foo");
	builder.build();

	RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	HandlerExecutionChain chain = hm.getHandler(request);

	assertNotNull(chain);
	assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
}