|
| 1 | +package com.baeldung.reactive.urlmatch; |
| 2 | + |
| 3 | +import static org.springframework.web.reactive.function.BodyInserters.fromObject; |
| 4 | +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; |
| 5 | +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; |
| 6 | +import static org.springframework.web.reactive.function.server.RequestPredicates.path; |
| 7 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 8 | +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; |
| 9 | +import static org.springframework.web.reactive.function.server.ServerResponse.ok; |
| 10 | + |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 14 | + |
| 15 | +import org.apache.catalina.Context; |
| 16 | +import org.apache.catalina.startup.Tomcat; |
| 17 | +import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; |
| 18 | +import org.springframework.boot.web.server.WebServer; |
| 19 | +import org.springframework.core.io.ClassPathResource; |
| 20 | +import org.springframework.http.server.reactive.HttpHandler; |
| 21 | +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; |
| 22 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 23 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 24 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 25 | +import org.springframework.web.server.WebHandler; |
| 26 | +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; |
| 27 | + |
| 28 | +import reactor.core.publisher.Flux; |
| 29 | + |
| 30 | +public class FunctionalWebApplication { |
| 31 | + |
| 32 | + private static final Actor BRAD_PITT = new Actor("Brad", "Pitt"); |
| 33 | + private static final Actor TOM_HANKS = new Actor("Tom", "Hanks"); |
| 34 | + private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS)); |
| 35 | + |
| 36 | + private RouterFunction<ServerResponse> routingFunction() { |
| 37 | + FormHandler formHandler = new FormHandler(); |
| 38 | + |
| 39 | + RouterFunction<ServerResponse> restfulRouter = route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class) |
| 40 | + .doOnNext(actors::add) |
| 41 | + .then(ok().build())); |
| 42 | + |
| 43 | + return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin) |
| 44 | + .andRoute(POST("/upload"), formHandler::handleUpload) |
| 45 | + .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) |
| 46 | + .andNest(path("/actor"), restfulRouter) |
| 47 | + .filter((request, next) -> { |
| 48 | + System.out.println("Before handler invocation: " + request.path()); |
| 49 | + return next.handle(request); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + WebServer start() throws Exception { |
| 54 | + WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); |
| 55 | + HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) |
| 56 | + .filter(new IndexRewriteFilter()) |
| 57 | + .build(); |
| 58 | + |
| 59 | + Tomcat tomcat = new Tomcat(); |
| 60 | + tomcat.setHostname("localhost"); |
| 61 | + tomcat.setPort(9090); |
| 62 | + Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); |
| 63 | + ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); |
| 64 | + Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); |
| 65 | + rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); |
| 66 | + |
| 67 | + TomcatWebServer server = new TomcatWebServer(tomcat); |
| 68 | + server.start(); |
| 69 | + return server; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + public static void main(String[] args) { |
| 74 | + try { |
| 75 | + new FunctionalWebApplication().start(); |
| 76 | + } catch (Exception e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments