| 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.factory.template; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.List; |
| 25 |
|
import java.util.Map; |
| 26 |
|
|
| 27 |
|
import javax.inject.Inject; |
| 28 |
|
import javax.inject.Named; |
| 29 |
|
import javax.mail.MessagingException; |
| 30 |
|
import javax.mail.internet.MimeBodyPart; |
| 31 |
|
|
| 32 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 33 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
| 34 |
|
import org.xwiki.mail.internal.factory.AbstractMimeBodyPartFactory; |
| 35 |
|
import org.xwiki.model.reference.DocumentReference; |
| 36 |
|
|
| 37 |
|
import com.xpn.xwiki.api.Attachment; |
| 38 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 39 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@version |
| 46 |
|
@since |
| 47 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 6 |
Complexity Density: 0.32 |
|
| 48 |
|
public abstract class AbstractTemplateMimeBodyPartFactory extends AbstractMimeBodyPartFactory<DocumentReference> |
| 49 |
|
{ |
| 50 |
|
private static final String ATTACHMENT_PROPERTY_NAME = "attachments"; |
| 51 |
|
|
| 52 |
|
private static final String INCLUDE_TEMPLATE_ATTACHMENTS_PROPERTY_NAME = "includeTemplateAttachments"; |
| 53 |
|
|
| 54 |
|
@Inject |
| 55 |
|
@Named("text/html") |
| 56 |
|
private MimeBodyPartFactory<String> htmlBodyPartFactory; |
| 57 |
|
|
| 58 |
|
@Inject |
| 59 |
|
private DocumentAccessBridge bridge; |
| 60 |
|
|
| 61 |
|
@Inject |
| 62 |
|
private AttachmentConverter attachmentConverter; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@return |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
protected abstract MailTemplateManager getTemplateManager(); |
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 6 |
Complexity Density: 0.32 |
|
| 70 |
10 |
@Override... |
| 71 |
|
public MimeBodyPart create(DocumentReference documentReference, Map<String, Object> parameters) |
| 72 |
|
throws MessagingException |
| 73 |
|
{ |
| 74 |
10 |
Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables"); |
| 75 |
|
|
| 76 |
10 |
Object localeValue = parameters.get("language"); |
| 77 |
|
|
| 78 |
10 |
String textContent = getTemplateManager().evaluate(documentReference, "text", velocityVariables, localeValue); |
| 79 |
10 |
String htmlContent = getTemplateManager().evaluate(documentReference, "html", velocityVariables, localeValue); |
| 80 |
|
|
| 81 |
10 |
Map<String, Object> htmlParameters = new HashMap<>(); |
| 82 |
10 |
htmlParameters.put("alternate", textContent); |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
10 |
List<Attachment> attachments = new ArrayList<>(); |
| 89 |
10 |
List<Attachment> parameterAttachments = (List<Attachment>) parameters.get(ATTACHMENT_PROPERTY_NAME); |
| 90 |
10 |
if (parameterAttachments != null) { |
| 91 |
4 |
attachments.addAll(parameterAttachments); |
| 92 |
|
} |
| 93 |
10 |
Boolean includeTemplateAttachments = (Boolean) parameters.get(INCLUDE_TEMPLATE_ATTACHMENTS_PROPERTY_NAME); |
| 94 |
10 |
if (includeTemplateAttachments != null && includeTemplateAttachments) { |
| 95 |
5 |
try { |
| 96 |
5 |
List<XWikiAttachment> xwikiAttachments = |
| 97 |
|
((XWikiDocument) this.bridge.getDocument(documentReference)).getAttachmentList(); |
| 98 |
4 |
attachments.addAll(this.attachmentConverter.convert(xwikiAttachments)); |
| 99 |
|
} catch (Exception e) { |
| 100 |
1 |
throw new MessagingException( |
| 101 |
|
String.format("Failed to include attachments from the Mail Template [%s]", documentReference), |
| 102 |
|
e); |
| 103 |
|
} |
| 104 |
|
} |
| 105 |
9 |
if (!attachments.isEmpty()) { |
| 106 |
5 |
htmlParameters.put(ATTACHMENT_PROPERTY_NAME, attachments); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
9 |
return this.htmlBodyPartFactory.create(htmlContent, htmlParameters); |
| 110 |
|
} |
| 111 |
|
} |