package com.crm.gateway.util;


import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by max on 2017/11/2.
 */
public class InteceptorResultUtil {

    public static void returnResult(HttpServletResponse response, BaseResultDto<Object> baseResultDto) throws Exception{
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
        response.setContentType("application/json");
        PrintWriter printWriter = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writeValueAsString(baseResultDto);
        printWriter.write(result);
        printWriter.flush();
        if(printWriter != null){
            printWriter.close();
        }
    }
}
