[ResponseHandlerFilter]

package com.ktds.act.apigw.filter;

import com.ktds.act.apigw.filter.model.TransactionDto;

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.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import com.ktds.act.apigw.filter.function.ResponseMethod;

import reactor.core.publisher.Mono;

@Slf4j
@Component
public class ResponseHandlerFilter implements GlobalFilter, Ordered {// 추출과 핸들링 기능 분리
	public static final String RESPONSE_HANDLER = "RESPONSE-HANDLER";
	
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		if((boolean) exchange.getAttribute("shouldBodyRef")) {
				return chain.filter(exchange);
			
		}else{
			return  chain.filter(exchange).then(Mono.fromRunnable(() -> {
				TransactionDto transaction = (TransactionDto) exchange.getAttribute(TransactionDto.TRANSACTION);
				//Thread.currentThread().setName(transaction.getComn().getTxId());
				byte[] body = null;
				ResponseMethod.getExchangeResponse(exchange, body, transaction);
				ResponseMethod.responseHandler(exchange, transaction);
				ResponseMethod.setExchangeResponse(exchange, transaction);

				log.info("ResponseHandlerFilter.filter - end");
			}));
		}

		
	}

	

	@Override
	public int getOrder() {
		return 5;
	}
}