/** 
 * CommonsUtils.java
 * @author 임새미 
 * @since 2016. 10. 14.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 14. 	    임새미				       최초생성
 * 
 */
package kcg.faics.cmmn;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/** 
 * CommonsUtils.java
 * @author 임새미 
 * @since 2016. 10. 14.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 14. 	    임새미				       최초생성
 *  2016. 12. 14. 	    하영지				       isEmpty 생성
 * 
 */
public class CommonsUtils {

	public static String sqlInjectionFilter(String str){
		Pattern evilChars = Pattern.compile("\\[\'-#()@;=*/+%\\]");
		str = evilChars.matcher(str).replaceAll("");
		
		String lowerStr = str.toLowerCase();
		
		if (lowerStr.contains("union") || lowerStr.contains("select") || lowerStr.contains("insert") || lowerStr.contains("drop") 
				|| lowerStr.contains("update") || lowerStr.contains("delete") || lowerStr.contains("join") || lowerStr.contains("from")
				|| lowerStr.contains("where") || lowerStr.contains("substr") || lowerStr.contains("user_tables") || lowerStr.contains("user_tab_columns")) {
			
			str = lowerStr;
			str = str.replaceAll("union", "q-union");			
			str = str.replaceAll("select", "q-select");			
			str = str.replaceAll("insert", "q-insert");			
			str = str.replaceAll("drop", "q-drop");			
			str = str.replaceAll("update", "q-update");			
			str = str.replaceAll("delete", "q-delete");			
			str = str.replaceAll("join", "q-join");			
			str = str.replaceAll("from", "q-from");			
			str = str.replaceAll("where", "q-where");			
			str = str.replaceAll("substr", "q-substr");			
			str = str.replaceAll("user_tables", "q-user_tables");			
			str = str.replaceAll("user_tab_columns", "q-user_tab_columns");			
		}
		return str;
	}
	
	public static HashMap<String, Object> getParameterMap(HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String, Object> paramMap = request.getParameterMap();
		HashMap<String, Object> newMap = new HashMap<String, Object>();
		
		Set<String> keySet = paramMap.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			if (paramMap.get(key) instanceof String[]) {
				newMap.put(key, ((String[]) paramMap.get(key))[0]);
			} else {
				newMap.put(key, paramMap.get(key));
			}
		}
		return newMap;
	}
	
	/**
	 * 객체가 비어있는지 체크
	 * 
	 * @param s 체크할 객체
	 * @return 비어있음:true
	 */
	public static boolean isEmpty(final Object s) {
        if (s == null) {
            return true;
        }
        if ((s instanceof String) && ((String.valueOf(s)).trim().length() == 0)) {
            return true;
        }
        if (s instanceof Map) {
            return ((Map<?, ?>)s).isEmpty();
        }
        if (s instanceof List) {
            return ((List<?>)s).isEmpty();
        }
        if (s instanceof Object[]) {
            return (((Object[])s).length == 0);
        }
        return false;
    }
}
