Hi,
I need to modify the body of both the request and the response (encryption/decryption).
As I see it, I can create a filter that sets a ServerHttpRequestDecorator in the exchange and override the getBody method, but I didn't find a way to do the same with the response. The WriteResponseFilter is the first filter and consequently the exchange used is the original exchange.
Would it be possible to add a ResponseBodyHandler interface to allow modifying the response body? Or did I overlook something?
Thanks in advance.
Nevermind, I just added a GlobalFilter with an order of -2 and it works.
I'm trying, but no result...
Would you please post any sample code how to change response body?
Hi,
I'll try to make a Gist to show you how I've done (I'll have to clean up corporate code first), but in short:
Hope it makes sense. I have 2 filters that work that way (the first one removes some fields from the JSON response, the second one ciphers the body of the response in AES).
Not the cleanest code I've written though, maybe there is a better way.
Thanks a lot!
I'm new to reactive way, so I have to learn many things "in parallel mode" in short time...
And how do you cast the body from a Publisher extends DataBuffer> to a Flux?
I got it "works" somehow, but always with exception "only one subscriber allowed". And that way, as I understand, is wrong.
response.bufferFactory().allocateBuffer().asInputStream() reproduces nothing...
I made a small example here : https://gist.github.com/WeirdBob/b25569d461f0f54444d2c0eab51f3c48
it just converts the body into to uppercase.
you can launch it and try going to localhost:8080/uuid
Thank you very much!!!
I got hope!
You've made my day!!!
We can simplify response body modification by using a plain GatewayFilter:
GatewayFilter implementation with the same filter() content as in the sample above;bodyToUppercase() (if you prefer - just for simplification);.route("routeIdHere")
.predicate(path("/deep/deep/hurray"))
...
.filter(bodyToUppercase(), NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1)
...
.uri("lb://serverId")
So, it can be achieved with a plain GatewayFilter, by forcing the order pass. Using the GlobalFilter is not mandatory, but using GatewayFilter we have clear picture of the routes.
How to append or modify the request body ? the length of the body may become longer, but the service behind the gateway received the request body have been truncated, some data lost.
I solved this problem by modifying the length of the head:
To modify the header CONTENT_LENGTH,You must to create a new HttpHeaders, and then copy the orignal values and remove the CONTENT_LENGTH, add the correct length(If you do not do this , the request body will be truncated if you modify the request body and the body become longer )
HttpHeaders myHeaders = new HttpHeaders();
copyMultiValueMap(request.getHeaders(), myHeaders);
myHeaders.remove(HttpHeaders.CONTENT_LENGTH);
myHeaders.set(HttpHeaders.CONTENT_LENGTH, String.valueOf(len));
Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer);
newRequest = new ServerHttpRequestDecorator(newRequest) {
@Override
public Flux<DataBuffer> getBody() {
return bodyFlux;
}
@Override
public HttpHeaders getHeaders() {
return myHeaders;
}
};
private static
source.forEach((key, value) -> target.put(key, new LinkedList<>(value)));
}
Most helpful comment
Hi,
I'll try to make a Gist to show you how I've done (I'll have to clean up corporate code first), but in short:
Hope it makes sense. I have 2 filters that work that way (the first one removes some fields from the JSON response, the second one ciphers the body of the response in AES).
Not the cleanest code I've written though, maybe there is a better way.