package nlib.sch.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.json.JSONArray; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.ui.ModelMap; import nlib.cmm.Constant; import nlib.cmm.service.CmmAmsDocTableOfContentsVO; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import egovframework.rte.fdl.cmmn.exception.EgovBizException; import egovframework.rte.fdl.property.EgovPropertyService; import egovframework.rte.fdl.security.userdetails.util.EgovUserDetailsHelper; import egovframework.rte.psl.dataaccess.util.EgovMap; /** * * Util 클래스 * */ @Component public class NlibCmmUtil { private static final Logger LOGGER = LoggerFactory.getLogger(NlibCmmUtil.class); public static String DEFAULT_DELIMITER = "-"; protected static EgovPropertyService propertyService; @Resource(name = "propertiesService") private EgovPropertyService propertyServiceInstance; @PostConstruct private void init() { propertyService = propertyServiceInstance; } public static String reflectionToString(Object obj) { return ToStringBuilder.reflectionToString(obj); } public static boolean isRoleKdm() { List listRole = (List) EgovUserDetailsHelper.getAuthorities(); for (String role : listRole) { if (Constant.AUTH_ROLE_KDM.equals(role)) { return true; } } return false; } public static boolean isRoleSysKdm() { List listRole = (List) EgovUserDetailsHelper.getAuthorities(); for (String role : listRole) { if (Constant.AUTH_ROLE_SYM.equals(role)) { return true; } } return false; } public static String clearXSS(String value) { if (value == null || value.trim().equals("")) { return ""; } String returnValue = value; returnValue = returnValue.replaceAll("&", "&"); returnValue = returnValue.replaceAll("<", "<"); returnValue = returnValue.replaceAll(">", ">"); returnValue = returnValue.replaceAll("'", "'"); returnValue = returnValue.replaceAll("eval\\((.*)\\)", ""); returnValue = returnValue.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\""); returnValue = returnValue.replaceAll("\"", """); returnValue = returnValue.replaceAll("\\(", "(").replaceAll("\\)", ")"); returnValue = returnValue.replaceAll("script", ""); returnValue = returnValue.replaceAll("alert", ""); return returnValue; } public static String escapeNewlineToBr(String text) { String str = text; if (StringUtils.isEmpty(str)) return str; if (str.indexOf("\\r") >= 0) str = str.replaceAll("\\\\r", "\r"); if (str.indexOf("\\n") >= 0) str = str.replaceAll("\\\\n", "\n"); if (str.indexOf("\r\n") >= 0 || str.indexOf("\n") >= 0 || str.indexOf("\r") >= 0) { str = str.replaceAll("\\r\\n", "
"); str = str.replaceAll("\\r", "
"); str = str.replaceAll("\\n", "
"); } return str; } public static String escapeNewlineToSpace(String text) { String str = text; if (StringUtils.isEmpty(str)) return str; if (str.indexOf("\\r") >= 0) str = str.replaceAll("\\\\r", "\r"); if (str.indexOf("\\n") >= 0) str = str.replaceAll("\\\\n", "\n"); if (str.indexOf("\r\n") >= 0 || str.indexOf("\n") >= 0 || str.indexOf("\r") >= 0) { str = str.replaceAll("\\r\\n", ""); str = str.replaceAll("\\r", ""); str = str.replaceAll("\\n", ""); } str = str.replaceAll("\"", ""); str = str.replaceAll("'", ""); return str; } public static String cut(String text, int size) { return cut(text, size, ""); } public static String cut(String text, int size, String suffix) { if (text == null) { return ""; } if (text.length() < size) { return text; } size = size - suffix.length(); return StringUtils.substring(text, 0, size) + suffix; } public static boolean isConsecutive(String str, int n) { int sumOfDifferences = 0; int lastTemp = 0; Boolean consecutive = false; for (int i = 0; i < str.length() - 1; i++) { int temp = str.charAt(i) - str.charAt(i + 1); if (temp != lastTemp) { sumOfDifferences = 0; } if (temp == 1 || temp == -1) { sumOfDifferences += temp; } else { sumOfDifferences = 0; } if (sumOfDifferences == n - 1 || sumOfDifferences == ((n - 1) * -1)) { consecutive = true; break; } lastTemp = temp; } return consecutive; } public static boolean regexFind(String str, String regex) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.find(); } public static String getIpAddress(HttpServletRequest req) { String ipAddress = ""; if (req.getHeader("X-Forwarded-For") == null) { ipAddress = req.getRemoteAddr(); } else { ipAddress = req.getHeader("X-Forwarded-For"); int idx = ipAddress.indexOf(','); if (idx > -1) { ipAddress = ipAddress.substring(0, idx); } } return ipAddress; } public static String returnUrl(HttpServletRequest req, String parameterName) { String url = req.getRequestURI(); if (url.startsWith("/logout") || url.startsWith("/error") || "/".equals(url)) { return ""; } String queryString = req.getQueryString(); if (queryString != null) { url += "?" + queryString; try { url = URLEncoder.encode(url, Constant.ENCODING_UTF8); } catch (UnsupportedEncodingException e) { url = ""; } } return "?" + parameterName + "=" + url; } public static String getUrl(HttpServletRequest req, String domain) { final String uri = req.getRequestURI(); final String queryString = req.getQueryString(); String url = ""; url = domain + uri; if (StringUtils.isNotBlank(queryString)) { url += "?" + queryString; } return url; } public static int getTridentVersion(String userAgent) { String lowerUserAgent = userAgent.toLowerCase(); int start = lowerUserAgent.indexOf("trident/"); if (start == -1) { return 0; } String trident = lowerUserAgent.substring(start + 8, start + 9); return NumberUtils.toInt(trident); } public static String getPropertyStr(String key) { String str = ""; if (org.apache.commons.lang3.StringUtils.isNotBlank(key)) str = propertyService.getString(key); return str; } public static String deleteChar(String str) { if (StringUtils.isEmpty(str)) return str; String regExpNumber = "[^0-9]"; return str.replaceAll(regExpNumber, ""); } @SuppressWarnings("rawtypes") public static boolean isEmpty(Map map, String key) { boolean isEmpty = true; if (map == null) return isEmpty; Object obj = map.get(key); if ( obj != null && !StringUtils.isEmpty(obj.toString()) && !(obj+"").equals("null")) { isEmpty = false; } return isEmpty; } public static boolean checkMaxbyte(String chkString, int maxbyte) { boolean checkResult = true; if (StringUtils.isBlank(chkString)) return checkResult; int bytes = 0; try { bytes = chkString.getBytes(Constant.ENCODING_UTF8).length; if (bytes > maxbyte) checkResult = false; } catch (UnsupportedEncodingException e) { LOGGER.error("fail in checkMaxbyte() maxbyte="+maxbyte+", bytes="+bytes+", chkString="+chkString, e); checkResult = false; } catch (Exception e) { LOGGER.error("fail in checkMaxbyte() maxbyte="+maxbyte+", bytes="+bytes+", chkString="+chkString, e); checkResult = false; } return checkResult; } public static String[] getValueArr(List list, String key) { String[] exidArr = null; if (list == null || list.size() == 0) return exidArr; exidArr = new String[list.size()]; for (int i=0; i