保誠-保戶業務員媒合平台
wayne
2021-12-17 374045ebbeaa725ec2d030f5474067e37f568637
[add][todo 132507] 新增發送email service

修改5個檔案
新增4個檔案
291 ■■■■■ 已變更過的檔案
pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java 30 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java 81 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java 103 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java 25 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java 4 ●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java 28 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java 9 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java 8 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/resources/config/application-dev.yml 3 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
@@ -19,6 +19,7 @@
    private String eServiceLoginFunc;
    private String eServiceLoginSys;
    private SMS sms;
    private Email email;
    public boolean isMockLogin() {
        return mockLogin;
@@ -84,6 +85,14 @@
        this.sms = sms;
    }
    public Email getEmail() {
        return email;
    }
    public void setEmail(Email email) {
        this.email = email;
    }
    public static class SMS {
        private String url;
        private String sourceCode;
@@ -122,4 +131,25 @@
            this.smsType = smsType;
        }
    }
    public static class Email {
        private String url;
        private String functionId;
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public String getFunctionId() {
            return functionId;
        }
        public void setFunctionId(String functionId) {
            this.functionId = functionId;
        }
    }
}
pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -1,46 +1,44 @@
package com.pollex.pam.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.service.dto.SMSDetail;
import com.pollex.pam.service.dto.SendSMSRequest;
import com.pollex.pam.service.dto.SendSMSResponse;
import com.pollex.pam.config.ApplicationProperties.Email;
import com.pollex.pam.config.ApplicationProperties.SMS;
import com.pollex.pam.service.dto.*;
import com.pollex.pam.service.util.HttpRequestUtil;
import com.pollex.pam.web.rest.errors.SendEmailFailException;
import com.pollex.pam.web.rest.errors.SendSMSFailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@Service
public class SendMsgService {
    private final static Logger log = LoggerFactory.getLogger(SendMsgService.class);
    private static final Logger log = LoggerFactory.getLogger(SendMsgService.class);
    private final Encoder encoder = Base64.getEncoder();
    @Autowired
    ApplicationProperties applicationProperties;
    public SendSMSResponse sendMsgBySMS(String subject, String toMobile, String content) throws Exception{
        final String SMS_URL = applicationProperties.getSms().getUrl();
    public void sendMsgBySMS(String subject, String toMobile, String content) throws SendSMSFailException{
        SMS smsProperties = applicationProperties.getSms();
        SendSMSRequest sendSMSRequest = new SendSMSRequest();
        sendSMSRequest.setpKey(UUID.randomUUID().toString());
        sendSMSRequest.setSourceCode(applicationProperties.getSms().getSourceCode());
        sendSMSRequest.setSender(applicationProperties.getSms().getSender());
        sendSMSRequest.setMsgTypeSet(applicationProperties.getSms().getSmsType());
        sendSMSRequest.setSourceCode(smsProperties.getSourceCode());
        sendSMSRequest.setSender(smsProperties.getSender());
        sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
        sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
        sendSMSRequest.setSubject(subject);
        sendSMSRequest.setActivityId("");
@@ -51,9 +49,60 @@
        sendSMSRequest.setDetail(Collections.singletonList(smsDetail));
        ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(SMS_URL, sendSMSRequest, SendSMSResponse.class);
        return responseEntity.getBody();
        try {
            ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(smsProperties.getUrl(), sendSMSRequest, SendSMSResponse.class);
            log.debug("response status code = {}", responseEntity.getStatusCode());
            log.debug("smsResponse = {}", responseEntity.getBody());
            // todo å¯èƒ½éœ€è¦å†è£œä¸Šå¾ŒçºŒéŒ¯èª¤è™•理,但要先測通
        }
        catch (Exception e) {
            log.debug("send sms failed!", e);
            throw new SendSMSFailException();
        }
    }
    public void sendMsgByEmail(String from, String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
        sendMsgByEmail(from, to, subject, content, htmlFormat, Collections.emptyList(), Collections.emptyList());
    }
    public void sendMsgByEmail(
        String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
        List<String> attachments) throws SendEmailFailException
    {
        SendMailRequest sendMailRequest = new SendMailRequest();
        sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress));
        sendMailRequest.setFrom(fromAddress);
        sendMailRequest.setContent(content);
        sendMailRequest.setSubject(subject);
        sendMailRequest.setSendCCMailAddresses(toCCAddress);
        sendMailRequest.setAttachments(attachments);
        sendMailRequest.setHtmlFormat(htmlFormat);
        sendMailRequest.setFunctionId(applicationProperties.getEmail().getFunctionId());
        sendMsgByEmail(sendMailRequest);
    }
    public void sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
        try {
            ResponseEntity<SendMailResponse> responseEntity =
                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, SendMailResponse.class);
            SendMailResponse sendMailResponse = responseEntity.getBody();
            log.debug("response status code = {}", responseEntity.getStatusCode());
            log.debug("emailResponse = {}", responseEntity.getBody());
            if(sendMailResponse == null || sendMailResponse.getData() == null || "ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
                throw new SendEmailFailException();
            }
        }
        catch (SendEmailFailException e) {
            throw e;
        }
        catch (Exception e) {
            log.debug("send email failed!", e);
            throw new SendEmailFailException();
        }
    }
}
pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,103 @@
package com.pollex.pam.service.dto;
import java.util.List;
public class SendMailRequest {
    /**
     * æ”¶ä»¶è€…
     */
    private List<String> sendMailAddresses;
    /**
     * å‰¯æœ¬
     */
    private List<String> sendCCMailAddresses;
    /**
     * ä¸»é¡Œ
     */
    private String subject;
    /**
     * å…§æ–‡
     */
    private String content;
    /**
     * å¯„件者
     */
    private String from;
    /**
     * é™„件路徑
     */
    private List<String> attachments;
    /**
     * æ˜¯å¦ html format
     */
    private boolean htmlFormat;
    /**
     * è‡ªå®šç¾©è¨Šæ¯åˆ†é¡ž (因應保誠的要求,先暫時寫死城epos)
     */
    private String functionId;
    public List<String> getSendMailAddresses() {
        return sendMailAddresses;
    }
    public void setSendMailAddresses(List<String> sendMailAddresses) {
        this.sendMailAddresses = sendMailAddresses;
    }
    public List<String> getSendCCMailAddresses() {
        return sendCCMailAddresses;
    }
    public void setSendCCMailAddresses(List<String> sendCCMailAddresses) {
        this.sendCCMailAddresses = sendCCMailAddresses;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public List<String> getAttachments() {
        return attachments;
    }
    public void setAttachments(List<String> attachments) {
        this.attachments = attachments;
    }
    public boolean isHtmlFormat() {
        return htmlFormat;
    }
    public void setHtmlFormat(boolean htmlFormat) {
        this.htmlFormat = htmlFormat;
    }
    public String getFunctionId() {
        return functionId;
    }
    public void setFunctionId(String functionId) {
        this.functionId = functionId;
    }
}
pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,25 @@
package com.pollex.pam.service.dto;
public class SendMailResponse {
    private Data data;
    public Data getData() {
        return data;
    }
    public void setData(Data data) {
        this.data = data;
    }
    public static class Data {
        private String messageStatus;
        public String getMessageStatus() {
            return messageStatus;
        }
        public void setMessageStatus(String messageStatus) {
            this.messageStatus = messageStatus;
        }
    }
}
pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java
@@ -68,7 +68,7 @@
        throws JsonProcessingException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        String parameters = new ObjectMapper().writeValueAsString(jsonData);
        //如果有帶Base64,debug印出來效能會很慘
        // é¿å…é•·åº¦éŽé•·é€²è€Œå°Žè‡´log爆掉
        if(parameters.length() < 1000){
            log.debug("parameters : {}",parameters);
        }
@@ -80,6 +80,8 @@
        setHeaders(headers, addedHeaders);
        HttpEntity<String> entity = new HttpEntity<>(parameters, headers);
        log.debug("rest post with json, url = {}", url);
        return restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }
pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java
@@ -1,23 +1,33 @@
package com.pollex.pam.web.rest;
import com.pollex.pam.service.SendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.bind.annotation.*;
@Deprecated
@RestController
@RequestMapping("/api/test/sendMsg")
public class TestSendMsgResource {
    @PostMapping("/bySMS")
    public ResponseEntity<Void> bySMS(@RequestBody Object param) {
    @Autowired
    SendMsgService sendMsgService;
    @GetMapping("/bySMS")
    public ResponseEntity<Void> bySMS(@RequestParam String subject, @RequestParam String toMobile, @RequestParam String content) {
        sendMsgService.sendMsgBySMS(subject, toMobile, content);
        return ResponseEntity.noContent().build();
    }
    @PostMapping("/byEmail")
    public ResponseEntity<Void> byEmail(@RequestBody Object param) {
    @GetMapping("/byEmail")
    public ResponseEntity<Void> byEmail(
        @RequestParam String from,
        @RequestParam String to,
        @RequestParam String subject,
        @RequestParam String content,
        @RequestParam boolean htmlFormat
    ) {
        sendMsgService.sendMsgByEmail(from, to, subject, content, htmlFormat);
        return ResponseEntity.noContent().build();
    }
}
pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,9 @@
package com.pollex.pam.web.rest.errors;
public class SendEmailFailException extends RuntimeException {
    public SendEmailFailException() {
    }
    public SendEmailFailException(String message) {
        super(message);
    }
}
pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,8 @@
package com.pollex.pam.web.rest.errors;
public class SendSMSFailException extends RuntimeException{
    public SendSMSFailException(){}
    public SendSMSFailException(String message) {
        super(message);
    }
}
pamapi/src/main/resources/config/application-dev.yml
@@ -124,3 +124,6 @@
    source-code: ePos
    sender: POS
    sms-type: 0017
  email:
    url: https://vtwlifeopensysuat.pru.intranet.asia/tsgw/mq/mqSendMail
    function-id: epos