1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal

File DefaultMailSender.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
10
1
1
93
48
3
0.3
10
1
3

Classes

Class Line # Actions
DefaultMailSender 50 10 0% 3 3
0.880%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.internal;
21   
22    import java.util.UUID;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26    import javax.mail.Session;
27    import javax.mail.internet.MimeMessage;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.context.ExecutionContext;
32    import org.xwiki.mail.MailListener;
33    import org.xwiki.mail.MailResult;
34    import org.xwiki.mail.MailSender;
35    import org.xwiki.mail.internal.thread.MailQueueManager;
36    import org.xwiki.mail.internal.thread.PrepareMailQueueItem;
37    import org.xwiki.mail.internal.thread.context.Copier;
38   
39    import com.xpn.xwiki.XWikiContext;
40   
41    /**
42    * Default implementation using the {@link org.xwiki.mail.internal.thread.SendMailRunnable} to send emails
43    * asynchronously.
44    *
45    * @version $Id: cd023bbe7e6697e8d0f69347a8e74912a9eaf7b4 $
46    * @since 6.1M2
47    */
48    @Component
49    @Singleton
 
50    public class DefaultMailSender implements MailSender
51    {
52    private static final String SESSION_BATCHID_KEY = "xwiki.batchId";
53   
54    @Inject
55    private Execution execution;
56   
57    @Inject
58    private MailQueueManager<PrepareMailQueueItem> prepareMailQueueManager;
59   
60    @Inject
61    private Copier<ExecutionContext> executionContextCloner;
62   
 
63  40 toggle @Override
64    public MailResult sendAsynchronously(Iterable<? extends MimeMessage> messages, Session session,
65    MailListener listener)
66    {
67    // If the session has specified a batch id, then use it! This can be used for example when resending email.
68  40 String batchId = session.getProperty(SESSION_BATCHID_KEY);
69  40 if (batchId == null) {
70  40 batchId = UUID.randomUUID().toString();
71    }
72   
73    // Pass a clone of the current execution context so that the mail message will be prepared and later sent in the
74    // same context, but in read-only mode (i.e. the preparation of the mail will not impact the current thread's
75    // context).
76  40 ExecutionContext executionContext = this.execution.getContext();
77  40 ExecutionContext clonedExecutionContext = this.executionContextCloner.copy(executionContext);
78   
79    // TODO: Remove once we've found the reason for the functional Mail test flicker: from time to time the wiki
80    // is not set in the Mail Status LT
81  40 XWikiContext xcontext = (XWikiContext) clonedExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
82  40 if (xcontext.getWikiId() == null) {
83  0 throw new RuntimeException(String.format("Aborting Mail Sending: the Wiki Id must not be null in the "
84    + "XWiki Context. Got [%s] in the original context.",
85    ((XWikiContext) executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).getWikiId()));
86    }
87   
88  40 this.prepareMailQueueManager.addToQueue(new PrepareMailQueueItem(messages, session, listener, batchId,
89    clonedExecutionContext));
90   
91  40 return new DefaultMailResult(batchId);
92    }
93    }