avoid breaking changes
Signed-off-by: jiangyuan <joe469391363@gmail.com>
This commit is contained in:
@@ -34,13 +34,18 @@ import static org.springframework.cloud.gateway.support.GatewayToStringStyler.fi
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class AddResponseHeaderGatewayFilterFactory
|
||||
extends AbstractGatewayFilterFactory<AddResponseHeaderGatewayFilterFactory.Config> {
|
||||
public class AddResponseHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
|
||||
|
||||
private static final String OVERRIDE_KEY = "override";
|
||||
|
||||
public AddResponseHeaderGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
@Override
|
||||
public Class getConfigClass() {
|
||||
return Config.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NameValueConfig newConfig() {
|
||||
return new Config();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,7 +54,7 @@ public class AddResponseHeaderGatewayFilterFactory
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
public GatewayFilter apply(NameValueConfig config) {
|
||||
return new GatewayFilter() {
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
@@ -58,21 +63,32 @@ public class AddResponseHeaderGatewayFilterFactory
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (config instanceof Config) {
|
||||
return filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this)
|
||||
.append(GatewayFilter.NAME_KEY, config.getName())
|
||||
.append(GatewayFilter.VALUE_KEY, config.getValue())
|
||||
.append(OVERRIDE_KEY, ((Config) config).isOverride())
|
||||
.toString();
|
||||
}
|
||||
return filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this)
|
||||
.append(GatewayFilter.NAME_KEY, config.getName())
|
||||
.append(GatewayFilter.VALUE_KEY, config.getValue())
|
||||
.append(OVERRIDE_KEY, config.isOverride())
|
||||
.append(config.getName(), config.getValue())
|
||||
.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void addHeader(ServerWebExchange exchange, Config config) {
|
||||
void addHeader(ServerWebExchange exchange, NameValueConfig config) {
|
||||
// if response has been commited, no more response headers will bee added.
|
||||
if (!exchange.getResponse().isCommitted()) {
|
||||
final String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
|
||||
HttpHeaders headers = exchange.getResponse().getHeaders();
|
||||
if (config.override) {
|
||||
|
||||
boolean override = true; // default is true
|
||||
if (config instanceof Config) {
|
||||
override = ((Config) config).isOverride();
|
||||
}
|
||||
|
||||
if (override) {
|
||||
headers.add(config.getName(), value);
|
||||
}
|
||||
else {
|
||||
@@ -86,32 +102,10 @@ public class AddResponseHeaderGatewayFilterFactory
|
||||
}
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
private String name;
|
||||
|
||||
private String value;
|
||||
public static class Config extends AbstractNameValueGatewayFilterFactory.NameValueConfig {
|
||||
|
||||
private boolean override = true;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Config setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Config setValue(String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isOverride() {
|
||||
return override;
|
||||
}
|
||||
|
||||
@@ -233,8 +233,12 @@ public class GatewayFilterSpec extends UriSpec {
|
||||
* @return a {@link GatewayFilterSpec} that can be used to apply additional filters
|
||||
*/
|
||||
public GatewayFilterSpec addResponseHeader(String headerName, String headerValue, boolean override) {
|
||||
return filter(getBean(AddResponseHeaderGatewayFilterFactory.class)
|
||||
.apply(c -> c.setName(headerName).setValue(headerValue).setOverride(override)));
|
||||
AddResponseHeaderGatewayFilterFactory.Config config = new AddResponseHeaderGatewayFilterFactory.Config();
|
||||
config.setName(headerName);
|
||||
config.setValue(headerValue);
|
||||
config.setOverride(override);
|
||||
|
||||
return filter(getBean(AddResponseHeaderGatewayFilterFactory.class).apply(config));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory.NameValueConfig;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests;
|
||||
@@ -114,9 +115,7 @@ class AddResponseHeaderGatewayFilterFactoryTests extends BaseWebClientTests {
|
||||
|
||||
@Test
|
||||
void toStringFormat() {
|
||||
AddResponseHeaderGatewayFilterFactory.Config config = new AddResponseHeaderGatewayFilterFactory.Config()
|
||||
.setName("myname")
|
||||
.setValue("myvalue");
|
||||
NameValueConfig config = new NameValueConfig().setName("myname").setValue("myvalue");
|
||||
GatewayFilter filter = new AddResponseHeaderGatewayFilterFactory().apply(config);
|
||||
assertThat(filter.toString()).contains("myname").contains("myvalue");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user