//package com.nantian.sc.cloud.common.gateway.filter;
//
//import com.nantian.sc.cloud.common.gateway.utils.AesUtil;
//import org.reactivestreams.Publisher;
//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.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.core.io.buffer.DefaultDataBufferFactory;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
//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.Charset;
//
///**
// * @author hzp
// * @title: GlobalResponseDecodeFilter
// * @projectName cloud2020
// * @description: TODO
// * @date 2022/09/01,0001下午 23:16
// */
//@Component
//public class GlobalResponseDecodeFilter implements GlobalFilter, Ordered {
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        String bgDebug = exchange.getAttributeOrDefault(ConstantFilter.BG_DEBUG_KEY, ConstantFilter.REQ_RES_ENCRYPT);
////        String skipFileDownload = exchange.getAttributeOrDefault(ConstantFilter.BG_DEBUG_KEY, ConstantFilter.REQ_RES_NALMORE);
//
//        ServerHttpResponse originalResponse = exchange.getResponse();
//        DataBufferFactory bufferFactory = originalResponse.bufferFactory();
//        ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
//
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//
//                if (body instanceof Flux) {
//
//                    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
//                    return super.writeWith(fluxBody.buffer().map(dataBuffer -> {
//
//                        DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
//                        DataBuffer join = dataBufferFactory.join(dataBuffer);
//
//                        byte[] content = new byte[join.readableByteCount()];
//                        join.read(content);
//                        //释放掉内存
//                        DataBufferUtils.release(join);
//                        // 正常返回的数据
//                        String rootData = new String(content, Charset.forName("UTF-8"));
//                        byte[] respData = rootData.getBytes();
//
//                        if(ConstantFilter.REQ_RES_ENCRYPT.equals(bgDebug)){
//                            String encryptData = null;
//                            try {
//                                encryptData = AesUtil.encrypt(rootData, "MP5v0^zee5Qlgq5V");
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                            respData = encryptData.getBytes();
//                        }
//
//                        // 加密后的数据返回给客户端
//                        byte[] uppedContent = new String(respData, Charset.forName("UTF-8")).getBytes();
//                        return bufferFactory.wrap(uppedContent);
//                    }));
//                }
//                return super.writeWith(body);
//            }
//        };
//        return chain.filter(exchange.mutate().response(decoratedResponse).build());
//    }
//
//    @Override
//    public int getOrder() {
//        return -22;
//    }
//}
