package com.lemon.lifecenter.common; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.spec.AlgorithmParameterSpec; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Base64; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Random; import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.Mac; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.lemon.lifecenter.dto.PatientDTO; public class LifeCenterFunction { public static String setURLEncode(String content, String lngType) throws UnsupportedEncodingException { return URLEncoder.encode(content, lngType); } public static String setURLDecode(String content, String lngType) throws UnsupportedEncodingException { return URLDecoder.decode(content, lngType); } public static String aesEncrypt(String encKey, byte[] iv, String paramInput) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec key = new SecretKeySpec(encKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); byte[] cipherText = cipher.doFinal(paramInput.getBytes("UTF-8")); return Base64.getEncoder().encodeToString(cipherText); } public static String aesDecrypt(String encKey, byte[] iv, String paramInput) throws Exception { byte[] textBytes = Base64.getDecoder().decode(paramInput); AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); SecretKeySpec newKey = new SecretKeySpec(encKey.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return new String(cipher.doFinal(textBytes), "UTF-8"); } public static ArrayList> parseJSON(String jsonString) { ArrayList> rtn = new ArrayList>(); try { JSONArray array = new JSONArray(jsonString); for (int i = 0; i < array.length(); i++) { HashMap map = new HashMap(); JSONObject object = array.getJSONObject(i); Iterator keys = object.keys(); while (keys.hasNext()) { String key = (String) keys.next(); String val = ""; if (object.isNull(key)) { val = ""; } else { if (object.get(key) instanceof Integer) { val = String.valueOf(object.getInt(key)); } else if (object.get(key) instanceof Long) { val = String.valueOf(object.getLong(key)); } else if (object.get(key) instanceof Double) { val = String.valueOf(object.getDouble(key)); } else { val = object.getString(key).equals(null) ? "" : object.getString(key); } } map.put(key, val); } rtn.add(i, map); map = null; } } catch (JSONException e) { e.printStackTrace(); } return rtn; } public static String getRandomUUID() { return UUID.randomUUID().toString().replaceAll("-", ""); } public static String exportOnlyNumber(String input) { return input.replaceAll("[^0-9?!\\.]", ""); } public static String getNow() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); return dateFormat.format(date); } public static String getPrevDate( String ymd ) throws ParseException { DateFormat df = new SimpleDateFormat( "yyyy-MM-dd" ); Date date = df.parse( ymd ); Calendar cal = Calendar.getInstance(); cal.setTime( date ); cal.add( Calendar.DATE, -1 ); return df.format( cal.getTime() ); } public static String getNextDate( String ymd ) throws ParseException { DateFormat df = new SimpleDateFormat( "yyyy-MM-dd" ); Date date = df.parse( ymd ); Calendar cal = Calendar.getInstance(); cal.setTime( date ); cal.add( Calendar.DATE, +1 ); return df.format( cal.getTime() ); } public static String getNow(String format) { DateFormat dateFormat = new SimpleDateFormat(format); Date date = new Date(); return dateFormat.format(date); } public static long getNowUnixTimeStamp() { return System.currentTimeMillis() / 1000; } public static long setFormatUnixTimeStamp(String data) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long timeStamp = 0; try { long time = dateFormat.parse(data).getTime(); timeStamp = time / 1000; } catch (ParseException e) { e.printStackTrace(); } return timeStamp; } public static String setFormatDateFromUnixTimeStamp(Date data) { SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return transFormat.format(data); } public static String getRandomNumber(int maxSize) { Random rand = new Random(); String code = ""; for (int i = 0; i < maxSize; i++) { code += rand.nextInt(9); } return code; } public static void scriptMessage(HttpServletResponse response, String script) { response.setContentType("text/html; charset=UTF-8"); PrintWriter out; try { out = response.getWriter(); out.println(""); out.flush(); } catch (IOException e) { e.printStackTrace(); } } public static String getRemoteAddr(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null) { ip = request.getRemoteAddr(); } return ip; } public static String setPhoneFormat(String number) { StringBuilder sb = new StringBuilder(15); StringBuilder temp = new StringBuilder(number); int tempLength = temp.length(); if (tempLength == 3) { sb.append(number); } else { while (tempLength < 10) { temp.insert(0, "0"); } char[] chars = temp.toString().toCharArray(); int size = chars.length; if (size < 11) { for (int i = 0; i < size; i++) { if (i == 3) { sb.append("-"); } else if (i == 6) { sb.append("-"); } sb.append(chars[i]); } } else { for (int i = 0; i < size; i++) { if (i == 3) { sb.append("-"); } else if (i == 7) { sb.append("-"); } sb.append(chars[i]); } } } return sb.toString(); } public static String removeTag(String html) throws Exception { return html.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", ""); } public static String getSalt() { String uniqId = ""; Random randomGenerator = new Random(); // length - set the unique Id length for (int length = 1; length <= 10; ++length) { int randomInt = randomGenerator.nextInt(10); // digit range from 0 - 9 uniqId += randomInt + ""; } return uniqId; } public static String getTimestamp() { long timestamp_long = System.currentTimeMillis() / 1000; String timestamp = Long.toString(timestamp_long); return timestamp; } private static final Logger logger = LoggerFactory.getLogger(LifeCenterFunction.class); public static String getSignature(String apiSecret, String salt, String timestamp) throws Exception { String signature = ""; try { String temp = timestamp + salt; logger.error("temp -- > " + temp); SecretKeySpec keySpec = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); // 바이너리를 hex로 변환 byte[] result = mac.doFinal(temp.getBytes()); char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[result.length * 2]; for (int i = 0; i < result.length; i++) { int positive = result[i] & 0xff; hexChars[i * 2] = hexArray[positive >>> 4]; hexChars[i * 2 + 1] = hexArray[positive & 0x0F]; } signature = new String(hexChars); } catch (Exception e) { e.printStackTrace(); } return signature.toLowerCase(); } public static String httpUrlConnection(String apiUrl, HashMap hash) { String result = ""; URL url; try { url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Accept", "application/json"); conn.setRequestMethod("POST"); // post방식 통신 conn.setDoOutput(true); // 쓰기모드 지정 conn.setDoInput(true); // 읽기모드 지정 conn.setUseCaches(false); // 캐싱데이터를 받을지 안받을지 conn.setDefaultUseCaches(false); // 캐싱데이터 디폴트 값 설정 conn.setReadTimeout(10000); // 타임아웃 10초 OutputStream os = conn.getOutputStream(); // 서버로 보내기 위한 출력 스트림 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); // UTF-8로 전송 bw.write(getPostString(hash)); // 매개변수 전송 bw.flush(); bw.close(); os.close(); InputStream is = conn.getInputStream(); //데이타를 받기위 구멍을 열어준다 StringBuilder builder = new StringBuilder(); //문자열을 담기 위한 객체 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); //문자열 셋 세팅 String line; while ((line = reader.readLine()) != null) { builder.append(line+ "\n"); } // logger.error("httpUrlConnection Result -- > " + builder.toString()); result = builder.toString(); conn.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } private static String getPostString(HashMap map) { StringBuilder result = new StringBuilder(); boolean first = true; // 첫 번째 매개변수 여부 for (Map.Entry entry : map.entrySet()) { if (first) first = false; else // 첫 번째 매개변수가 아닌 경우엔 앞에 &를 붙임 result.append("&"); try { // UTF-8로 주소에 키와 값을 붙임 result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } catch (UnsupportedEncodingException ue) { ue.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return result.toString(); } public static String phone(String src) { if (src == null) { return ""; } if (src.length() == 8) { return src.replaceFirst("^([0-9]{4})([0-9]{4})$", "$1-$2"); } else if (src.length() == 12) { return src.replaceFirst("(^[0-9]{4})([0-9]{4})([0-9]{4})$", "$1-$2-$3"); } return src.replaceFirst("(^02|[0-9]{3})([0-9]{3,4})([0-9]{4})$", "$1-$2-$3"); } public static String getDisease(PatientDTO dto) { String highBloodPressureCheck = dto.getHighBloodPressureCheck().equals("Y") ? "고혈압" : ""; String lowBloodPressureCheck = dto.getLowBloodPressureCheck().equals("Y") ? "저혈압" : ""; String organTransplantCheck = dto.getOrganTransplantCheck().equals("Y") ? "장기이식(신장, 간 등)" : ""; String diabetesCheck = dto.getDiabetesCheck().equals("Y") ? "당뇨" : ""; String respiratoryDiseaseCheck = dto.getRespiratoryDiseaseCheck().equals("Y") ? "호흡기질환" : ""; String immunologicalDiseaseCheck = dto.getImmunologicalDiseaseCheck().equals("Y") ? "면역질환(류마티스 등)" : ""; String heartDisease = dto.getHeartDisease().equals("Y") ? "심장질환" : ""; String liverDisease = dto.getLiverDisease().equals("Y") ? "간질환" : ""; String operation = dto.getOperation().equals("Y") ? "수술(" + dto.getOperationContent() + ")" : ""; String allergyCheck = dto.getAllergyCheck().equals("Y") ? "알레르기" : ""; String kidneyDisease = dto.getKidneyDisease().equals("Y") ? "신장질환" : ""; String cancerName = dto.getCancerName() == null ? "" : "(" + dto.getCancerName() + ")"; String cancerCheck = dto.getCancerCheck().equals("Y") ? "암" + cancerName : ""; String etcContent = dto.getEtcContentDisease() == null ? "" : dto.getEtcContentDisease(); String ectCheckDisease = dto.getEtcCheckDisease().equals("Y") ? "기타(" + etcContent + ")" : ""; ArrayList disease = new ArrayList(); disease.add(highBloodPressureCheck); disease.add(lowBloodPressureCheck); disease.add(organTransplantCheck); disease.add(diabetesCheck); disease.add(respiratoryDiseaseCheck); disease.add(immunologicalDiseaseCheck); disease.add(heartDisease); disease.add(liverDisease); disease.add(operation); disease.add(allergyCheck); disease.add(kidneyDisease); disease.add(cancerCheck); disease.add(ectCheckDisease); String strDisease = ""; for (int i = 0; i < disease.size(); i++) { String str = disease.get(i); if (!str.equals("")) { strDisease += str; strDisease += ", "; } } strDisease = strDisease.trim(); if (!strDisease.equals("")) { strDisease = strDisease.substring(0, strDisease.length()-1); } return strDisease; } public static String getSymptom(PatientDTO dto) { String feverCheck = dto.getFeverCheck().equals("Y") ? "열감(열나는 느낌)" : ""; String coughCheck = dto.getCoughCheck().equals("Y") ? "기침" : ""; String colic = dto.getColic().equals("Y") ? "복통(배아픔)" : ""; String coldFitCheck = dto.getColdFitCheck().equals("Y") ? "오한(추운 느낌)" : ""; String sputumCheck = dto.getSputumCheck().equals("Y") ? "가래" : ""; String ocinCheck = dto.getOcinCheck().equals("Y") ? "오심(구역질)" : ""; String chestPain = dto.getChestPain().equals("Y") ? "흉통" : ""; String noseCheck = dto.getNoseCheck().equals("Y") ? "콧물 또는 코 막힘" : ""; String vomitingCheck = dto.getVomitingCheck().equals("Y") ? "구토" : ""; String musclePainCheck = dto.getMusclePainCheck().equals("Y") ? "근육통(몸살)" : ""; String soreThroatCheck = dto.getSoreThroatCheck().equals("Y") ? "인후통(목 아픔)" : ""; String diarrheaCheck = dto.getDiarrheaCheck().equals("Y") ? "설사" : ""; String headacheCheck = dto.getHeadacheCheck().equals("Y") ? "두통(머리아픔)" : ""; String dyspneaCheck = dto.getDyspneaCheck().equals("Y") ? "호흡곤란(숨가쁨)" : ""; String fatigueCheck = dto.getFatigueCheck().equals("Y") ? "권태감(피로감)" : ""; String etcContent = dto.getEtcContentSymptom() == null ? "" : dto.getEtcContentSymptom(); String ectCheckSymptom = dto.getEtcCheckSymptom().equals("Y") ? "기타(" + etcContent + ")" : ""; String strSymptom = ""; ArrayList symptom = new ArrayList(); symptom.add(feverCheck); symptom.add(coughCheck); symptom.add(colic); symptom.add(coldFitCheck); symptom.add(sputumCheck); symptom.add(ocinCheck); symptom.add(chestPain); symptom.add(noseCheck); symptom.add(vomitingCheck); symptom.add(musclePainCheck); symptom.add(soreThroatCheck); symptom.add(diarrheaCheck); symptom.add(headacheCheck); symptom.add(dyspneaCheck); symptom.add(fatigueCheck); symptom.add(ectCheckSymptom); for (int i = 0; i < symptom.size(); i++) { String str = symptom.get(i); if (!str.equals("")) { strSymptom += str; strSymptom += ", "; } } strSymptom = strSymptom.trim(); if (!strSymptom.equals("")) { strSymptom = strSymptom.substring(0, strSymptom.length() - 1); } return strSymptom; } }