|
|
@@ -0,0 +1,323 @@
|
|
|
+package com.tsi.kafka.security.auth.api;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.GsonBuilder;
|
|
|
+import com.tsi.kafka.security.auth.api.result.TscSsipApiResultKafkaAuth;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONArray;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONException;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+
|
|
|
+public class HttpUtils {
|
|
|
+
|
|
|
+ public static void get(String strUrl) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(strUrl);
|
|
|
+ HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
+ con.setConnectTimeout(5000); //서버에 연결되는 Timeout 시간 설정
|
|
|
+ con.setReadTimeout(5000); // InputStream 읽어 오는 Timeout 시간 설정
|
|
|
+ //TODO con.addRequestProperty("x-api-key", RestTestCommon.API_KEY); //key 값 설정
|
|
|
+
|
|
|
+ con.setRequestMethod("GET");
|
|
|
+
|
|
|
+ // URLConnection 에 대한 doOutput 필드값을 지정된 값으로 설정한다.
|
|
|
+ // URL 연결은 입출력에 사용될 수 있다.
|
|
|
+ // URL 연결을 출력용으로 사용하려는 경우 DoOutput 플래그를 true 로 설정하고, 그렇지 않은 경우는 false 로 설정해야 한다. 기본값은 false 이다.
|
|
|
+
|
|
|
+ con.setDoOutput(false);
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ //Stream 을 처리해줘야 하는 귀찮음이 있음.
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line).append("\n");
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ System.out.println("" + sb.toString());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ System.out.println(con.getResponseMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ System.err.println(e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void post(String strUrl, String jsonMessage) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(strUrl);
|
|
|
+ HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
+ con.setConnectTimeout(5000); //서버에 연결되는 Timeout 시간 설정
|
|
|
+ con.setReadTimeout(5000); // InputStream 읽어 오는 Timeout 시간 설정
|
|
|
+ //con.addRequestProperty("x-api-key", RestTestCommon.API_KEY); //key값 설정
|
|
|
+
|
|
|
+ con.setRequestMethod("POST");
|
|
|
+
|
|
|
+ //json 으로 message 를 전달하고자 할 때
|
|
|
+ con.setRequestProperty("Content-Type", "application/json");
|
|
|
+ con.setDoInput(true);
|
|
|
+ con.setDoOutput(true); //POST 데이터를 OutputStream으로 넘겨 주겠다는 설정
|
|
|
+ con.setUseCaches(false);
|
|
|
+ con.setDefaultUseCaches(false);
|
|
|
+
|
|
|
+ OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
|
|
+ wr.write(jsonMessage); //json 형식의 message 전달
|
|
|
+ wr.flush();
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ //Stream을 처리해줘야 하는 귀찮음이 있음.
|
|
|
+ BufferedReader br = new BufferedReader(
|
|
|
+ new InputStreamReader(con.getInputStream(), "utf-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line).append("\n");
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ System.out.println("" + sb.toString());
|
|
|
+ } else {
|
|
|
+ System.out.println(con.getResponseMessage());
|
|
|
+ }
|
|
|
+ } catch (Exception e){
|
|
|
+ System.err.println(e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void callApi(String apiUrl, JSONObject params, String type){
|
|
|
+
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ JSONObject responseJson = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ //URL 설정
|
|
|
+ URL url = new URL(apiUrl);
|
|
|
+
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ // type 의 경우 POST, GET, PUT, DELETE 가능
|
|
|
+ conn.setRequestMethod(type);
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json");
|
|
|
+ conn.setRequestProperty("Transfer-Encoding", "chunked");
|
|
|
+ conn.setRequestProperty("Connection", "keep-alive");
|
|
|
+ conn.setDoOutput(true);
|
|
|
+
|
|
|
+
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
|
|
|
+ // JSON 형식의 데이터 셋팅
|
|
|
+ JSONObject commands = new JSONObject();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+
|
|
|
+ // JSON 형식의 데이터 셋팅 끝
|
|
|
+
|
|
|
+ // 데이터를 STRING 으로 변경
|
|
|
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
+ String jsonOutput = gson.toJson(params);
|
|
|
+
|
|
|
+ bw.write(params.toString());
|
|
|
+ bw.flush();
|
|
|
+ bw.close();
|
|
|
+
|
|
|
+ // 보내고 결과값 받기
|
|
|
+ int responseCode = conn.getResponseCode();
|
|
|
+ if (responseCode == 200) {
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+ responseJson = new JSONObject(sb.toString());
|
|
|
+
|
|
|
+ // 응답 데이터
|
|
|
+ System.out.println("responseJson :: " + responseJson);
|
|
|
+ }
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static TscSsipApiResultKafkaAuth getKafkaLoginAuth(String apiUrl, JSONObject params) {
|
|
|
+
|
|
|
+ TscSsipApiResultKafkaAuth result = new TscSsipApiResultKafkaAuth(0, "Success");
|
|
|
+ result.setResult("0");
|
|
|
+
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ JSONObject responseJson = null;
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ try {
|
|
|
+ URL url = new URL(apiUrl);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setConnectTimeout(5000); //서버에 연결되는 Timeout 시간 설정
|
|
|
+ conn.setReadTimeout(5000); // InputStream 읽어 오는 Timeout 시간 설정
|
|
|
+
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json;utf-8");
|
|
|
+ conn.setRequestProperty("Accept", "application/json");
|
|
|
+ //conn.setRequestProperty("Connection", "keep-alive");
|
|
|
+ conn.setDoOutput(true);
|
|
|
+
|
|
|
+
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
|
|
|
+ // JSON 형식의 데이터 셋팅
|
|
|
+ JSONObject commands = new JSONObject();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+
|
|
|
+ // JSON 형식의 데이터 셋팅 끝
|
|
|
+
|
|
|
+ // 데이터를 STRING 으로 변경
|
|
|
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
+ String jsonOutput = gson.toJson(params);
|
|
|
+
|
|
|
+ bw.write(params.toString());
|
|
|
+ bw.flush();
|
|
|
+ bw.close();
|
|
|
+
|
|
|
+ // 보내고 결과값 받기
|
|
|
+ int responseCode = conn.getResponseCode();
|
|
|
+ if (responseCode == 200) {
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+ responseJson = new JSONObject(sb.toString());
|
|
|
+
|
|
|
+ result = objectMapper.readValue(sb.toString(), TscSsipApiResultKafkaAuth.class);
|
|
|
+
|
|
|
+ // 응답 데이터
|
|
|
+ System.out.println("responseJson :: " + responseJson);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ result.setResult(String.valueOf(responseCode));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (MalformedURLException e) {
|
|
|
+ result.setResult("11");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ result.setResult("12");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ catch (JSONException e) {
|
|
|
+ result.setResult("13");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main1(String[] args) {
|
|
|
+ String token = "YOUR_ACCESS_TOKEN";// 네아로 접근 토큰 값";
|
|
|
+ String header = "Bearer " + token; // Bearer 다음에 공백 추가
|
|
|
+ try {
|
|
|
+ String apiURL = "https://openapi.naver.com/v1/nid/me";
|
|
|
+ URL url = new URL(apiURL);
|
|
|
+ HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
|
|
+ con.setRequestMethod("GET");
|
|
|
+ con.setRequestProperty("Authorization", header);
|
|
|
+ int responseCode = con.getResponseCode();
|
|
|
+ BufferedReader br;
|
|
|
+ if(responseCode==200) { // 정상 호출
|
|
|
+ br = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
+ } else { // 에러 발생
|
|
|
+ br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
|
|
|
+ }
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+ while ((inputLine = br.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ System.out.println(response.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main2(String[] args) {
|
|
|
+ String clientId = "YOUR_CLIENT_ID";//애플리케이션 클라이언트 아이디값";
|
|
|
+ String clientSecret = "YOUR_CLIENT_SECRET";//애플리케이션 클라이언트 시크릿값";
|
|
|
+ try {
|
|
|
+ String text = URLEncoder.encode("안녕하세요. 오늘 기분은 어떻습니까?", "UTF-8");
|
|
|
+ String apiURL = "https://openapi.naver.com/v1/papago/n2mt";
|
|
|
+ URL url = new URL(apiURL);
|
|
|
+ HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
|
|
+ con.setRequestMethod("POST");
|
|
|
+ con.setRequestProperty("X-Naver-Client-Id", clientId);
|
|
|
+ con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
|
|
|
+ // post request
|
|
|
+ String postParams = "source=ko&target=en&text=" + text;
|
|
|
+ con.setDoOutput(true);
|
|
|
+ DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
|
|
+ wr.writeBytes(postParams);
|
|
|
+ wr.flush();
|
|
|
+ wr.close();
|
|
|
+ int responseCode = con.getResponseCode();
|
|
|
+ BufferedReader br;
|
|
|
+ if(responseCode==200) { // 정상 호출
|
|
|
+ br = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
+ } else { // 에러 발생
|
|
|
+ br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
|
|
|
+ }
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+ while ((inputLine = br.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ System.out.println(response.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void RestCall(String paramUrl, JSONObject jsonObject){
|
|
|
+ try {
|
|
|
+ URL url = new URL(paramUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ //TODO conn.setRequestProperty("X-Auth-Token", API_KEY);
|
|
|
+ conn.setRequestProperty("X-Data-Type", "application/json");
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json");
|
|
|
+ conn.setDoOutput(true);
|
|
|
+
|
|
|
+ OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
|
|
|
+ osw.write(jsonObject.toString());
|
|
|
+ osw.flush();
|
|
|
+ osw.close();
|
|
|
+
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
|
|
|
+ if (conn.getResponseCode() != 200) {
|
|
|
+ System.out.println("Failed: HTTP error code : " + conn.getResponseCode());
|
|
|
+ throw new RuntimeException("Failed: HTTP error code : " + conn.getResponseCode());
|
|
|
+ } else {
|
|
|
+ System.out.println("발송 성공");
|
|
|
+ }
|
|
|
+
|
|
|
+ String line = null;
|
|
|
+ while((line = br.readLine()) != null){
|
|
|
+ System.out.println(line);
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ conn.disconnect();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("RestCall Fail : " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|