Subversion Repositories SmartDukaan

Rev

Rev 36623 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36623 Rev 37477
Line 6... Line 6...
6
import org.apache.commons.io.FileUtils;
6
import org.apache.commons.io.FileUtils;
7
import org.apache.commons.lang3.StringEscapeUtils;
7
import org.apache.commons.lang3.StringEscapeUtils;
8
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
9
import org.apache.logging.log4j.Logger;
10
import org.springframework.core.io.InputStreamSource;
10
import org.springframework.core.io.InputStreamSource;
-
 
11
import com.spice.profitmandi.common.mail.MailQueue;
-
 
12
import com.spice.profitmandi.common.mail.MailQueueHolder;
-
 
13
import com.spice.profitmandi.common.mail.MailRequest;
-
 
14
import org.springframework.core.io.FileSystemResource;
-
 
15
import java.io.File;
-
 
16
import java.io.Serializable;
-
 
17
import java.util.ArrayList;
-
 
18
import java.util.List;
-
 
19
import java.util.Map;
11
import org.springframework.mail.javamail.JavaMailSender;
20
import org.springframework.mail.javamail.JavaMailSender;
12
import org.springframework.mail.javamail.MimeMessageHelper;
21
import org.springframework.mail.javamail.MimeMessageHelper;
13
 
22
 
14
import javax.mail.Multipart;
23
import javax.mail.Multipart;
15
import javax.mail.internet.InternetAddress;
24
import javax.mail.internet.InternetAddress;
Line 132... Line 141...
132
        paramsMap.put("mobile", mobileNumber);
141
        paramsMap.put("mobile", mobileNumber);
133
        paramsMap.put("messageType", "Text");
142
        paramsMap.put("messageType", "Text");
134
        // rc.getResponse(SMS_GATEWAY, paramsMap, null);
143
        // rc.getResponse(SMS_GATEWAY, paramsMap, null);
135
    }
144
    }
136
 
145
 
-
 
146
    /*
-
 
147
     * Mail helpers. These no longer send anything themselves.
-
 
148
     *
-
 
149
     * Each used to build its own MimeMessage, hardcode From: noreply@smartdukaan.com,
-
 
150
     * and push it straight down whichever JavaMailSender the caller passed. That From
-
 
151
     * is precisely why mail was being refused — an authenticated Workspace session may
-
 
152
     * send only as the account it logged in as, and noreply@ is a different identity.
-
 
153
     * A failed send also took the calling job down with it, which is how one expired
-
 
154
     * credential came to mark ten report jobs FAILED.
-
 
155
     *
-
 
156
     * They now hand the mail to the outbox, which owns retry, de-duplication and the
-
 
157
     * choice of transport. The JavaMailSender parameter is ignored and kept only so the
-
 
158
     * 27 existing call sites continue to compile; new code should inject
-
 
159
     * {@link com.spice.profitmandi.common.mail.MailQueue} and build a MailRequest.
-
 
160
     */
-
 
161
 
-
 
162
    @Deprecated
137
    public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
163
    public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
138
                                               String body, List<File> attachments) throws Exception {
164
                                               String body, List<File> attachments) {
139
        MimeMessage message = mailSender.createMimeMessage();
165
        queue(MailRequest.to(emailTo).cc(cc).subject(subject).body(body)
140
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
166
                .attach(fromFiles(attachments)));
-
 
167
    }
-
 
168
 
-
 
169
    @Deprecated
-
 
170
    public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
-
 
171
                                              String body, String fileName, InputStreamSource inputStreamSource) {
141
        helper.setSubject(subject);
172
        queue(MailRequest.to(emailTo).cc(cc).subject(subject).body(body)
-
 
173
                .attach(new Attachment(fileName, inputStreamSource)));
-
 
174
    }
-
 
175
 
142
        helper.setText(body);
176
    @Deprecated
-
 
177
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
-
 
178
                                               String subject, String body, Attachment... attachments) {
-
 
179
        sendMailWithAttachments(mailSender, emailTo, cc, bcc, subject, body, false, attachments);
-
 
180
    }
-
 
181
 
143
        if (cc != null) {
182
    @Deprecated
-
 
183
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
-
 
184
                                               String subject, String body, boolean html, Attachment... attachments) {
-
 
185
        MailRequest request = MailRequest.to(emailTo).cc(cc).bcc(bcc).subject(subject).attach(attachments);
144
            helper.setCc(cc);
186
        queue(html ? request.html(body) : request.body(body));
145
        }
187
    }
-
 
188
 
146
        helper.setTo(emailTo);
189
    @Deprecated
-
 
190
    public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
-
 
191
                                                   String subject, String body, Attachment... attachments) {
147
        // helper.setTo("amit.gupta@smartdukaan.com");
192
        queue(MailRequest.to(emailTo).cc(cc).subject(subject).html(body).attach(attachments));
-
 
193
    }
-
 
194
 
-
 
195
    /**
-
 
196
     * Inline images become ordinary attachments: the outbox stores attachments, not
-
 
197
     * CID-referenced parts. The mail still carries its images rather than being lost.
-
 
198
     */
-
 
199
    @Deprecated
-
 
200
    public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
148
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
201
                                            String body, Map<? extends Serializable, File> inlineImages) {
149
        helper.setFrom(senderAddress);
202
        List<Attachment> images = new ArrayList<>();
150
        if (attachments != null) {
203
        if (inlineImages != null) {
-
 
204
            for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {
151
            for (File file : attachments) {
205
                File file = entry.getValue();
-
 
206
                if (file != null) {
152
                helper.addAttachment(file.getName(), file);
207
                    images.add(new Attachment(file.getName(), new FileSystemResource(file)));
-
 
208
                }
153
            }
209
            }
154
        }
210
        }
-
 
211
        queue(MailRequest.to(emailTo).cc(cc).subject(subject).html(body)
-
 
212
                .attach(images.toArray(new Attachment[0])));
-
 
213
    }
-
 
214
 
-
 
215
    @Deprecated
-
 
216
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
-
 
217
                                               String body, List<File> attachments) {
-
 
218
        queue(MailRequest.to(emailTo).cc(cc).subject(subject).body(body)
-
 
219
                .attach(fromFiles(attachments)));
-
 
220
    }
-
 
221
 
-
 
222
    private static void queue(MailRequest request) {
-
 
223
        MailQueue queue = MailQueueHolder.get();
-
 
224
        if (queue != null) {
-
 
225
            queue.queue(request.source("Utils"));
-
 
226
        }
-
 
227
    }
-
 
228
 
-
 
229
    private static Attachment[] fromFiles(List<File> files) {
-
 
230
        if (files == null || files.isEmpty()) {
-
 
231
            return null;
-
 
232
        }
-
 
233
        List<Attachment> converted = new ArrayList<>(files.size());
-
 
234
        for (File file : files) {
-
 
235
            if (file != null) {
-
 
236
                converted.add(new Attachment(file.getName(), new FileSystemResource(file)));
-
 
237
            }
-
 
238
        }
-
 
239
        return converted.toArray(new Attachment[0]);
-
 
240
    }
-
 
241
 
-
 
242
    public static String getIconUrl(int entityId, String host, int port, String webapp) {
-
 
243
        return "";
-
 
244
    }
-
 
245
 
-
 
246
    public static String getHyphenatedString(String s) {
-
 
247
        s = s.trim().replaceAll("\\s+", " ");
-
 
248
        s = s.replaceAll("\\s", "-");
155
        mailSender.send(message);
249
        return s.toLowerCase();
-
 
250
    }
-
 
251
 
-
 
252
    public static void main(String[] args) throws Exception {
-
 
253
        Utils.sendSms("Hello", "9990381569");
156
    }
254
    }
157
 
255
 
158
    public static class Attachment {
256
    public static class Attachment {
159
        public Attachment(String fileName, InputStreamSource inputStreamSource) {
257
        public Attachment(String fileName, InputStreamSource inputStreamSource) {
160
            this.fileName = fileName;
258
            this.fileName = fileName;
Line 184... Line 282...
184
        public String toString() {
282
        public String toString() {
185
            return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
283
            return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
186
        }
284
        }
187
    }
285
    }
188
 
286
 
189
    public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
-
 
190
                                              String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
-
 
191
        MimeMessage message = mailSender.createMimeMessage();
-
 
192
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-
 
193
        helper.setSubject(subject);
-
 
194
        helper.setText(body);
-
 
195
        if (cc != null) {
-
 
196
            helper.setCc(cc);
-
 
197
        }
-
 
198
        helper.setTo(emailTo);
-
 
199
        helper.addAttachment(fileName, inputStreamSource);
-
 
200
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
-
 
201
        helper.setFrom(senderAddress);
-
 
202
        mailSender.send(message);
-
 
203
    }
-
 
204
 
-
 
205
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
-
 
206
                                               String subject, String body, Attachment... attachments) throws Exception {
-
 
207
        sendMailWithAttachments(mailSender, emailTo, cc, bcc,
-
 
208
                subject, body, false, attachments);
-
 
209
    }
-
 
210
 
-
 
211
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
-
 
212
                                               String subject, String body, boolean html, Attachment... attachments) throws Exception {
-
 
213
        MimeMessage message = mailSender.createMimeMessage();
-
 
214
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-
 
215
        helper.setSubject(subject);
-
 
216
        helper.setText(body, html);
-
 
217
        if (cc != null) {
-
 
218
            helper.setCc(cc);
-
 
219
        }
-
 
220
        if (bcc != null) {
-
 
221
            helper.setBcc(bcc);
-
 
222
        }
-
 
223
        helper.setTo(emailTo);
-
 
224
        for (Attachment attachment : attachments) {
-
 
225
            //saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
-
 
226
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
-
 
227
        }
-
 
228
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
-
 
229
        helper.setFrom(senderAddress);
-
 
230
        mailSender.send(message);
-
 
231
    }
