/**
 * @(#)file    NetUtil.java
 * @(#)author  OK
 * @(#)version 1.0
 * @(#)date    2014.09.04
 * @(#)since   JDK 1.6.24
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.common.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.HttpServletRequest;

import kr.co.hsnc.common.logger.Logger;

public class NetUtil {

    
    /**
     * targetUrl에 해당하는 화면을 조회
     * @param targetUrl 조회할 화면의 URL(Http://localhost:80/test.html)
     * @return
     */
    public static String getHttpPage(String targetUrl) {
        URL url = null;
        URLConnection con = null;
        DataOutputStream out = null;
        InputStream in = null;
        InputStreamReader insr = null;

        BufferedReader br = null;
        StringBuffer buf = new StringBuffer();
        String str = "";
        try {
            url = new URL(targetUrl);
            con = url.openConnection();

            con.setDoInput(true);
            con.setDoOutput(true);

            con.setUseCaches(false);
            con.setRequestProperty("content-Type", "application/x-www-form-urlencoded;charset=utf-8");//euc-kr

            in = con.getInputStream();
            
            insr = new InputStreamReader(in);
            br = new BufferedReader(insr);
            
            String temp = null;
            
            while((temp = br.readLine())!=null) {             
                buf.append(temp);
                buf.append("\n");
            }       
            return buf.toString();
        }
        catch(Exception e) {
            e.printStackTrace(System.err);
            return null;
        }
        finally {
            try {
                if( out != null ) {
                    out.flush();
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            }
            catch(Exception e) {
            }
        }
    }
    
    /**
     * L4 장비 도입후 보완사항 - 클라이언트 유저의 IP 정보 획득 
     */
    public static String getUserAddr(HttpServletRequest request) { 
  
        String xForwardedFor  = request.getHeader("x-forwarded-for");
        String intelSourceIP  = request.getHeader("INTEL_SOURCE_IP");
        String RemoteAddr     = request.getRemoteAddr();
        String return_ip      = null;
        
        try {
 
            if ( xForwardedFor == null  || xForwardedFor.length() < 4 ) {
            	
            	if( intelSourceIP == null ) return_ip = RemoteAddr ;
            	else                        return_ip = intelSourceIP;
            	
            } else { 
            	
            	return_ip = xForwardedFor;
            }
        }
        catch(Exception wafe) {
            Logger.err.println("[WAFException]SignOnFilter.getUserAddr() -> " + wafe);
            wafe.printStackTrace();
            Logger.err.println("");
        }
        finally {
            return return_ip;
        }
	}
}