Skip to content

Commit c4675e4

Browse files
committed
avoid breaking changes
1 parent 989dd62 commit c4675e4

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/AddResponseHeaderGatewayFilterFactory.java

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@
3434
/**
3535
* @author Spencer Gibb
3636
*/
37-
public class AddResponseHeaderGatewayFilterFactory
38-
extends AbstractGatewayFilterFactory<AddResponseHeaderGatewayFilterFactory.Config> {
37+
public class AddResponseHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
3938

4039
private static final String OVERRIDE_KEY = "override";
4140

42-
public AddResponseHeaderGatewayFilterFactory() {
43-
super(Config.class);
41+
@Override
42+
public Class getConfigClass() {
43+
return Config.class;
44+
}
45+
46+
@Override
47+
public NameValueConfig newConfig() {
48+
return new Config();
4449
}
4550

4651
@Override
@@ -49,7 +54,7 @@ public List<String> shortcutFieldOrder() {
4954
}
5055

5156
@Override
52-
public GatewayFilter apply(Config config) {
57+
public GatewayFilter apply(NameValueConfig config) {
5358
return new GatewayFilter() {
5459
@Override
5560
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@@ -58,21 +63,32 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
5863

5964
@Override
6065
public String toString() {
66+
if (config instanceof Config) {
67+
return filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this)
68+
.append(GatewayFilter.NAME_KEY, config.getName())
69+
.append(GatewayFilter.VALUE_KEY, config.getValue())
70+
.append(OVERRIDE_KEY, ((Config) config).isOverride())
71+
.toString();
72+
}
6173
return filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this)
62-
.append(GatewayFilter.NAME_KEY, config.getName())
63-
.append(GatewayFilter.VALUE_KEY, config.getValue())
64-
.append(OVERRIDE_KEY, config.isOverride())
74+
.append(config.getName(), config.getValue())
6575
.toString();
6676
}
6777
};
6878
}
6979

70-
void addHeader(ServerWebExchange exchange, Config config) {
80+
void addHeader(ServerWebExchange exchange, NameValueConfig config) {
7181
// if response has been commited, no more response headers will bee added.
7282
if (!exchange.getResponse().isCommitted()) {
7383
final String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
7484
HttpHeaders headers = exchange.getResponse().getHeaders();
75-
if (config.override) {
85+
86+
boolean override = true; // default is true
87+
if (config instanceof Config) {
88+
override = ((Config) config).isOverride();
89+
}
90+
91+
if (override) {
7692
headers.add(config.getName(), value);
7793
}
7894
else {
@@ -86,32 +102,10 @@ void addHeader(ServerWebExchange exchange, Config config) {
86102
}
87103
}
88104

89-
public static class Config {
90-
91-
private String name;
92-
93-
private String value;
105+
public static class Config extends AbstractNameValueGatewayFilterFactory.NameValueConfig {
94106

95107
private boolean override = true;
96108

97-
public String getName() {
98-
return name;
99-
}
100-
101-
public Config setName(String name) {
102-
this.name = name;
103-
return this;
104-
}
105-
106-
public String getValue() {
107-
return value;
108-
}
109-
110-
public Config setValue(String value) {
111-
this.value = value;
112-
return this;
113-
}
114-
115109
public boolean isOverride() {
116110
return override;
117111
}

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,12 @@ public GatewayFilterSpec addResponseHeader(String headerName, String headerValue
233233
* @return a {@link GatewayFilterSpec} that can be used to apply additional filters
234234
*/
235235
public GatewayFilterSpec addResponseHeader(String headerName, String headerValue, boolean override) {
236-
return filter(getBean(AddResponseHeaderGatewayFilterFactory.class)
237-
.apply(c -> c.setName(headerName).setValue(headerValue).setOverride(override)));
236+
AddResponseHeaderGatewayFilterFactory.Config config = new AddResponseHeaderGatewayFilterFactory.Config();
237+
config.setName(headerName);
238+
config.setValue(headerValue);
239+
config.setOverride(override);
240+
241+
return filter(getBean(AddResponseHeaderGatewayFilterFactory.class).apply(config));
238242
}
239243

240244
/**

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/AddResponseHeaderGatewayFilterFactoryTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2828
import org.springframework.boot.test.context.SpringBootTest;
2929
import org.springframework.cloud.gateway.filter.GatewayFilter;
30+
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory.NameValueConfig;
3031
import org.springframework.cloud.gateway.route.RouteLocator;
3132
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
3233
import org.springframework.cloud.gateway.test.BaseWebClientTests;
@@ -114,9 +115,7 @@ void testResponseHeaderFilterHeaderPresentJavaDsl() {
114115

115116
@Test
116117
void toStringFormat() {
117-
AddResponseHeaderGatewayFilterFactory.Config config = new AddResponseHeaderGatewayFilterFactory.Config()
118-
.setName("myname")
119-
.setValue("myvalue");
118+
NameValueConfig config = new NameValueConfig().setName("myname").setValue("myvalue");
120119
GatewayFilter filter = new AddResponseHeaderGatewayFilterFactory().apply(config);
121120
assertThat(filter.toString()).contains("myname").contains("myvalue");
122121
}

0 commit comments

Comments
 (0)