package com.its.web.util;

import com.its.web.dto.message.ResultDto;

import javax.servlet.http.HttpServletRequest;

public class CommonUtil {

    public static ResultDto getResultDto(String success, String message) {
        ResultDto result = new ResultDto();
        if (success == null && message == null) {
            success = "F";
            message = "-";
        }
        result.setMessage(message);
        result.setSuccess(success);
        return result;
    }

    public static String getHttpServletRemoteIP(HttpServletRequest request) {
        if (request == null) {
            return "";
        }

        String ipAddress = request.getHeader("X-FORWARDED-FOR");
        // proxy 환경일 경우
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        // 웹로직 서버일 경우
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        // 기타
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr() ;
        }
        //-Djava.net.preferIPv4Stack=true
        if (ipAddress.equals("0:0:0:0:0:0:0:1"))   //==> ipv6 <== default
        {
            ipAddress = "127.0.0.1";   //==> localhost
        }
        return ipAddress;
    }
}
