Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6f8f8d

Browse files
Doha2012pivovarit
authored andcommitted
move url matching (eugenp#3326)
* make sure modules using java8 * move url matching code
1 parent ccf1f4e commit f6f8f8d

File tree

11 files changed

+185
-8
lines changed

11 files changed

+185
-8
lines changed

spring-5-reactive/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</dependency>
2929
<dependency>
3030
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-starter-web</artifactId>
31+
<artifactId>spring-boot-starter-tomcat</artifactId>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.springframework.boot</groupId>

spring-5/src/main/java/com/baeldung/web/PathPatternController.java renamed to spring-5-reactive/src/main/java/com/baeldung/reactive/controller/PathPatternController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.web;
1+
package com.baeldung.reactive.controller;
22

33
import org.springframework.web.bind.annotation.GetMapping;
44
import org.springframework.web.bind.annotation.PathVariable;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.reactive.urlmatch;
2+
3+
class Actor {
4+
private String firstname;
5+
private String lastname;
6+
7+
public Actor() {
8+
}
9+
10+
public Actor(String firstname, String lastname) {
11+
this.firstname = firstname;
12+
this.lastname = lastname;
13+
}
14+
15+
public String getFirstname() {
16+
return firstname;
17+
}
18+
19+
public String getLastname() {
20+
return lastname;
21+
}
22+
23+
}

spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java renamed to spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.functional;
1+
package com.baeldung.reactive.urlmatch;
22

33
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
44
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.reactive.urlmatch;
2+
3+
import org.springframework.core.io.buffer.DataBuffer;
4+
import org.springframework.util.MultiValueMap;
5+
import org.springframework.web.reactive.function.server.ServerRequest;
6+
import org.springframework.web.reactive.function.server.ServerResponse;
7+
import reactor.core.publisher.Mono;
8+
9+
import java.util.List;
10+
import java.util.concurrent.atomic.AtomicLong;
11+
12+
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
13+
import static org.springframework.web.reactive.function.BodyExtractors.toFormData;
14+
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
15+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
16+
17+
public class FormHandler {
18+
19+
Mono<ServerResponse> handleLogin(ServerRequest request) {
20+
return request.body(toFormData())
21+
.map(MultiValueMap::toSingleValueMap)
22+
.filter(formData -> "baeldung".equals(formData.get("user")))
23+
.filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
24+
.flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
25+
.switchIfEmpty(ServerResponse.badRequest()
26+
.build());
27+
}
28+
29+
Mono<ServerResponse> handleUpload(ServerRequest request) {
30+
return request.body(toDataBuffers())
31+
.collectList()
32+
.flatMap(dataBuffers -> ok().body(fromObject(extractData(dataBuffers).toString())));
33+
}
34+
35+
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
36+
AtomicLong atomicLong = new AtomicLong(0);
37+
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
38+
.array().length));
39+
return atomicLong;
40+
}
41+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.reactive.urlmatch;
2+
3+
import org.springframework.http.server.reactive.ServerHttpRequest;
4+
import org.springframework.web.server.ServerWebExchange;
5+
import org.springframework.web.server.WebFilter;
6+
import org.springframework.web.server.WebFilterChain;
7+
import reactor.core.publisher.Mono;
8+
9+
class IndexRewriteFilter implements WebFilter {
10+
11+
@Override
12+
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
13+
ServerHttpRequest request = serverWebExchange.getRequest();
14+
if (request.getURI()
15+
.getPath()
16+
.equals("/")) {
17+
return webFilterChain.filter(serverWebExchange.mutate()
18+
.request(builder -> builder.method(request.getMethod())
19+
.contextPath(request.getPath()
20+
.toString())
21+
.path("/test"))
22+
.build());
23+
}
24+
return webFilterChain.filter(serverWebExchange);
25+
}
26+
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

spring-5/src/test/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctionsTest.java renamed to spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package com.baeldung.functional;
1+
package com.baeldung.reactive.urlmatch;
22

33
import org.junit.AfterClass;
44
import org.junit.BeforeClass;
55
import org.junit.Test;
66
import org.springframework.boot.web.server.WebServer;
77
import org.springframework.test.web.reactive.server.WebTestClient;
88

9-
public class ExploreSpring5URLPatternUsingRouterFunctionsTest {
9+
import com.baeldung.reactive.urlmatch.ExploreSpring5URLPatternUsingRouterFunctions;
10+
11+
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
1012

1113
private static WebTestClient client;
1214
private static WebServer server;

0 commit comments

Comments
 (0)