package com.cm.common.utils;

import com.cm.common.exception.base.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @ClassName: RequestUtil
 * @Description: 请求工具类
 * @Author: WangGeng
 * @Date: 2019/3/14 11:38 AM
 * @Version: 1.0
 **/
public class RequestUtil {

    private static final Logger LOG = LoggerFactory.getLogger(RequestUtil.class);
    public static final String UNKNOWN = "unknown";

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取IP
     *
     * @return
     */
    public static String getRequestIp() {
        return getRequestIp(getRequest());
    }

    /**
     * 请求IP
     *
     * @return
     */
    public static String getRequestIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "127.0.0.1";
        }
        // 多次反向代理后会有多个ip值，第一个ip才是真实ip
        if(ip.indexOf(",") != -1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }

    /**
     * 下载
     *
     * @param response
     * @param filePath
     * @throws SystemException
     */
    public static void downLoad(HttpServletResponse response, String filePath) throws SystemException {
        try {
            String[] filePathArray = filePath.split("/");
            String fileName = filePathArray[filePathArray.length - 1];
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            FileInputStream fileInputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream();
            for (byte[] buf = new byte[1024]; fileInputStream.read(buf) > 0; ) {
                outputStream.write(buf);
            }
            outputStream.flush();
        } catch (FileNotFoundException e) {
            LOG.error(e.getMessage(), e);
            throw new SystemException();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            throw new SystemException();
        }

    }

}
