Adds support for hints in reading/modifying request/response bodies.
fixes gh-298
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.springframework.cloud.gateway.filter.factory.rewrite;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -58,7 +59,7 @@ public class ModifyRequestBodyGatewayFilterFactory
|
||||
|
||||
if (reader.isPresent()) {
|
||||
Mono<Object> readMono = reader.get()
|
||||
.readMono(inElementType, exchange.getRequest(), null)
|
||||
.readMono(inElementType, exchange.getRequest(), config.getInHints())
|
||||
.cast(Object.class);
|
||||
|
||||
return process(readMono, peek -> {
|
||||
@@ -74,7 +75,7 @@ public class ModifyRequestBodyGatewayFilterFactory
|
||||
|
||||
HttpMessageWriterResponse fakeResponse = new HttpMessageWriterResponse(exchange.getResponse().bufferFactory());
|
||||
writer.get().write(publisher, inElementType, mediaType,
|
||||
fakeResponse, null);
|
||||
fakeResponse, config.getOutHints());
|
||||
ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(
|
||||
exchange.getRequest()) {
|
||||
@Override
|
||||
@@ -109,6 +110,8 @@ public class ModifyRequestBodyGatewayFilterFactory
|
||||
public static class Config {
|
||||
private Class inClass;
|
||||
private Class outClass;
|
||||
private Map<String, Object> inHints;
|
||||
private Map<String, Object> outHints;
|
||||
|
||||
private RewriteFunction rewriteFunction;
|
||||
|
||||
@@ -130,6 +133,24 @@ public class ModifyRequestBodyGatewayFilterFactory
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, Object> getInHints() {
|
||||
return inHints;
|
||||
}
|
||||
|
||||
public Config setInHints(Map<String, Object> inHints) {
|
||||
this.inHints = inHints;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, Object> getOutHints() {
|
||||
return outHints;
|
||||
}
|
||||
|
||||
public Config setOutHints(Map<String, Object> outHints) {
|
||||
this.outHints = outHints;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RewriteFunction getRewriteFunction() {
|
||||
return rewriteFunction;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.springframework.cloud.gateway.filter.factory.rewrite;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -53,7 +54,6 @@ public class ModifyResponseBodyGatewayFilterFactory
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public GatewayFilter apply(Config config) {
|
||||
return new ModifyResponseGatewayFilter(config);
|
||||
}
|
||||
@@ -66,6 +66,7 @@ public class ModifyResponseBodyGatewayFilterFactory
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(exchange.getResponse()) {
|
||||
@Override
|
||||
@@ -81,13 +82,14 @@ public class ModifyResponseBodyGatewayFilterFactory
|
||||
|
||||
ResponseAdapter responseAdapter = new ResponseAdapter(body, getDelegate().getHeaders());
|
||||
|
||||
Flux<?> modified = reader.get().read(inElementType, responseAdapter, null)
|
||||
Flux<?> modified = reader.get().read(inElementType, responseAdapter, config.getInHints())
|
||||
.cast(inElementType.resolve())
|
||||
.flatMap(originalBody -> Flux.just(config.rewriteFunction.apply(exchange, originalBody)))
|
||||
.cast(outElementType.resolve());
|
||||
|
||||
return getDelegate().writeWith(
|
||||
writer.get().write((Publisher)modified, outElementType, null, getDelegate(), null)
|
||||
writer.get().write((Publisher)modified, outElementType, null, getDelegate(),
|
||||
config.getOutHints())
|
||||
);
|
||||
|
||||
}
|
||||
@@ -141,6 +143,8 @@ public class ModifyResponseBodyGatewayFilterFactory
|
||||
public static class Config {
|
||||
private Class inClass;
|
||||
private Class outClass;
|
||||
private Map<String, Object> inHints;
|
||||
private Map<String, Object> outHints;
|
||||
|
||||
private RewriteFunction rewriteFunction;
|
||||
|
||||
@@ -162,6 +166,24 @@ public class ModifyResponseBodyGatewayFilterFactory
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, Object> getInHints() {
|
||||
return inHints;
|
||||
}
|
||||
|
||||
public Config setInHints(Map<String, Object> inHints) {
|
||||
this.inHints = inHints;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, Object> getOutHints() {
|
||||
return outHints;
|
||||
}
|
||||
|
||||
public Config setOutHints(Map<String, Object> outHints) {
|
||||
this.outHints = outHints;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RewriteFunction getRewriteFunction() {
|
||||
return rewriteFunction;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.springframework.cloud.gateway.handler.predicate;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -53,9 +54,9 @@ public class ReadBodyPredicateFactory extends AbstractRoutePredicateFactory<Read
|
||||
ResolvableType elementType = ResolvableType.forClass(config.getInClass());
|
||||
Optional<HttpMessageReader<?>> reader = getHttpMessageReader(codecConfigurer, elementType, mediaType);
|
||||
boolean answer = false;
|
||||
if (reader.isPresent()) {
|
||||
if (reader.isPresent()) {
|
||||
Mono<Object> readMono = reader.get()
|
||||
.readMono(elementType, exchange.getRequest(), null)
|
||||
.readMono(elementType, exchange.getRequest(), config.getHints())
|
||||
.cast(Object.class);
|
||||
answer = process(readMono, peek -> {
|
||||
Optional<HttpMessageWriter<?>> writer = getHttpMessageWriter(codecConfigurer, elementType, mediaType);
|
||||
@@ -63,7 +64,7 @@ if (reader.isPresent()) {
|
||||
if (writer.isPresent()) {
|
||||
Publisher publisher = Mono.just(peek);
|
||||
HttpMessageWriterResponse fakeResponse = new HttpMessageWriterResponse(exchange.getResponse().bufferFactory());
|
||||
writer.get().write(publisher, elementType, mediaType, fakeResponse, null);
|
||||
writer.get().write(publisher, elementType, mediaType, fakeResponse, config.getHints());
|
||||
exchange.getAttributes().put(CACHED_REQUEST_BODY_KEY, fakeResponse.getBody());
|
||||
}
|
||||
return config.getPredicate().test(peek);
|
||||
@@ -77,6 +78,7 @@ if (reader.isPresent()) {
|
||||
public static class Config {
|
||||
private Class inClass;
|
||||
private Predicate predicate;
|
||||
private Map<String, Object> hints;
|
||||
|
||||
public Class getInClass() {
|
||||
return inClass;
|
||||
@@ -101,5 +103,14 @@ if (reader.isPresent()) {
|
||||
this.predicate = predicate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> getHints() {
|
||||
return hints;
|
||||
}
|
||||
|
||||
public Config setHints(Map<String, Object> hints) {
|
||||
this.hints = hints;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user