package no.vipps.service.impl; import java.io.IOException; import java.util.Arrays; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import com.sendgrid.Attachments; import com.sendgrid.Content; import com.sendgrid.Email; import com.sendgrid.Mail; import com.sendgrid.Method; import com.sendgrid.Personalization; import com.sendgrid.Request; import com.sendgrid.Response; import com.sendgrid.SendGrid; import no.vipps.datamodel.EmailDTO; import no.vipps.service.MailDeliveryService; public class MailDeliveryV3Implementation implements MailDeliveryService{ public static final String NO_REPLY_MAIL_ID = "abc@def.no"; private static final Logger LOGGER = LoggerFactory.getLogger(MailDeliveryV3Implementation.class); @Value("${sendGridAPIKey:}") private String apiKey; public void sendMail(EmailDTO sendDTO) throws IOException { if (CollectionUtils.isEmpty(Arrays.asList(sendDTO.getTo()))) { LOGGER.info("No recipients are defined"); throw new RuntimeException("No Recipients are defined"); } if (StringUtils.isEmpty(sendDTO.getSubject())) { LOGGER.info("No subject is defined"); throw new RuntimeException("No Subject is defined"); } if (StringUtils.isEmpty(sendDTO.getFrom())) { sendDTO.setFrom(NO_REPLY_MAIL_ID); } createSendGridRequest(sendDTO); } private Mail buildEmail(EmailDTO sendDTO) throws IOException { Email from = new Email(sendDTO.getFrom()); if(NO_REPLY_MAIL_ID.equalsIgnoreCase(sendDTO.getFrom())) { from.setName("NoReply "); } Content content = new Content(); if(!StringUtils.isEmpty(sendDTO.getContent_type())) { content.setType(sendDTO.getContent_type()); content.setValue(sendDTO.getMessage()); } else { LOGGER.info("Content_type should be present"); throw new RuntimeException("Content_Type should be present"); } LOGGER.info("Request Email DTO :"+sendDTO.toString()); Mail mail = new Mail(); mail.setFrom(from); mail.setSubject(sendDTO.getSubject()); mail.addContent(content); Personalization person = new Personalization(); for(int i=0;i substitutions = sendDTO.getTemplate().getSubstitution(); Set key = substitutions.keySet(); for(String newKey:key) { person.addSubstitution(newKey, substitutions.get(newKey)); } } mail.addPersonalization(person); if(!CollectionUtils.isEmpty(sendDTO.getAttachment())) { for(Attachments attach : sendDTO.getAttachment()) { mail.addAttachments(attach); } } return mail; } private void createSendGridRequest(EmailDTO sendDTO) throws IOException { SendGrid sg = new SendGrid(apiKey); sg.addRequestHeader("X-Mock", "true"); Mail mailRequest = buildEmail(sendDTO); Request request = new Request(); request.method = Method.POST; request.endpoint = "mail/send"; request.body= mailRequest.build(); LOGGER.info("Mail Request :"+request.body); Response response = sg.api(request); LOGGER.info("Mail Response :"+response.statusCode); } }