package com.pgf.mqspring; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import javax.jms.ConnectionFactory; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.view.RedirectView; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.pgf.mqspring.model.QlPoc01Test2Request; import com.pgf.mqspring.model.ReplyRequestModel; import com.pgf.mqspring.model.SampleRequestModel; import com.pgf.mqspring.model.SampleResponseModel; import com.pgf.mqspring.model.TestModel; import com.pgf.mqspring.service.CommonService; import com.pgf.mqspring.service.JmsSendService; import com.pgf.mqspring.service.MqQlPoc01Test2ReceiveService; import com.pgf.mqspring.service.MqQlPoc01Test2SendService; import com.pgf.mqspring.service.MqReceiveService; import com.pgf.mqspring.service.MqSendService; import com.pgf.mqspring.service.SampleMqReceiveService; import com.pgf.mqspring.service.SampleMqSendService; import com.pgf.mqspring.service.SampleService; import com.pgf.mqspring.service.impl.MqQlPoc01Test2ReceiveServiceImpl; import com.pgf.mqspring.service.impl.MqQlPoc01Test2SendServiceImpl; import com.pgf.mqspring.service.impl.MqReceiveServiceImpl; import com.pgf.mqspring.service.impl.MqSendServiceImpl; import com.pgf.mqspring.service.impl.SampleMqReceiveServiceImpl; import com.pgf.mqspring.service.impl.SampleMqSendServiceImpl; @SpringBootApplication @RestController @EnableJms @EnableCaching public class MqSpringApplication { static private ConfigurableApplicationContext context = null; static private JmsTemplate jmsTemplate = null; static private Logger logger = null; static private RequestAuthFilter requestAuthFilter = new RequestAuthFilter(); @Value("${aws.bucketname}") String s3BucketName; public static void main(String[] args) { logger = LogManager.getLogger(); context = SpringApplication.run(MqSpringApplication.class, args); jmsTemplate = context.getBean(JmsTemplate.class); //TODO:↓後でコメント解除する // 受信待ち時間のタイムアウトを60秒に設定 //jmsTemplate.setReceiveTimeout(60000); // メッセージの生存時間を60秒に設定 //jmsTemplate.setExplicitQosEnabled(true); //jmsTemplate.setTimeToLive(60000); //TODO:↓Apigeeの検証するときコメント解除する //requestAuthFilter.init(null); TelegramInfo.initTelegramInfo(); } @Bean JmsListenerContainerFactory listenerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConcurrency("1-1"); // This provides all boot's default to this factory, including the message converter configurer.configure(factory, connectionFactory); // You could still override some of Boot's default if necessary. return factory; } @RequestMapping("send-test") String send_test() { MqSendService service = new MqSendServiceImpl(jmsTemplate); logger.info("Set Que Name at 'QL.POC01.IN' [send]"); service.setQueue("QL.POC01.IN"); return service.send("This is IBM MQ message!"); } @RequestMapping("recv-test") String recv_test() { MqReceiveService service = new MqReceiveServiceImpl(jmsTemplate); logger.info("Set Que Name at 'QL.POC01.IN' [recv]"); service.setQueue("QL.POC01.IN"); return service.receive(); } @RequestMapping("send") String send(@RequestBody TestModel testModel) throws IOException { MqSendService service = new MqSendServiceImpl(jmsTemplate); service.setQueue("QL.POC01.IN"); return service.send(testModel); } @RequestMapping("send-cics") String send_cics(@RequestBody TestModel testModel) throws IOException { MqSendService service = new MqSendServiceImpl(jmsTemplate); service.setQueue("QL.TEST.IN"); return service.sendCics(testModel); } @RequestMapping("send-recv") RedirectView send_recv(@RequestBody TestModel testModel) throws IOException { MqSendService service = new MqSendServiceImpl(jmsTemplate); service.setQueue("QL.TEST.IN"); if (!"OK".equals(service.send(testModel))) { return new RedirectView("send-ng"); } return new RedirectView("recv?id=" + testModel.getId()); } @RequestMapping("send-recv-cics") RedirectView send_recv_cics(@RequestBody TestModel testModel) throws IOException { MqSendService service = new MqSendServiceImpl(jmsTemplate); //service.setQueue("QL.POC05.TEST"); service.setQueue("QL.POC01.IN"); if (!"OK".equals(service.sendCics(testModel))) { return new RedirectView("send-ng"); } return new RedirectView("recv2?id=" + testModel.getId()); } @RequestMapping("/sic/mq-rest/v1/apl/cda/sendRawData") RedirectView sendRawData(@RequestBody TestModel testModel) throws IOException, InterruptedException { MqSendService service = new MqSendServiceImpl(jmsTemplate); service.setQueue("QL.TEST.IN"); //service.setQueue("QL.POC05.TEST"); if (!"200".equals(service.sendRawData(testModel))) { return new RedirectView("/send-ng"); } Thread.sleep(200); String str = service.getMessageId(); logger.info("MessageID=" + str); return new RedirectView("/recvMessage?id=" + str); } @RequestMapping(value = "/sic/mq-rest/v1/apl/cda/reply", consumes = "application/json", produces = "application/json") public String replyRequest(@RequestBody(required = false) ReplyRequestModel request) throws InterruptedException { MqSendService service = new MqSendServiceImpl(jmsTemplate); service.setQueue("QL.TEST.IN"); //service.setQueue("QL.POC05.TEST"); // if (!"OK".equals(service.sendMqMessage(request))) { // System.out.println("send-ng"); // return new RedirectView("/send-ng"); // } // return new RedirectView("/recvMessage?id=" + request.getMqmd().getMsgId()); String ret = service.sendMqMessage(request); Thread.sleep(200); logger.info("MessageID=" + service.getMessageId()); return ret; } @RequestMapping("recv") String recv(@RequestParam(name = "id", defaultValue = "10001") String id) { MqReceiveService service = new MqReceiveServiceImpl(jmsTemplate); service.setQueue("QL.POC01.IN"); //service.setQueue("QL.POC05.TEST"); return service.receive(id); } @RequestMapping("recv2") String recv2(@RequestParam(name = "id", defaultValue = "10001") String id) { MqReceiveService service = new MqReceiveServiceImpl(jmsTemplate); service.setQueue("QL.POC01.REPLY"); //service.setQueue("QL.POC05.REPLY"); return service.receiveCics(id); } @RequestMapping("/recvMessage") String recvMessage(@RequestParam(name = "id") String id) { MqReceiveService service = new MqReceiveServiceImpl(jmsTemplate); service.setQueue("QL.TEST1.REPLY"); if (id.length() > 0) { String ret = service.receiveMessage(id); return ret; } return "500"; } @RequestMapping("send-ng") String sendNg() { return "SEND FAIL"; } @Autowired SampleService sampleService; @PostMapping(value = "/createSample", consumes = "application/json", produces = "application/json") public SampleResponseModel createSample(@RequestBody(required = false) SampleRequestModel request) { // JMS MQ送受信 String retMsg = jmsSendSample(request); return sampleService.createSample(request, retMsg); } public String jmsSendSample(SampleRequestModel request) { SampleMqSendService mqSendService = new SampleMqSendServiceImpl(jmsTemplate); mqSendService.setQUE("QL.TEST.IN"); mqSendService.setRequest(request); return mqSendService.send(); // return jmsReplySample(); // return "redirect:/jmsReplySample"; } @Autowired JmsSendService jmsSendService; @RequestMapping("/jmsReplySample") public String jmsReplySample() { System.out.println("jmsReplySample:start"); SampleMqReceiveService mqRecvService = new SampleMqReceiveServiceImpl(jmsTemplate); mqRecvService.setQUE("QL.TEST.IN"); System.out.println("jmsReplySample:end"); return mqRecvService.receive(); } /** * リクエスト「jmsSend」のメソッド * * @param request リクエストパラメータ * @return リダイレクト先/NG * @throws IOException */ @PostMapping(value = "jmsSend", consumes = "application/json", produces = "application/json") public RedirectView jmsSend(@RequestBody(required = false) QlPoc01Test2Request request) { String[] ret = jmsQlPoc01Test2Send(request); jmsSendService.jmsSend(request, ret[0]); logger.info("ret[0]:" + ret[0]); logger.info("ret[1]:" + ret[1]); if ("OK".equals(ret[0])) { return new RedirectView("jmsRecv?JMSMessageID=" + ret[1]); } else { return null; } } /** * リクエスト「auth」のメソッド(Apigee検証用メソッド) * * @param request リクエストパラメータ * @throws ServletException * @throws IOException */ @PostMapping(value = "/pgf/mq-rest/v1/apl/auth", consumes = "application/json", produces = "application/json") public String auth(@RequestBody(required = false) QlPoc01Test2Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException, NoSuchAlgorithmException, InvalidKeySpecException { //String token = requestAuthFilter.generate(); //httpRequest.setAttribute("X-PruAuthJWT", token); //requestAuthFilter.doFilter(httpRequest, httpResponse, null); if (httpResponse.getStatus() != HttpServletResponse.SC_OK) { return "NG AuthError"; } return "OK"; } /** * リクエスト「mqmd」のメソッド * * @param request リクエストパラメータ * @throws JsonProcessingException */ @PostMapping(value = "/api/cda/reply", consumes = "application/json", produces = "application/json") public String mqmd(@RequestBody(required = false) ReplyRequestModel request) throws JsonProcessingException { ////toStringにして出力 // return "\r\nall=" + request.toString() + "\r\nMQMD=" + request.getMqmd().toString() + "\r\nMQCIH=" // + request.getMqcih().toString() + "\r\nSendMessage=" + request.getSendMessage().toString() + "\r\n"; //jsonにして出力 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(request); return json; } /** * キュー「QL.TEST.IN」に送信するメソッド * * @param request リクエストパラメータ * @return 送信結果 */ public String[] jmsQlPoc01Test2Send(QlPoc01Test2Request request) { MqQlPoc01Test2SendService mqSendService = new MqQlPoc01Test2SendServiceImpl(jmsTemplate); // mqSendService.setQue("QR.POC01.IN"); mqSendService.setQue("QL.TEST.IN"); mqSendService.setRequest(request); return mqSendService.send(request); } /** * リクエスト「jmsRecv」のメソッド * * @return 受信内容/FAIL */ @RequestMapping("jmsRecv") public String jmsRecv(@RequestParam(name = "JMSMessageID") String id) { logger.info("jmsRecv:start"); MqQlPoc01Test2ReceiveService mqRecvService = new MqQlPoc01Test2ReceiveServiceImpl(jmsTemplate); // mqRecvService.setQue("QL.POC01.REPLY"); mqRecvService.setQue("QL.TEST.IN"); logger.info("jmsRecv:end"); return mqRecvService.receive(id); } @Autowired CommonService commonService; @RequestMapping("convert") public String convertEbcdicToSjis() { String unicode = "mq変換コード12あ#"; byte[] bytes = commonService.encodeEbcdic(unicode); String result = commonService.decodeEbcdic(bytes); System.out.println(unicode); System.out.println("EBCDIC encode結果:" + Arrays.toString(bytes)); System.out.println("EBCDIC decode結果:" + result); return result; } @RequestMapping("convert_length_sisonone") public String convertEbcdicToSjis1() { String unicode = "Ads1234567"; byte[] bytes = commonService.encodeStringData(unicode, 7, true); String result = commonService.decodeStringData(bytes, true); System.out.println(unicode); System.out.println("adsdata1 encode結果:" + Arrays.toString(bytes)); System.out.println("adsdata1 decode結果:" + result); return result; } @RequestMapping("convert_length") public String convertEbcdicToSjis2() { String unicode = "Ads123"; byte[] bytes = commonService.encodeStringData(unicode, 10, false); String result = commonService.decodeStringData(bytes, false); System.out.println(unicode); System.out.println("adsdata2 encode結果:" + Arrays.toString(bytes)); System.out.println("adsdata2 decode結果:" + result); return result; } @RequestMapping("s3download") public String s3Download() { return commonService.downloadIbmTelegramInfo(); } @RequestMapping("readcsvfile") public void readCsvFile() { String dispCode = "EK10"; List> list = new ArrayList>(); list = commonService.getTelInfo(dispCode); for (int i = 0; i < list.size(); i++) { HashMap resultMap = list.get(i); // 読み込んだCSVファイルの内容を出力 System.out.print(resultMap.get("DISP_CODE") + ","); System.out.print(resultMap.get("FIELD_NO") + ","); System.out.print(resultMap.get("ITEM_NAME") + ","); System.out.print(resultMap.get("ITEM_LENGTH") + ","); System.out.print(resultMap.get("CHAR_DIV") + ","); System.out.print(resultMap.get("REPEAT_NUM") + ","); System.out.print(resultMap.get("GROUP_NUM") + ","); System.out.println(resultMap.get("INS_DATE")); } } @RequestMapping("adsdata") public String convertAdsData() { String dispCode = "GE01"; String data = "12345678,20220926,125610,user0001,password,newpass,newpass1,Msg001"; byte[] bytes = commonService.encodeAdsData(dispCode, data); String adsData = commonService.decodeAdsData(dispCode, bytes); return adsData; } @PostMapping(value = "sendMqMessage", consumes = "application/json", produces = "application/json") public RedirectView sendMqMessage(@RequestBody(required = false) ReplyRequestModel request) { MqSendService service = new MqSendServiceImpl(jmsTemplate); jmsTemplate.setReceiveTimeout(Long.parseLong(request.getTimeout()) ); jmsTemplate.setExplicitQosEnabled(true); jmsTemplate.setTimeToLive(Long.parseLong(request.getTimeout())); service.setQueue("QL.TEST1.IN"); String ret = service.sendMqMessage(request); if (!"200".equals(ret)) { return new RedirectView("/send-ng"); } // Thread.sleep(1000); String id = service.getMessageId(); // logger.info("[After 1000m Sleep] MessageID=" + id); return new RedirectView("/recvMessage?id=" + id); } }