|
@@ -0,0 +1,121 @@
|
|
|
|
+package com.icontrols.kakao.controller;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+import org.springframework.web.server.ResponseStatusException;
|
|
|
|
+
|
|
|
|
+@Controller
|
|
|
|
+public class MainController {
|
|
|
|
+
|
|
|
|
+ public String COMPLEX_SERVER = "http://61.33.215.54:8002/kakao/danzi"; // 의현씨 컴퓨터
|
|
|
|
+
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(MainController.class);
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/kakao", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
|
|
|
|
+ public @ResponseBody String From3rdServer(@RequestHeader HttpHeaders headers, HttpEntity<String> httpEntity,
|
|
|
|
+ HttpServletResponse response) {
|
|
|
|
+ logger.info("/iotservice");
|
|
|
|
+ String answer = "";
|
|
|
|
+ String body = httpEntity.getBody();
|
|
|
|
+ logger.info("Body: " + body);
|
|
|
|
+ JSONObject obj = new JSONObject(body);
|
|
|
|
+ String token = obj.get("authorization").toString();
|
|
|
|
+// JWTUtils jwt = new JWTUtils();
|
|
|
|
+// String complexIp = jwt.getCmpIpFromToken(token);
|
|
|
|
+// logger.info(complexIp);
|
|
|
|
+
|
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
|
+ factory.setConnectTimeout(3000);
|
|
|
|
+ RestTemplate template = new RestTemplate(factory);
|
|
|
|
+
|
|
|
|
+ response.setHeader("Content-Type", "application/json");
|
|
|
|
+// response.setHeader("Authorization", headers.get("Authorization").get(0));
|
|
|
|
+ HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
|
|
|
+ answer = template.postForObject(COMPLEX_SERVER, entity, String.class);
|
|
|
|
+
|
|
|
|
+ return answer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/events", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
|
|
|
|
+ public @ResponseBody String FromValleyServerEvent(@RequestHeader HttpHeaders headers, HttpEntity<String> httpEntity,
|
|
|
|
+ HttpServletResponse response) {
|
|
|
|
+
|
|
|
|
+ logger.info("/iotservice");
|
|
|
|
+ String KAKAO_SERVER_EVENT = "https://kakaohome-api.i.kakao.com/events";
|
|
|
|
+ String answer = "";
|
|
|
|
+ String body = httpEntity.getBody();
|
|
|
|
+ logger.info("Body: " + body);
|
|
|
|
+ JSONObject obj = new JSONObject(body);
|
|
|
|
+ String token = obj.get("authorization").toString();
|
|
|
|
+// JWTUtils jwt = new JWTUtils();
|
|
|
|
+// String complexIp = jwt.getCmpIpFromToken(token);
|
|
|
|
+// logger.info(complexIp);
|
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
|
+ factory.setConnectTimeout(3000);
|
|
|
|
+ RestTemplate template = new RestTemplate(factory);
|
|
|
|
+ response.setHeader("Content-Type", "application/json");
|
|
|
|
+ if (headers.get("Authorization").get(0) == null)
|
|
|
|
+ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
|
|
|
|
+ response.setHeader("Authorization", headers.get("Authorization").get(0));
|
|
|
|
+ HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
|
|
|
+ answer = template.postForObject(KAKAO_SERVER_EVENT, entity, String.class);
|
|
|
|
+
|
|
|
|
+ return answer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/events/token", method = RequestMethod.POST)
|
|
|
|
+ public @ResponseBody String FromValleyServerToken(@RequestHeader HttpHeaders headers,
|
|
|
|
+ @RequestParam(value = "refresh_token", required = true) String refresh_token,
|
|
|
|
+ @RequestParam(value = "grant_type", required = true) String grant_type, HttpEntity<String> httpEntity,
|
|
|
|
+ HttpServletResponse response) {
|
|
|
|
+ logger.info(refresh_token);
|
|
|
|
+ String KAKAO_SERVER_EVENT = "https://kakaohome-api.i.kakao.com/events/token";
|
|
|
|
+ String answer = "";
|
|
|
|
+
|
|
|
|
+ MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
|
|
|
|
+ multiValueMap.add("refresh_token", refresh_token);
|
|
|
|
+ multiValueMap.add("grant_type", grant_type);
|
|
|
|
+
|
|
|
|
+// HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
|
+// factory.setConnectTimeout(3000);
|
|
|
|
+
|
|
|
|
+ RestTemplate template = new RestTemplate();
|
|
|
|
+
|
|
|
|
+ HttpHeaders header = new HttpHeaders();
|
|
|
|
+ header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
+ header.add("Authorization", headers.get("Authorization").get(0));
|
|
|
|
+
|
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(multiValueMap, header);
|
|
|
|
+ answer = template.postForObject(KAKAO_SERVER_EVENT, entity, String.class);
|
|
|
|
+
|
|
|
|
+ return answer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
|
|
|
|
+ public @ResponseBody String Complex(HttpEntity<String> httpEntity) {
|
|
|
|
+ logger.info("/test");
|
|
|
|
+ String answer = "";
|
|
|
|
+ String body = httpEntity.getBody();
|
|
|
|
+ logger.info("Body: " + body);
|
|
|
|
+
|
|
|
|
+ return body;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|