Subversion Repositories SmartDukaan

Rev

Rev 36398 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.services;

import com.spice.profitmandi.common.mail.MailQueue;
import com.spice.profitmandi.common.mail.MailRequest;
import com.spice.profitmandi.common.mail.MailSenderType;
import com.spice.profitmandi.common.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Deprecated. Every method here now hands the mail to {@link MailQueue} and
 * returns; nothing is sent inline any more.
 *
 * This class used to build its own MimeMessage, hardcode
 * From: noreply@smartdukaan.com in five places, and send straight down whichever
 * JavaMailSender the caller happened to pass. That From is the reason mail was
 * being rejected: an authenticated Workspace session may only send as the
 * account it logged in as, and noreply@ is a different identity in the same
 * domain. It also meant a failed send was simply lost, and took the calling job
 * down with it.
 *
 * The signatures are kept so the ~11 existing callers keep compiling. The
 * JavaMailSender argument is ignored — choosing a transport is not the caller's
 * job. New code should use {@link MailQueue} directly.
 */
@Deprecated
@Service
public class EmailServiceImpl implements EmailService {

    private final MailQueue mailQueue;

    @Autowired
    public EmailServiceImpl(MailQueue mailQueue) {
        this.mailQueue = mailQueue;
    }

    @Override
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
                                        String subject, String body) {
        send(emailTo, cc, null, subject, body, true, null);
    }

    @Override
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
                                        String subject, String body, List<File> attachments) {
        send(emailTo, cc, null, subject, body, true, fromFiles(attachments));
    }

    @Override
    public void sendMailWithAttachments(JavaMailSender ignored, String emailTo, String[] cc,
                                        String subject, String body, List<File> attachments) {
        send(new String[]{emailTo}, cc, null, subject, body, true, fromFiles(attachments));
    }

    @Override
    public void sendMailWithAttachment(JavaMailSender ignored, String[] emailTo, String[] cc,
                                       String subject, String body,
                                       String fileName, InputStreamSource inputStreamSource) {
        send(emailTo, cc, null, subject, body, true,
                new Utils.Attachment[]{new Utils.Attachment(fileName, inputStreamSource)});
    }

    @Override
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc, String[] bcc,
                                        String subject, String body, boolean html,
                                        Attachment... attachments) {
        send(emailTo, cc, bcc, subject, body, html, convert(attachments));
    }

    @Override
    public void sendHtmlMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
                                            String subject, String body, Attachment... attachments) {
        send(emailTo, cc, null, subject, body, true, convert(attachments));
    }

    /**
     * Inline images are not carried through the outbox — it stores attachments,
     * not CID-referenced parts. The images are attached normally instead, so the
     * mail still arrives with its content rather than being dropped.
     */
    @Override
    public void sendEmbeddedHtmlMail(JavaMailSender ignored, String[] emailTo, String[] cc,
                                     String subject, String body,
                                     Map<? extends Serializable, File> inlineImages) {
        List<Utils.Attachment> images = new ArrayList<>();
        if (inlineImages != null) {
            for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {
                File file = entry.getValue();
                if (file != null) {
                    images.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
                }
            }
        }
        send(emailTo, cc, null, subject, body, true, images.toArray(new Utils.Attachment[0]));
    }

    private void send(String[] to, String[] cc, String[] bcc, String subject, String body,
                      boolean html, Utils.Attachment[] attachments) {
        MailRequest request = MailRequest.to(to)
                .cc(cc)
                .bcc(bcc)
                .subject(subject)
                .from(MailSenderType.RELAY)
                .source("EmailService");
        if (html) {
            request.html(body);
        } else {
            request.body(body);
        }
        if (attachments != null && attachments.length > 0) {
            request.attach(attachments);
        }
        mailQueue.queue(request);
    }

    private Utils.Attachment[] fromFiles(List<File> files) {
        if (files == null || files.isEmpty()) {
            return null;
        }
        List<Utils.Attachment> converted = new ArrayList<>(files.size());
        for (File file : files) {
            if (file != null) {
                converted.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
            }
        }
        return converted.toArray(new Utils.Attachment[0]);
    }

    private Utils.Attachment[] convert(Attachment[] attachments) {
        if (attachments == null || attachments.length == 0) {
            return null;
        }
        Utils.Attachment[] converted = new Utils.Attachment[attachments.length];
        for (int i = 0; i < attachments.length; i++) {
            converted[i] = new Utils.Attachment(
                    attachments[i].getFileName(), attachments[i].getInputStreamSource());
        }
        return converted;
    }
}