2018-07-23 23:31:28 +03:00
|
|
|
package com.baeldung.functional;
|
|
|
|
|
|
|
|
|
|
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
|
|
|
|
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.http.server.reactive.HttpHandler;
|
2018-07-29 00:06:59 +05:30
|
|
|
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
2018-07-23 23:31:28 +03:00
|
|
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
|
|
|
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
|
|
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
|
|
|
import org.springframework.web.server.WebHandler;
|
|
|
|
|
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
|
|
|
|
|
|
|
|
import reactor.core.publisher.Flux;
|
2018-07-29 00:06:59 +05:30
|
|
|
import reactor.ipc.netty.NettyContext;
|
|
|
|
|
import reactor.ipc.netty.http.server.HttpServer;
|
2018-07-23 23:31:28 +03:00
|
|
|
|
|
|
|
|
public class FunctionalWebApplication {
|
|
|
|
|
|
|
|
|
|
private static final Actor BRAD_PITT = new Actor("Brad", "Pitt");
|
|
|
|
|
private static final Actor TOM_HANKS = new Actor("Tom", "Hanks");
|
|
|
|
|
private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS));
|
|
|
|
|
|
|
|
|
|
private RouterFunction<ServerResponse> routingFunction() {
|
|
|
|
|
FormHandler formHandler = new FormHandler();
|
|
|
|
|
|
|
|
|
|
RouterFunction<ServerResponse> restfulRouter = route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class)
|
|
|
|
|
.doOnNext(actors::add)
|
|
|
|
|
.then(ok().build()));
|
|
|
|
|
|
|
|
|
|
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin)
|
|
|
|
|
.andRoute(POST("/upload"), formHandler::handleUpload)
|
|
|
|
|
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
|
|
|
|
|
.andNest(path("/actor"), restfulRouter)
|
|
|
|
|
.filter((request, next) -> {
|
|
|
|
|
System.out.println("Before handler invocation: " + request.path());
|
|
|
|
|
return next.handle(request);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 00:06:59 +05:30
|
|
|
NettyContext start() throws Exception {
|
2018-07-23 23:31:28 +03:00
|
|
|
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
|
|
|
|
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
|
|
|
|
.filter(new IndexRewriteFilter())
|
|
|
|
|
.build();
|
|
|
|
|
|
2018-07-29 00:06:59 +05:30
|
|
|
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
|
|
|
|
|
HttpServer server = HttpServer.create("localhost", 9090);
|
|
|
|
|
return server.newHandler(adapter).block();
|
2018-07-23 23:31:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
try {
|
|
|
|
|
new FunctionalWebApplication().start();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|