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

File AbstractTemplateMimeBodyPartFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
19
1
1
111
61
6
0.32
19
1
6

Classes

Class Line # Actions
AbstractTemplateMimeBodyPartFactory 48 19 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 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.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    * Creates an Body Part from a Document Reference pointing to a Document containing an XWiki.Mail XObject (the first one
43    * found is used).
44    *
45    * @version $Id: e05e8695d3fddade9fed29e4a10a1fb09a17dccc $
46    * @since 6.1RC1
47    */
 
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 the Template Manager instance to use, this allows passing either the default component implementation or
66    * a secure one for scripts
67    */
68    protected abstract MailTemplateManager getTemplateManager();
69   
 
70  10 toggle @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    // Handle attachments:
85    // - if the user has passed an "attachments" property with a list of attachment then add them
86    // - if the user has set the "includeTemplateAttachments" property then add all attachments found in the
87    // template document too
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    }