|
| 1 | +package com.baeldung.functional; |
| 2 | + |
| 3 | +import org.springframework.core.io.ClassPathResource; |
| 4 | +import org.springframework.http.server.reactive.HttpHandler; |
| 5 | +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; |
| 6 | +import org.springframework.util.MultiValueMap; |
| 7 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 8 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 9 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 10 | +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; |
| 11 | +import reactor.core.publisher.Flux; |
| 12 | +import reactor.core.publisher.Mono; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 17 | +import java.util.concurrent.atomic.AtomicLong; |
| 18 | + |
| 19 | +import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; |
| 20 | +import static org.springframework.web.reactive.function.BodyExtractors.toFormData; |
| 21 | +import static org.springframework.web.reactive.function.BodyInserters.fromObject; |
| 22 | +import static org.springframework.web.reactive.function.server.RequestPredicates.*; |
| 23 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 24 | +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; |
| 25 | +import static org.springframework.web.reactive.function.server.ServerResponse.ok; |
| 26 | + |
| 27 | +public class RootServlet extends ServletHttpHandlerAdapter { |
| 28 | + |
| 29 | + public RootServlet() { |
| 30 | + this(WebHttpHandlerBuilder |
| 31 | + .webHandler(toHttpHandler(routingFunction())) |
| 32 | + .prependFilter(new IndexRewriteFilter()) |
| 33 | + .build()); |
| 34 | + } |
| 35 | + |
| 36 | + private RootServlet(HttpHandler httpHandler) { |
| 37 | + super(httpHandler); |
| 38 | + } |
| 39 | + |
| 40 | + private static final Actor BRAD_PITT = new Actor("Brad", "Pitt"); |
| 41 | + private static final Actor TOM_HANKS = new Actor("Tom", "Hanks"); |
| 42 | + private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS)); |
| 43 | + |
| 44 | + private static RouterFunction<?> routingFunction() { |
| 45 | + |
| 46 | + return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))) |
| 47 | + .andRoute(POST("/login"), serverRequest -> serverRequest |
| 48 | + .body(toFormData()) |
| 49 | + .map(MultiValueMap::toSingleValueMap) |
| 50 | + .map(formData -> { |
| 51 | + System.out.println("form data: " + formData.toString()); |
| 52 | + if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) { |
| 53 | + return ok() |
| 54 | + .body(Mono.just("welcome back!"), String.class) |
| 55 | + .block(); |
| 56 | + } |
| 57 | + return ServerResponse |
| 58 | + .badRequest() |
| 59 | + .build() |
| 60 | + .block(); |
| 61 | + })) |
| 62 | + .andRoute(POST("/upload"), serverRequest -> serverRequest |
| 63 | + .body(toDataBuffers()) |
| 64 | + .collectList() |
| 65 | + .map(dataBuffers -> { |
| 66 | + AtomicLong atomicLong = new AtomicLong(0); |
| 67 | + dataBuffers.forEach(d -> atomicLong.addAndGet(d |
| 68 | + .asByteBuffer() |
| 69 | + .array().length)); |
| 70 | + System.out.println("data length:" + atomicLong.get()); |
| 71 | + return ok() |
| 72 | + .body(fromObject(atomicLong.toString())) |
| 73 | + .block(); |
| 74 | + })) |
| 75 | + .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) |
| 76 | + .andNest(path("/actor"), route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest |
| 77 | + .bodyToMono(Actor.class) |
| 78 | + .doOnNext(actors::add) |
| 79 | + .then(ok().build()))) |
| 80 | + .filter((request, next) -> { |
| 81 | + System.out.println("Before handler invocation: " + request.path()); |
| 82 | + return next.handle(request); |
| 83 | + }); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments