Skip to content

Commit

Permalink
feat: create RequestBodyCachingFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
gmkim20713 committed Oct 25, 2024
1 parent 88a73af commit 20b06b7
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package kr.mafoo.photo.config;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebExchangeDecorator;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class RequestBodyCachingFilter implements WebFilter {

// TODO: 추후 정리 필요

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return DataBufferUtils.join(exchange.getRequest().getBody())
.flatMap(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);

ServerHttpRequestDecorator decoratedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public Flux<DataBuffer> getBody() {
return Flux.just(exchange.getResponse().bufferFactory().wrap(bytes));
}
};

ServerWebExchangeDecorator decoratedExchange = new ServerWebExchangeDecorator(exchange) {
@Override
public ServerHttpRequest getRequest() {
return decoratedRequest;
}
};

return chain.filter(decoratedExchange);
});
}
}

0 comments on commit 20b06b7

Please sign in to comment.