| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@version |
| 46 |
|
@since |
| 47 |
|
|
| 48 |
|
@Component |
| 49 |
|
@Singleton |
| |
|
| 80% |
Uncovered Elements: 3 (15) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 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 |
|
|
| |
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 63 |
40 |
@Override... |
| 64 |
|
public MailResult sendAsynchronously(Iterable<? extends MimeMessage> messages, Session session, |
| 65 |
|
MailListener listener) |
| 66 |
|
{ |
| 67 |
|
|
| 68 |
40 |
String batchId = session.getProperty(SESSION_BATCHID_KEY); |
| 69 |
40 |
if (batchId == null) { |
| 70 |
40 |
batchId = UUID.randomUUID().toString(); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
40 |
ExecutionContext executionContext = this.execution.getContext(); |
| 77 |
40 |
ExecutionContext clonedExecutionContext = this.executionContextCloner.copy(executionContext); |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 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 |
|
} |