|
@@ -1,8 +1,17 @@
|
|
|
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;
|
|
@@ -14,10 +23,12 @@ import java.util.Base64;
|
|
|
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;
|
|
@@ -26,6 +37,8 @@ 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;
|
|
|
|
|
|
public class LifeCenterFunction {
|
|
|
public static String setURLEncode(String content, String lngType) throws UnsupportedEncodingException {
|
|
@@ -239,4 +252,123 @@ public class LifeCenterFunction {
|
|
|
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<String, String> 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<String, String> map) {
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ boolean first = true; // 첫 번째 매개변수 여부
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> 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();
|
|
|
+ }
|
|
|
}
|