Browse Source

Adds GatewayRouterFunctions.route()

This overloads the methods from RouterFunctions and automatically adds the FilterFunctions.routeId() filter, which adds the routeId as a request attribute.

This request attribute is in turn, used to determine if the current route is a "gateway route";

See gh-2949
pull/3006/head
sgibb 1 year ago
parent
commit
e12d6b4d1a
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 7
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java
  2. 46
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/GatewayRouterFunctions.java
  3. 2
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java
  4. 16
      spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

7
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java

@ -145,6 +145,13 @@ public interface FilterFunctions { @@ -145,6 +145,13 @@ public interface FilterFunctions {
};
}
static HandlerFilterFunction<ServerResponse, ServerResponse> routeId(String routeId) {
return (request, next) -> {
request.attributes().put(MvcUtils.GATEWAY_ROUTE_ID_ATTR, routeId);
return next.handle(request);
};
}
@Shortcut
static HandlerFilterFunction<ServerResponse, ServerResponse> setPath(String path) {
UriTemplate uriTemplate = new UriTemplate(path);

46
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/GatewayRouterFunctions.java

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.server.mvc.handler;
import org.springframework.web.servlet.function.HandlerFunction;
import org.springframework.web.servlet.function.RequestPredicate;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.RouterFunctions.Builder;
import org.springframework.web.servlet.function.ServerResponse;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.routeId;
public abstract class GatewayRouterFunctions {
private GatewayRouterFunctions() {
}
public static Builder route() {
return RouterFunctions.route();
}
public static Builder route(String routeId) {
return RouterFunctions.route().filter(routeId(routeId));
}
public static <T extends ServerResponse> RouterFunction<T> route(RequestPredicate predicate,
HandlerFunction<T> handlerFunction) {
return RouterFunctions.route(predicate, handlerFunction);
}
}

2
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java

@ -29,7 +29,7 @@ import org.springframework.web.servlet.function.ServerResponse; @@ -29,7 +29,7 @@ import org.springframework.web.servlet.function.ServerResponse;
public abstract class HandlerFunctions {
protected HandlerFunctions() {
private HandlerFunctions() {
}

16
spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

@ -78,11 +78,13 @@ import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunction @@ -78,11 +78,13 @@ import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunction
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.redirectTo;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.removeRequestHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.rewritePath;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.routeId;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.setPath;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.setStatus;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.stripPrefix;
import static org.springframework.cloud.gateway.server.mvc.filter.LoadBalancerFilterFunctions.lb;
import static org.springframework.cloud.gateway.server.mvc.filter.RetryFilterFunctions.retry;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.cookie;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.header;
@ -93,7 +95,6 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; @@ -93,7 +95,6 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import static org.springframework.web.servlet.function.RequestPredicates.GET;
import static org.springframework.web.servlet.function.RequestPredicates.POST;
import static org.springframework.web.servlet.function.RequestPredicates.path;
import static org.springframework.web.servlet.function.RouterFunctions.route;
@SuppressWarnings("unchecked")
@SpringBootTest(properties = {}, webEnvironment = WebEnvironment.RANDOM_PORT)
@ -406,15 +407,16 @@ public class ServerMvcIntegrationTests { @@ -406,15 +407,16 @@ public class ServerMvcIntegrationTests {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsSetStatusAndAddRespHeader() {
// @formatter:off
return (RouterFunction<ServerResponse>) route().GET("/status/{status}", http())
return (RouterFunction<ServerResponse>) route("testsetstatus")
.GET("/status/{status}", http())
.filter(new HttpbinUriResolver())
.filter(setStatus(HttpStatus.TOO_MANY_REQUESTS))
.filter(addResponseHeader("X-Status", "{status}"))
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testsetstatus")
.build().andOther(route().GET("/anything/addresheader", http())
.build().andOther(route()
.GET("/anything/addresheader", http())
.filter(routeId("testaddresponseheader"))
.filter(new HttpbinUriResolver())
.filter(addResponseHeader("X-Bar", "val1"))
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testaddresponseheader")
.build());
// @formatter:on
}
@ -581,13 +583,11 @@ public class ServerMvcIntegrationTests { @@ -581,13 +583,11 @@ public class ServerMvcIntegrationTests {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsForm() {
// @formatter:off
return route()
return route("testform")
.POST("/post", header("test", "form"), http())
.filter(new LocalServerPortUriResolver())
.filter(prefixPath("/test"))
.filter(addRequestHeader("X-Test", "form"))
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testform")
.build();
// @formatter:on
}

Loading…
Cancel
Save