package cfs.trsmrcv.web; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import cfs.WASInfo; import cfs.common.util.ResultSmartPlatform; import cfs.schd.dao.SchdDao; import cfs.trsmrcv.dao.TrsmrcvDao; @Controller public class TestController { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource(name = "trsmrcvDao") private TrsmrcvDao trsmrcvDao; @Resource(name = "schdDao") private SchdDao schdDao; @RequestMapping(value="/cfs/trsmrcv/sleep.do", method=RequestMethod.GET) public @ResponseBody Map sleep(HttpServletRequest request) throws Exception { TrsmrcvUtil.printRequestHeader(request); logger.info("sleep 요청"); Thread.sleep(100000); logger.info("sleep 끝"); ResultSmartPlatform result = new ResultSmartPlatform(); result.setMsg(ResultSmartPlatform.STATUS_SUCESS, "정상적으로 처리되었습니다."); return result.getResult(); } @RequestMapping(value="/cfs/trsmrcv/insertSusinLog.do", method=RequestMethod.GET) public @ResponseBody Map insertSusinLog(HttpServletRequest request) throws Exception { TrsmrcvUtil.printRequestHeader(request); Map susinInfo = TrsmrcvUtil.getInfoMapForConnSusinLog(request); trsmrcvDao.insertConnSusinLog(susinInfo); Map result = new HashMap(); result.put("res", "ok"); return result; } @RequestMapping(value="/cfs/trsmrcv/updateRunWas.do", method=RequestMethod.GET) public @ResponseBody Map updateRunWas(HttpServletRequest request) throws Exception { String schdulName = request.getParameter("schdulName"); Map map = new HashMap(); map.put("runWas", WASInfo.getWAS()); map.put("schdulName", schdulName); schdDao.updateRunWas(map); Map result = new HashMap(); result.put("res", "ok"); return result; } @RequestMapping(value="/rest/test/nothing.do", method=RequestMethod.POST) public @ResponseBody Map nothing(@RequestBody Map reqMap) throws Exception { Map result = new HashMap(); String commandSeq = trsmrcvDao.getCommandSeq(""); Map commandInMap = new HashMap(); commandInMap.put("commandSeq", commandSeq); commandInMap.put("sysName", "monitoring"); commandInMap.put("command", "nothing"); trsmrcvDao.insertCommand(commandInMap); this.touchAndWait("monitoring", commandSeq); result.put("res", "OK"); return result; } @RequestMapping(value="/rest/test/bringJuso.do", method=RequestMethod.POST) public @ResponseBody Map bringJuso(@RequestBody Map reqMap) throws Exception { Map result = new HashMap(); String commandSeq = trsmrcvDao.getCommandSeq(""); Map commandInMap = new HashMap(); commandInMap.put("commandSeq", commandSeq); commandInMap.put("sysName", "monitoring"); commandInMap.put("command", "bringJuso"); trsmrcvDao.insertCommand(commandInMap); this.touchAndWait("monitoring", commandSeq); result.put("res", "OK"); return result; } @RequestMapping(value="/rest/test/sendMail.do", method=RequestMethod.POST) public @ResponseBody Map sendMail(@RequestBody Map reqMap) throws Exception { Map result = new HashMap(); String SEND_MAIL_MANAGER = "monitoring"; String commandSeq = trsmrcvDao.getCommandSeq(""); Map commandInMap = new HashMap(); commandInMap.put("commandSeq", commandSeq); commandInMap.put("sysName", SEND_MAIL_MANAGER); commandInMap.put("command", "sendMail"); trsmrcvDao.insertCommand(commandInMap); List hpup = (List) reqMap.get("hpup"); List mailProps = (List) reqMap.get("mailProps"); String from = reqMap.get("from").toString(); List to = (List) reqMap.get("to"); String subject = reqMap.get("subject").toString(); String text = reqMap.get("text").toString(); Map mailInMap = new HashMap(); mailInMap.put("commandSeq", commandSeq); mailInMap.put("hpup", String.join(",", hpup)); mailInMap.put("mailProps", String.join(",", mailProps)); mailInMap.put("from", from); mailInMap.put("to", String.join(",", to)); mailInMap.put("subject", subject); mailInMap.put("text", text); trsmrcvDao.insertSendMail(mailInMap); this.touchAndWait(SEND_MAIL_MANAGER, commandSeq); result.put("res", "OK"); return result; } private Map touchAndWait(String touchSys, String commandSeq) throws Exception { Map commandResult = new HashMap(); this.shellCmd("/gpta/source-app/cfs/relay_jar/ssh_sh.sh " + touchSys); int FIRST_SLEEP = 2000; Thread.sleep(FIRST_SLEEP); int WAITING_CNT = 20; int WAITING_SLEEP = 1000; boolean commandComplate = false; for(int i=0; i < WAITING_CNT; i++){ Map commandOutMap = trsmrcvDao.selectCommandById(commandSeq); if(commandOutMap.get("STAT_CD").equals("2")){ commandComplate = true; //TODO if need result //commandResult.put } if(commandComplate){ break; } Thread.sleep(WAITING_SLEEP); } return commandResult; } private boolean shellCmd(String cmd) { Process process = null; Runtime runtime = Runtime.getRuntime(); StringBuffer successOutput = new StringBuffer(); StringBuffer errorOutput = new StringBuffer(); BufferedReader successBufferReader = null; BufferedReader errorBufferReader = null; String msg = null; boolean result = false; logger.info("shellCmd - 1"); List cmdList = new ArrayList(); cmdList.add("/bin/sh"); cmdList.add("-c"); cmdList.add(cmd); String[] array = cmdList.toArray(new String[cmdList.size()]); try { process = runtime.exec(array); successBufferReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); while ((msg = successBufferReader.readLine()) != null) { successOutput.append(msg + System.getProperty("line.separator")); } errorBufferReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8")); while ((msg = errorBufferReader.readLine()) != null) { errorOutput.append(msg + System.getProperty("line.separator")); } process.waitFor(); if (process.exitValue() == 0) { result = true; } else { logger.info("abnormal finish error : " + successOutput.toString()); } if (errorOutput.toString().length() > 0) { logger.info("error : " + errorOutput.toString()); } } catch (IOException e) { logger.info(e.getMessage()); } catch (InterruptedException e) { logger.info(e.getMessage()); } finally { try { process.destroy(); if (successBufferReader != null) successBufferReader.close(); if (errorBufferReader != null) errorBufferReader.close(); } catch (IOException e1) { logger.info(e1.getMessage()); } } return result; } }