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

File AbstractTemplateMimeMessageFactory.java

 

Coverage histogram

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

Code metrics

6
22
2
1
112
55
5
0.23
11
2
2.5

Classes

Class Line # Actions
AbstractTemplateMimeMessageFactory 45 22 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 2 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.Map;
23   
24    import javax.inject.Inject;
25    import javax.mail.Address;
26    import javax.mail.Message;
27    import javax.mail.MessagingException;
28    import javax.mail.Multipart;
29    import javax.mail.internet.MimeMessage;
30    import javax.mail.internet.MimeMultipart;
31   
32    import org.xwiki.mail.ExtendedMimeMessage;
33    import org.xwiki.mail.MimeBodyPartFactory;
34    import org.xwiki.mail.internal.factory.AbstractMimeMessageFactory;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.properties.ConverterManager;
37   
38    /**
39    * Creates a Mime Message with the subject pre-filled with the evaluated "subject" xproperty found in an "XWiki.Mail"
40    * xobject located in the template document pointed to by the passed reference.
41    *
42    * @version $Id: f95cbb4c935da7dd888460cd21fad7fe02e7bd55 $
43    * @since 6.1RC1
44    */
 
45    public abstract class AbstractTemplateMimeMessageFactory extends AbstractMimeMessageFactory<MimeMessage>
46    {
47    @Inject
48    private ConverterManager converterManager;
49   
50    /**
51    * @return the Template Manager instance to use, this allows passing either the default component implementation or
52    * a secure one for scripts
53    */
54    protected abstract MailTemplateManager getTemplateManager();
55   
56    /**
57    * @return the Body Part Factory instance to use, this allows passing either the default component implementation or
58    * a secure one for scripts
59    */
60    protected abstract MimeBodyPartFactory<DocumentReference> getMimeBodyPartFactory();
61   
 
62  8 toggle @Override
63    public MimeMessage createMessage(Object templateReferenceObject, Map<String, Object> parameters)
64    throws MessagingException
65    {
66  8 DocumentReference templateReference = getTypedSource(templateReferenceObject, DocumentReference.class);
67   
68    // Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
69    // given a valid Session when it's deserialized from the mail content store for sending.
70  8 ExtendedMimeMessage message = new ExtendedMimeMessage();
71   
72    // Handle optional "from" address.
73  8 Address from = this.converterManager.convert(Address.class, parameters.get("from"));
74  8 if (from != null) {
75  6 message.setFrom(from);
76    }
77   
78    // Handle optional "to", "cc" and "bcc" addresses.
79  8 setRecipient(message, Message.RecipientType.TO, parameters.get("to"));
80  8 setRecipient(message, Message.RecipientType.CC, parameters.get("cc"));
81  8 setRecipient(message, Message.RecipientType.BCC, parameters.get("bcc"));
82   
83    // Handle optional "type" parameter to set the mail type
84    // Set the Message type if passed in parameters
85  8 String type = (String) parameters.get("type");
86  8 if (type != null) {
87  2 message.setType(type);
88    }
89   
90    // Handle the subject. Get it from the template
91  8 Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
92  8 Object localeValue = parameters.get("language");
93  8 String subject = getTemplateManager().evaluate(templateReference, "subject", velocityVariables, localeValue);
94  8 message.setSubject(subject);
95   
96    // Add a default body part taken from the template.
97  8 Multipart multipart = new MimeMultipart("mixed");
98  8 multipart.addBodyPart(getMimeBodyPartFactory().create(templateReference, parameters));
99  8 message.setContent(multipart);
100   
101  8 return message;
102    }
103   
 
104  24 toggle private void setRecipient(MimeMessage message, Message.RecipientType type, Object value)
105    throws MessagingException
106    {
107  24 Address[] addresses = this.converterManager.convert(Address[].class, value);
108  24 if (addresses != null) {
109  4 message.setRecipients(type, addresses);
110    }
111    }
112    }