Search in sources :

Example 1 with HandlerAdapter

use of org.springframework.web.servlet.HandlerAdapter in project spring-integration by spring-projects.

the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.

@Test
public void testIntegrationGraphControllerParser() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
    HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
    HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo");
    request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    Object handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
    assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
    context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
    request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo/refresh");
    response = new MockHttpServletResponse();
    executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HandlerAdapter(org.springframework.web.servlet.HandlerAdapter) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with HandlerAdapter

use of org.springframework.web.servlet.HandlerAdapter in project openmrs-core by openmrs.

the class WebTestHelper method handle.

/**
 * Handles the request with a proper controller.
 *
 * @param request
 * @return
 * @throws Exception
 */
public Response handle(final HttpServletRequest request) throws Exception {
    if (handlerMappings == null || handlerAdapters == null) {
        throw new UnsupportedOperationException("The web context is not configured!");
    }
    // Simulate a request with a fresh Hibernate session
    Context.flushSession();
    Context.clearSession();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = null;
    HandlerExecutionChain handlerChain = null;
    for (HandlerMapping handlerMapping : handlerMappings) {
        handlerChain = handlerMapping.getHandler(request);
        if (handlerChain != null) {
            break;
        }
    }
    Assert.assertNotNull("The requested URI has no mapping: " + request.getRequestURI(), handlerChain);
    boolean supported = false;
    for (HandlerAdapter handlerAdapter : handlerAdapters) {
        final Object handler = handlerChain.getHandler();
        if (handlerAdapter.supports(handler)) {
            Assert.assertFalse("The requested URI has more than one handler: " + request.getRequestURI(), supported);
            modelAndView = handlerAdapter.handle(request, response, handler);
            supported = true;
        }
    }
    Assert.assertTrue("The requested URI has no handlers: " + request.getRequestURI(), supported);
    return new Response(response, request.getSession(), modelAndView);
}
Also used : MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HandlerAdapter(org.springframework.web.servlet.HandlerAdapter) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 HandlerAdapter (org.springframework.web.servlet.HandlerAdapter)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 HandlerMapping (org.springframework.web.servlet.HandlerMapping)2 Test (org.junit.Test)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1