puzhibing
2023-07-17 f195cf48cf57635c8848b2bc32afd4541c4090ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//package cn.mb.cloud.gateway.filter;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.*;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.Set;
//
//
//@Slf4j
//@Component
//public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
//
//    @Override
//    public int getOrder() {
//        return Ordered.HIGHEST_PRECEDENCE;
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest req = exchange.getRequest();
//        ServerHttpResponse res = exchange.getResponse();
//        String method = req.getMethodValue();
//        MediaType ct = req.getHeaders().getContentType();
//        if (HttpMethod.POST.matches(method)) {
//            return DataBufferUtils.join(req.getBody()).flatMap(dataBuffer -> {
//                byte[] bytes = new byte[dataBuffer.readableByteCount()];
//                dataBuffer.read(bytes);
////                String bodyStr = new String(bytes, StandardCharsets.UTF_8);
////                exchange.getAttributes().put("POST_BODY", bodyStr);
//                DataBufferUtils.release(dataBuffer);
//                Flux<DataBuffer> cachedFlux = Flux.defer(() -> {
//                    DataBuffer buffer = res.bufferFactory().wrap(bytes);
//                    return Mono.just(buffer);
//                });
//                ServerHttpRequest mutateReq = new ServerHttpRequestDecorator(req) {
//                    @Override
//                    public Flux<DataBuffer> getBody() {
//                        return cachedFlux;
//                    }
//                };
//                return chain.filter(exchange.mutate().request(mutateReq).build());
//            });
//        }
//        return chain.filter(exchange);
//    }
//
//}