WebAppCommonController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.its.traf.webapp.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.its.app.utils.Ping;
  4. import com.its.traf.config.ProcessingConfig;
  5. import com.its.traf.webapp.service.FileService;
  6. import com.its.traf.webapp.vo.voFileInfo;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.core.io.Resource;
  9. import org.springframework.core.io.ResourceLoader;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.ui.Model;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.servlet.ModelAndView;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. import java.io.IOException;
  19. import java.io.UnsupportedEncodingException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. @Slf4j
  26. @Controller
  27. public class WebAppCommonController {
  28. private final ResourceLoader resourceLoader;
  29. private final ProcessingConfig serverConfig;
  30. private final FileService fileService;
  31. public WebAppCommonController(ResourceLoader resourceLoader, ProcessingConfig serverConfig, FileService fileService) {
  32. this.resourceLoader = resourceLoader;
  33. this.serverConfig = serverConfig;
  34. this.fileService = fileService;
  35. }
  36. @RequestMapping({"/", "/index"})
  37. public String index(Model model, HttpServletRequest request) {
  38. String result = "system";
  39. model.addAttribute("ServerConfig", this.serverConfig);
  40. model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  41. return result;
  42. }
  43. @RequestMapping("/log")
  44. public String logFiles(Model model, HttpServletRequest request) {
  45. String result = "log";
  46. List<voFileInfo> info = this.fileService.getLogFiles();
  47. //JSON 형식으로 바꿔주기
  48. ObjectMapper mapper = new ObjectMapper();
  49. String jsonList="";
  50. try {
  51. jsonList = mapper.writeValueAsString(info);
  52. }
  53. catch (IOException e) {
  54. log.error("WebAppController.logFiles: Exception: {}", e.toString());
  55. }
  56. model.addAttribute("ServerConfig", this.serverConfig);
  57. model.addAttribute("fileList",jsonList);
  58. model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  59. return result;
  60. }
  61. @RequestMapping("/login")
  62. public String login(Model model) {
  63. String result = "login";
  64. model.addAttribute("ServerConfig", this.serverConfig);
  65. model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  66. return result;
  67. }
  68. @GetMapping("/logout")
  69. public void logout() {
  70. }
  71. @RequestMapping("/getServerDate")
  72. @ResponseBody
  73. public Map<String,Object> getServerDate() {
  74. Map<String,Object> map = new HashMap<>();
  75. map.put("serverDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  76. return map;
  77. }
  78. @RequestMapping(value = "/getFileView",method = {RequestMethod.POST},produces = {"application/json"})
  79. @ResponseBody
  80. public String getFileView(@RequestParam("fileName") String fileName, @RequestParam("filePath") String filePath, HttpServletRequest request) {
  81. return this.fileService.getView(request, fileName, filePath);
  82. }
  83. @RequestMapping("/getFileDownload")
  84. public ResponseEntity<Resource> getFileDownload(@RequestParam("fileName") String fileName , @RequestParam("filePath") String filePath
  85. , HttpSession session
  86. , HttpServletRequest req
  87. , HttpServletResponse res
  88. , ModelAndView mav) throws UnsupportedEncodingException {
  89. return this.fileService.fileDownload(fileName,filePath);
  90. }
  91. @RequestMapping(value = ("/getFileDelete"))
  92. @ResponseBody
  93. public List<voFileInfo> getFileDelete(@RequestParam("fileName") String fileName, @RequestParam("filePath") String filePath) {
  94. //파일 지우기
  95. this.fileService.fileDelete(fileName,filePath);
  96. //파일 리스트 가져오기
  97. List<voFileInfo> fileArr = fileService.getLogFiles();
  98. return fileArr;
  99. }
  100. @RequestMapping(value = ("/getPing"))
  101. @ResponseBody
  102. public String getPing(@RequestParam("ipAddr") String ipAddr) {
  103. //log.info("getPing: {}", ipAddr);
  104. String result = ping(ipAddr);
  105. //log.info("getPing: {} ==> {}", ipAddr, result);
  106. return result;
  107. }
  108. public String ping(String ipAddr) {
  109. String result = "";
  110. long startTm = System.nanoTime();
  111. long endTm = System.nanoTime();
  112. try {
  113. boolean isReachable = Ping.isReachable(ipAddr, 5);
  114. endTm = System.nanoTime();
  115. if (isReachable ) {
  116. result = ipAddr + " is alive. " + Long.toString((endTm - startTm) / 1000000) + " ms.";
  117. }
  118. else {
  119. result = ipAddr + " is not alive. time out: " + Long.toString((endTm - startTm) / 1000000) + " ms.";
  120. }
  121. }
  122. catch (Exception e) {
  123. result = ipAddr + " error. " + Long.toString((endTm - startTm) / 1000000) + " ms. Exception: " + e.toString();
  124. }
  125. return result;
  126. }
  127. }