-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create RequestBodyCachingFilter
- Loading branch information
1 parent
88a73af
commit 20b06b7
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
photo-service/src/main/java/kr/mafoo/photo/config/RequestBodyCachingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
|