Search in sources :

Example 1 with ServerResponse

use of org.springframework.web.servlet.function.ServerResponse in project spring-framework by spring-projects.

the class HandlerFunctionAdapter method handle.

@Nullable
@Override
public ModelAndView handle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler) throws Exception {
    WebAsyncManager asyncManager = getWebAsyncManager(servletRequest, servletResponse);
    ServerRequest serverRequest = getServerRequest(servletRequest);
    ServerResponse serverResponse;
    if (asyncManager.hasConcurrentResult()) {
        serverResponse = handleAsync(asyncManager);
    } else {
        HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
        serverResponse = handlerFunction.handle(serverRequest);
    }
    if (serverResponse != null) {
        return serverResponse.writeTo(servletRequest, servletResponse, new ServerRequestContext(serverRequest));
    } else {
        return null;
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) ServerRequest(org.springframework.web.servlet.function.ServerRequest) Nullable(org.springframework.lang.Nullable)

Example 2 with ServerResponse

use of org.springframework.web.servlet.function.ServerResponse in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method normal.

@Test
void normal() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = request -> Optional.of(handlerFunction);
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(result.getHandler()).isSameAs(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ServerResponse

use of org.springframework.web.servlet.function.ServerResponse in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method noMatch.

@Test
void noMatch() throws Exception {
    RouterFunction<ServerResponse> routerFunction = request -> Optional.empty();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNull();
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ServerResponse

use of org.springframework.web.servlet.function.ServerResponse in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method changeParser.

@Test
void changeParser() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/foo", handlerFunction).POST("/bar", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    PathPatternParser patternParser = new PathPatternParser();
    patternParser.setCaseSensitive(false);
    mapping.setPatternParser(patternParser);
    mapping.afterPropertiesSet();
    MockHttpServletRequest request = createTestRequest("/FOO");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(result.getHandler()).isSameAs(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 5 with ServerResponse

use of org.springframework.web.servlet.function.ServerResponse in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method mappedRequestShouldHoldAttributes.

@Test
void mappedRequestShouldHoldAttributes() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/match", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)).isEqualTo("/match");
    assertThat(request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerFunction (org.springframework.web.servlet.function.HandlerFunction)5 ServerResponse (org.springframework.web.servlet.function.ServerResponse)5 Collections (java.util.Collections)4 List (java.util.List)4 Optional (java.util.Optional)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)4 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)4 HandlerMapping (org.springframework.web.servlet.HandlerMapping)4 RouterFunction (org.springframework.web.servlet.function.RouterFunction)4 RouterFunctions (org.springframework.web.servlet.function.RouterFunctions)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 ServletRequestPathUtils (org.springframework.web.util.ServletRequestPathUtils)4 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)4 Nullable (org.springframework.lang.Nullable)1 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)1 ServerRequest (org.springframework.web.servlet.function.ServerRequest)1