-
 
232
 
-
 
233
    public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
-
 
234
                                                   String subject, String body, Attachment... attachments) throws Exception {
-
 
235
        MimeMessage message = mailSender.createMimeMessage();
-
 
236
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-
 
237
        helper.setSubject(subject);
-
 
238
        helper.setText(body, true);
-
 
239
        // helper.setText(body, true);
-
 
240
        if (cc != null) {
-
 
241
            helper.setCc(cc);
-
 
242
        }
-
 
243
        helper.setTo(emailTo);
-
 
244
        for (Attachment attachment : attachments) {
-
 
245
            if (attachment == null)
-
 
246
                break;
-
 
247
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
-
 
248
        }
-
 
249
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
-
 
250
        helper.setFrom(senderAddress);
-
 
251
        mailSender.send(message);
-
 
252
    }
-
 
253
 
-
 
254
    public static String getIconUrl(int entityId, String host, int port, String webapp) {
-
 
255
        return "";
-
 
256
    }
-
 
257
 
-
 
258
    public static void main(String[] args) throws Exception {
-
 
259
        Utils.sendSms("Hello", "9990381569");
-
 
260
    }
-
 
261
 
-
 
262
    public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
-
 
263
                                            String body, Map<? extends Serializable, File> map) throws Exception {
-
 
264
        MimeMessage message = mailSender.createMimeMessage();
-
 
265
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-
 
266
        helper.setSubject(subject);
-
 
267
        Multipart mp = new MimeMultipart();
-
 
268
 
-
 
269
        MimeBodyPart messageBodyPart = new MimeBodyPart();
-
 
270
        messageBodyPart.setContent(body, "text/html");
-
 
271
        mp.addBodyPart(messageBodyPart);
-
 
272
 
-
 
273
        // helper.setText(body, true);
-
 
274
        if (cc != null) {
-
 
275
            helper.setCc(cc);
-
 
276
        }
-
 
277
        helper.setTo(emailTo);
-
 
278
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
-
 
279
        helper.setFrom(senderAddress);
-
 
280
        for (Map.Entry<? extends Serializable, File> entry : map.entrySet()) {
-
 
281
            entry.getKey();
-
 
282
            entry.getValue();
-
 
283
            MimeBodyPart imagePart = new MimeBodyPart();
-
 
284
            imagePart.setHeader("Content-ID", entry.getKey() + "");
-
 
285
            imagePart.setDisposition(MimeBodyPart.INLINE);
-
 
286
            imagePart.attachFile(entry.getValue());
-
 
287
            mp.addBodyPart(imagePart);
-
 
288
 
-
 
289
        }
-
 
290
        message.setContent(mp);
-
 
291
        mailSender.send(message);
-
 
292
 
-
 
293
    }
-
 
294
 
-
 
295
    public String html(String string) {
-
 
296
        return org.apache.commons.lang.StringEscapeUtils.escapeHtml(String.valueOf(string));
-
 
297
    }
-
 
298
 
-
 
299
    public String htmlJson(Object object) {
-
 
300
        return StringEscapeUtils.escapeHtml4(gson.toJson(object));
-
 
301
    }
-
 
302
 
-
 
303
    public static String getHyphenatedString(String s) {
-
 
304
        s = s.trim().replaceAll("\\s+", " ");
-
 
305
        s = s.replaceAll("\\s", "-");
-
 
306
        return s.toLowerCase();
-
 
307
    }
-
 
308
 
-
 
309
    public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
-
 
310
                                               String body, Attachment... attachments) throws Exception {
-
 
311
        MimeMessage message = mailSender.createMimeMessage();
-
 
312
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-
 
313
        helper.setSubject(subject);
-
 
314
        helper.setText(body, true);
-
 
315
        if (cc != null) {
-
 
316
            helper.setCc(cc);
-
 
317
        }
-
 
318
        helper.setTo(emailTo);
-
 
319
        for (Attachment attachment : attachments) {
-
 
320
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
-
 
321
            //saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
-
 
322
        }
-
 
323
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
-
 
324
        helper.setFrom(senderAddress);
-
 
325
        mailSender.send(message);
-
 
326
    }
-
 
327
 
287
 
328
    public static int compareFloat(float f1, float f2) {
288
    public static int compareFloat(float f1, float f2) {
329
        if (Math.abs(f1 - f2) < DOUBLE_EPSILON) {
289
        if (Math.abs(f1 - f2) < DOUBLE_EPSILON) {
330
            return 0;
290
            return 0;
331
        }
291
        }