TscSsipAppController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.tsi.api.server.controller;
  2. import com.tsi.api.server.controller.result.*;
  3. import com.tsi.api.server.error.TscSsipApiErrorCode;
  4. import com.tsi.api.server.service.TscSsipAppService;
  5. import com.tsi.api.server.util.ApiUtils;
  6. import com.tsi.api.server.vo.ApiInvokeVo;
  7. import com.tsi.api.server.vo.DeviceInfo;
  8. import com.tsi.api.server.vo.VersionVo;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.core.io.InputStreamResource;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.http.ContentDisposition;
  13. import org.springframework.http.HttpHeaders;
  14. import org.springframework.http.HttpStatus;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Files;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import java.util.List;
  24. import java.util.UUID;
  25. @Slf4j
  26. @RestController
  27. public class TscSsipAppController {
  28. private final String NAVI_NODE_STATUS = "/app/nodes/{deviceId}";
  29. private final String NAVI_DEVICE_AUTH = "/app/auth/{deviceId}";
  30. private final String NAVI_DEVICE_REGISTER = "/app/devices";
  31. private final String NAVI_DOWNLOADS = "/app/download/{fileName}";
  32. private final String NAVI_API_UUID = "/app/uuid";
  33. private final String NAVI_SSL = "/.well-known/pki-validation/{fileName}";
  34. private final TscSsipAppService tscNaviApiService;
  35. public TscSsipAppController(TscSsipAppService tscNaviApiService) {
  36. this.tscNaviApiService = tscNaviApiService;
  37. }
  38. @GetMapping(value = {NAVI_NODE_STATUS}, produces = {"application/json; charset=utf8"})
  39. public ResponseEntity<AbstractTscSsipApiResult> getNodeStatus(@PathVariable("deviceId") String deviceId, HttpServletRequest request) {
  40. String apiId = NAVI_NODE_STATUS;
  41. String remoteIP = ApiUtils.getRemoteIP(request);
  42. log.info("{}, {}, {}", apiId, remoteIP, deviceId);
  43. ApiInvokeVo apiInvokeVo = ApiInvokeVo.builder()
  44. .apiId(apiId)
  45. .apiToken(deviceId)
  46. .ipAddr(remoteIP)
  47. .error(TscSsipApiErrorCode.SUCCESS.getCode())
  48. .build();
  49. try {
  50. TscSsipApiErrorCode authorizedInfo = this.tscNaviApiService.getAuthorizedInfo(apiId, deviceId, remoteIP);
  51. if (authorizedInfo != TscSsipApiErrorCode.SUCCESS) {
  52. // api token 인증 오류
  53. log.error("인증오류: [{}] ==> [{}]: [{}].[{}]" + apiId, remoteIP, deviceId, authorizedInfo.getCode(), authorizedInfo.getMessage());
  54. apiInvokeVo.setError(authorizedInfo.getCode());
  55. this.tscNaviApiService.insertInvokeHs(apiInvokeVo, false);
  56. TscSsipApiResultError error = new TscSsipApiResultError(authorizedInfo.getCode(), authorizedInfo.getMessage());
  57. return new ResponseEntity<> (error, HttpStatus.UNAUTHORIZED);
  58. }
  59. }
  60. catch (Exception e) {
  61. log.error("getNodeStatus: {}", e.getMessage());
  62. // 데이터베이스 오류
  63. TscSsipApiResultError error = new TscSsipApiResultError(TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getCode(), TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getMessage());
  64. return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
  65. }
  66. TscNaviApiResultNodeStatusInfo result = new TscNaviApiResultNodeStatusInfo(TscSsipApiErrorCode.SUCCESS.getCode(), TscSsipApiErrorCode.SUCCESS.getMessage());
  67. result.setStatusList(tscNaviApiService.getNodeStatusList());
  68. result.setCount(result.getStatusList().size());
  69. // 이력저장
  70. this.tscNaviApiService.insertInvokeHs(apiInvokeVo, false);
  71. log.info("NodeStatus: {}, {}", deviceId, result.toString());
  72. return new ResponseEntity<>(result, HttpStatus.OK);
  73. }
  74. @GetMapping(value = {NAVI_DEVICE_AUTH}, produces = {"application/json; charset=utf8"})
  75. public ResponseEntity<AbstractTscSsipApiResult> getDeviceAuthInfo(@PathVariable("deviceId") String deviceId, HttpServletRequest request) {
  76. String apiId = NAVI_DEVICE_AUTH;
  77. String remoteIP = ApiUtils.getRemoteIP(request);
  78. log.info("{}, {}, {}", apiId, remoteIP, deviceId);
  79. ApiInvokeVo apiInvokeVo = ApiInvokeVo.builder()
  80. .apiId(apiId)
  81. .apiToken(deviceId)
  82. .ipAddr(remoteIP)
  83. .error(TscSsipApiErrorCode.SUCCESS.getCode())
  84. .build();
  85. TscSsipApiErrorCode authorizedInfo = null;
  86. TscNaviApiResultAuthInfo result = null;
  87. try {
  88. authorizedInfo = this.tscNaviApiService.getAuthorizedInfo(apiId, deviceId, remoteIP);
  89. /*if (authorizedInfo == TscSsipApiErrorCode.UNREGISTERED_APITOKEN) {
  90. // api token 인증 오류
  91. log.error("인증오류: [{}] ==> [{}]: [{}].[{}]" + apiId, remoteIP, deviceId, authorizedInfo.getCode(), authorizedInfo.getMessage());
  92. apiInvokeVo.setError(authorizedInfo.getCode());
  93. this.tscNaviApiService.insertInvokeHs(apiInvokeVo);
  94. TscSsipApiResultError error = new TscSsipApiResultError(authorizedInfo.getCode(), authorizedInfo.getMessage());
  95. return new ResponseEntity<> (error, HttpStatus.UNAUTHORIZED);
  96. }*/
  97. result = new TscNaviApiResultAuthInfo(authorizedInfo.getCode(), authorizedInfo.getMessage());
  98. if (authorizedInfo == TscSsipApiErrorCode.SUCCESS) {
  99. List<VersionVo> versionList = this.tscNaviApiService.getVersion();
  100. log.info("version list: {}", versionList.toString());
  101. result.setVersions(versionList);
  102. }
  103. }
  104. catch (Exception e) {
  105. log.error("getDeviceAuthInfo: {}", e.getMessage());
  106. // 데이터베이스 오류
  107. TscSsipApiResultError error = new TscSsipApiResultError(TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getCode(), TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getMessage());
  108. return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
  109. }
  110. // 이력저장
  111. this.tscNaviApiService.insertInvokeHs(apiInvokeVo, true);
  112. log.info("Auth: {}, {}", deviceId, result.toString());
  113. return new ResponseEntity<>(result, HttpStatus.OK);
  114. }
  115. @PostMapping(value = {NAVI_DEVICE_REGISTER}, produces = {"application/json; charset=utf8"})
  116. public ResponseEntity<AbstractTscSsipApiResult> registerDevice(@RequestBody DeviceInfo deviceInfo, HttpServletRequest request){
  117. String apiId = NAVI_DEVICE_REGISTER;
  118. String remoteIP = ApiUtils.getRemoteIP(request);
  119. log.info("{}, {}, {}", apiId, remoteIP, deviceInfo.toString());
  120. try {
  121. this.tscNaviApiService.registerDevice(deviceInfo);
  122. }
  123. catch(Exception e) {
  124. log.error("registerDevice: {}", e.getMessage().toString());
  125. TscSsipApiResultError error = new TscSsipApiResultError(TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getCode(), TscSsipApiErrorCode.ERROR_INTERNAL_DATA.getMessage());
  126. return new ResponseEntity<> (error, HttpStatus.INTERNAL_SERVER_ERROR);
  127. }
  128. TscNaviApiResultDeviceRegisterInfo result = new TscNaviApiResultDeviceRegisterInfo(TscSsipApiErrorCode.SUCCESS.getCode(), TscSsipApiErrorCode.SUCCESS.getMessage());
  129. result.setResult("ok");
  130. return new ResponseEntity<>(result, HttpStatus.OK);
  131. }
  132. @GetMapping(value = {NAVI_DOWNLOADS})
  133. public ResponseEntity<Resource> download(@PathVariable("fileName") String fileName, HttpServletRequest request) {
  134. String apiId = NAVI_DOWNLOADS;
  135. String remoteIP = ApiUtils.getRemoteIP(request);
  136. log.info("{}, {}, {}", apiId, remoteIP, fileName);
  137. try {
  138. String separator = System.getProperty("file.separator");
  139. String fileFullName = System.getProperty("user.dir")+separator+"downloads"+separator+fileName;
  140. Path path = Paths.get(fileFullName);
  141. File file2Upload = path.toFile();
  142. String contentType = "application/download";
  143. //String contentType = Files.probeContentType(path);
  144. Resource resource = new InputStreamResource(Files.newInputStream(path));
  145. HttpHeaders headers = new HttpHeaders();
  146. headers.add(HttpHeaders.CONTENT_TYPE, contentType);
  147. headers.setContentLength(file2Upload.length());
  148. headers.setContentDisposition(ContentDisposition.parse("attachment;" + " filename=\"" + fileName + "\";"));
  149. return new ResponseEntity<>(resource, headers, HttpStatus.OK);
  150. }
  151. catch (IOException e) {
  152. log.error("download: {}", e.toString());
  153. return new ResponseEntity<> (HttpStatus.INTERNAL_SERVER_ERROR);
  154. }
  155. }
  156. @GetMapping(value = {NAVI_SSL})
  157. public ResponseEntity<Resource> downloadssl(@PathVariable("fileName") String fileName, HttpServletRequest request) {
  158. String apiId = NAVI_SSL;
  159. String remoteIP = ApiUtils.getRemoteIP(request);
  160. log.info("{}, {}, {}", apiId, remoteIP, fileName);
  161. try {
  162. String separator = System.getProperty("file.separator");
  163. String fileFullName = System.getProperty("user.dir")+separator+".well-known"+separator+"pki-validation"+separator+fileName;
  164. Path path = Paths.get(fileFullName);
  165. File file2Upload = path.toFile();
  166. String contentType = "application/download";
  167. //String contentType = Files.probeContentType(path);
  168. Resource resource = new InputStreamResource(Files.newInputStream(path));
  169. HttpHeaders headers = new HttpHeaders();
  170. headers.add(HttpHeaders.CONTENT_TYPE, contentType);
  171. headers.setContentLength(file2Upload.length());
  172. headers.setContentDisposition(ContentDisposition.parse("attachment;" + " filename=\"" + fileName + "\";"));
  173. return new ResponseEntity<>(resource, headers, HttpStatus.OK);
  174. }
  175. catch (IOException e) {
  176. log.error("download: {}", e.toString());
  177. return new ResponseEntity<> (HttpStatus.INTERNAL_SERVER_ERROR);
  178. }
  179. }
  180. @GetMapping(value = {NAVI_API_UUID})
  181. public String getUUID(){
  182. String uid = UUID.randomUUID().toString();
  183. uid = uid.replace("-","").toUpperCase();
  184. return uid;
  185. }
  186. }