Skip to content

Commit ccd5542

Browse files
committed
Fix #3729: Add overloaded methods for RewriteLocationResponseHeader filter
- Add 1, 2, 3 parameter overloads in FilterFunctions and AfterFilterFunctions - Support flexible YAML configuration: RewriteLocationResponseHeader=AS_IN_REQUEST - Add comprehensive tests for all parameter combinations - Update documentation with multiple configuration examples - Maintain full backward compatibility with existing 4-parameter method Resolves #3729 Signed-off-by: JongJun Kim <[email protected]>
1 parent f33e027 commit ccd5542

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway-server-webmvc/filters/rewritelocationresponseheader.adoc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ spring:
1818
predicates:
1919
- Path=/**
2020
filters:
21-
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
21+
# Minimal configuration - only stripVersion
22+
- RewriteLocationResponseHeader=AS_IN_REQUEST
23+
# Specify header name
24+
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location
25+
# Specify custom host value
26+
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, api.example.com
27+
# Full configuration with protocols regex
28+
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, api.example.com, https?|ftps?
2229
----
2330

2431
.GatewaySampleApplication.java
@@ -62,3 +69,14 @@ The `protocolsRegex` parameter must be a valid regex `String`, against which the
6269
If it is not matched, the filter does nothing.
6370
The default is `http|https|ftp|ftps`.
6471

72+
== Multiple Configuration Options
73+
74+
The `RewriteLocationResponseHeader` filter supports multiple configuration formats for different use cases:
75+
76+
* **Minimal configuration**: `RewriteLocationResponseHeader=AS_IN_REQUEST` - Uses default values for all other parameters
77+
* **With header name**: `RewriteLocationResponseHeader=AS_IN_REQUEST, Location` - Specifies custom header name (default is `Location`)
78+
* **With host value**: `RewriteLocationResponseHeader=AS_IN_REQUEST, Location, api.example.com` - Uses custom host instead of request Host header
79+
* **Full configuration**: `RewriteLocationResponseHeader=AS_IN_REQUEST, Location, api.example.com, https?|ftps?` - Specifies all parameters including protocols regex
80+
81+
All parameters after `stripVersionMode` are optional and use sensible defaults when not provided.
82+

spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/AfterFilterFunctions.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteL
123123
});
124124
}
125125

126+
public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteLocationResponseHeader(
127+
String stripVersion) {
128+
return RewriteLocationResponseHeaderFilterFunctions
129+
.rewriteLocationResponseHeader(config -> config.setStripVersion(stripVersion));
130+
}
131+
132+
public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteLocationResponseHeader(
133+
String stripVersion, String locationHeaderName) {
134+
return RewriteLocationResponseHeaderFilterFunctions.rewriteLocationResponseHeader(
135+
config -> config.setStripVersion(stripVersion).setLocationHeaderName(locationHeaderName));
136+
}
137+
138+
public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteLocationResponseHeader(
139+
String stripVersion, String locationHeaderName, String hostValue) {
140+
return RewriteLocationResponseHeaderFilterFunctions
141+
.rewriteLocationResponseHeader(config -> config.setStripVersion(stripVersion)
142+
.setLocationHeaderName(locationHeaderName)
143+
.setHostValue(hostValue));
144+
}
145+
146+
public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteLocationResponseHeader(
147+
String stripVersion, String locationHeaderName, String hostValue, String protocolsRegex) {
148+
return RewriteLocationResponseHeaderFilterFunctions
149+
.rewriteLocationResponseHeader(config -> config.setStripVersion(stripVersion)
150+
.setLocationHeaderName(locationHeaderName)
151+
.setHostValue(hostValue)
152+
.setProtocolsRegex(protocolsRegex));
153+
}
154+
126155
public static BiFunction<ServerRequest, ServerResponse, ServerResponse> rewriteLocationResponseHeader(
127156
Consumer<RewriteLocationResponseHeaderFilterFunctions.RewriteLocationResponseHeaderConfig> configConsumer) {
128157
return RewriteLocationResponseHeaderFilterFunctions.rewriteLocationResponseHeader(configConsumer);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ static HandlerFilterFunction<ServerResponse, ServerResponse> requestSize(DataSiz
160160
return ofRequestProcessor(BeforeFilterFunctions.requestSize(maxSize));
161161
}
162162

163+
@Shortcut
164+
static HandlerFilterFunction<ServerResponse, ServerResponse> rewriteLocationResponseHeader(String stripVersion) {
165+
return ofResponseProcessor(RewriteLocationResponseHeaderFilterFunctions
166+
.rewriteLocationResponseHeader(config -> config.setStripVersion(stripVersion)));
167+
}
168+
169+
@Shortcut
170+
static HandlerFilterFunction<ServerResponse, ServerResponse> rewriteLocationResponseHeader(String stripVersion,
171+
String locationHeaderName) {
172+
return ofResponseProcessor(RewriteLocationResponseHeaderFilterFunctions.rewriteLocationResponseHeader(
173+
config -> config.setStripVersion(stripVersion).setLocationHeaderName(locationHeaderName)));
174+
}
175+
176+
@Shortcut
177+
static HandlerFilterFunction<ServerResponse, ServerResponse> rewriteLocationResponseHeader(String stripVersion,
178+
String locationHeaderName, String hostValue) {
179+
return ofResponseProcessor(RewriteLocationResponseHeaderFilterFunctions
180+
.rewriteLocationResponseHeader(config -> config.setStripVersion(stripVersion)
181+
.setLocationHeaderName(locationHeaderName)
182+
.setHostValue(hostValue)));
183+
}
184+
163185
@Shortcut
164186
static HandlerFilterFunction<ServerResponse, ServerResponse> rewriteLocationResponseHeader(String stripVersion,
165187
String locationHeaderName, String hostValue, String protocolsRegex) {

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,42 @@ public void rewriteLocationResponseHeaderWorks() {
899899
.valueEquals("Location", "https://test1.rewritelocationresponseheader.org/some/object/id");
900900
}
901901

902+
@Test
903+
public void rewriteLocationResponseHeaderWithMinimalArgsWorks() {
904+
restClient.get()
905+
.uri("/anything/rewritelocationresponseheader-minimal")
906+
.header("Host", "test2.rewritelocationresponseheader.org")
907+
.exchange()
908+
.expectStatus()
909+
.isOk()
910+
.expectHeader()
911+
.valueEquals("Location", "https://test2.rewritelocationresponseheader.org/some/object/id");
912+
}
913+
914+
@Test
915+
public void rewriteLocationResponseHeaderWithTwoArgsWorks() {
916+
restClient.get()
917+
.uri("/anything/rewritelocationresponseheader-twoargs")
918+
.header("Host", "test3.rewritelocationresponseheader.org")
919+
.exchange()
920+
.expectStatus()
921+
.isOk()
922+
.expectHeader()
923+
.valueEquals("Location", "https://test3.rewritelocationresponseheader.org/some/object/id");
924+
}
925+
926+
@Test
927+
public void rewriteLocationResponseHeaderWithThreeArgsWorks() {
928+
restClient.get()
929+
.uri("/anything/rewritelocationresponseheader-threeargs")
930+
.header("Host", "test4.rewritelocationresponseheader.org")
931+
.exchange()
932+
.expectStatus()
933+
.isOk()
934+
.expectHeader()
935+
.valueEquals("Location", "https://custom.host.example.com/some/object/id");
936+
}
937+
902938
@Test
903939
public void readBodyWorks() {
904940
Event messageEvent = new Event("message", "bar");
@@ -1578,6 +1614,45 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsRewriteLocationRespo
15781614
// @formatter:on
15791615
}
15801616

1617+
@Bean
1618+
public RouterFunction<ServerResponse> gatewayRouterFunctionsRewriteLocationResponseHeaderMinimal() {
1619+
// @formatter:off
1620+
return route("testrewritelocationresponseheaderminimal")
1621+
.GET("/anything/rewritelocationresponseheader-minimal", host("**.rewritelocationresponseheader.org"), http())
1622+
.before(new HttpbinUriResolver())
1623+
// reverse order for "post" filters
1624+
.after(rewriteLocationResponseHeader("AS_IN_REQUEST"))
1625+
.after(addResponseHeader("Location", "https://backend.org:443/v1/some/object/id"))
1626+
.build();
1627+
// @formatter:on
1628+
}
1629+
1630+
@Bean
1631+
public RouterFunction<ServerResponse> gatewayRouterFunctionsRewriteLocationResponseHeaderTwoArgs() {
1632+
// @formatter:off
1633+
return route("testrewritelocationresponseheadertwoargs")
1634+
.GET("/anything/rewritelocationresponseheader-twoargs", host("**.rewritelocationresponseheader.org"), http())
1635+
.before(new HttpbinUriResolver())
1636+
// reverse order for "post" filters
1637+
.after(rewriteLocationResponseHeader("AS_IN_REQUEST", "Location"))
1638+
.after(addResponseHeader("Location", "https://backend.org:443/v1/some/object/id"))
1639+
.build();
1640+
// @formatter:on
1641+
}
1642+
1643+
@Bean
1644+
public RouterFunction<ServerResponse> gatewayRouterFunctionsRewriteLocationResponseHeaderThreeArgs() {
1645+
// @formatter:off
1646+
return route("testrewritelocationresponseheaderthreeargs")
1647+
.GET("/anything/rewritelocationresponseheader-threeargs", host("**.rewritelocationresponseheader.org"), http())
1648+
.before(new HttpbinUriResolver())
1649+
// reverse order for "post" filters
1650+
.after(rewriteLocationResponseHeader("AS_IN_REQUEST", "Location", "custom.host.example.com"))
1651+
.after(addResponseHeader("Location", "https://backend.org:443/v1/some/object/id"))
1652+
.build();
1653+
// @formatter:on
1654+
}
1655+
15811656
@Bean
15821657
public RouterFunction<ServerResponse> gatewayRouterFunctionsReadBodyPredicate() {
15831658
// @formatter:off

0 commit comments

Comments
 (0)