1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.mailsender

File MailSenderPluginApi.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

8
61
11
1
208
136
19
0.31
5.55
11
1.73

Classes

Class Line # Actions
MailSenderPluginApi 40 61 0% 19 55
0.312531.2%
 

Contributing tests

This file is covered by 3 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 com.xpn.xwiki.plugin.mailsender;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import org.apache.velocity.VelocityContext;
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28   
29    import com.xpn.xwiki.XWikiContext;
30    import com.xpn.xwiki.api.Attachment;
31    import com.xpn.xwiki.api.XWiki;
32    import com.xpn.xwiki.plugin.PluginApi;
33   
34    /**
35    * Plugin that brings powerful mailing capabilities. This is the wrapper accessible from in-document scripts.
36    *
37    * @see MailSender
38    * @version $Id: 5f6b291d7142cba6b8e814baec1a9ce14639e3dc $
39    */
 
40    public class MailSenderPluginApi extends PluginApi<MailSenderPlugin> implements MailSender
41    {
42    /**
43    * Log object to log messages in this class.
44    */
45    private static final Logger LOGGER = LoggerFactory.getLogger(MailSenderPluginApi.class);
46   
47    /**
48    * API constructor.
49    *
50    * @param plugin The wrapped plugin object.
51    * @param context Context of the request.
52    * @see PluginApi#PluginApi(com.xpn.xwiki.plugin.XWikiPluginInterface,XWikiContext)
53    */
 
54  4 toggle public MailSenderPluginApi(MailSenderPlugin plugin, XWikiContext context)
55    {
56  4 super(plugin, context);
57    }
58   
 
59  0 toggle @Override
60    public int sendHtmlMessage(String from, String to, String cc, String bcc, String subject, String body,
61    String alternative, List<Attachment> attachments)
62    {
63  0 Mail email = new Mail();
64  0 email.setSubject(subject);
65  0 email.setFrom(from);
66  0 email.setTo(to);
67  0 email.setCc(cc);
68  0 email.setBcc(bcc);
69  0 email.setTextPart(alternative);
70  0 email.setHtmlPart(body);
71  0 email.setAttachments(attachments);
72  0 return sendMail(email);
73    }
74   
 
75  0 toggle @Override
76    public int sendTextMessage(String from, String to, String subject, String message)
77    {
78  0 Mail email = new Mail();
79  0 email.setSubject(subject);
80  0 email.setTextPart(message);
81  0 email.setFrom(from);
82  0 email.setTo(to);
83  0 return sendMail(email);
84    }
85   
 
86  0 toggle @Override
87    public int sendTextMessage(String from, String to, String cc, String bcc, String subject, String message,
88    List<Attachment> attachments)
89    {
90  0 Mail email = new Mail();
91  0 email.setSubject(subject);
92  0 email.setTextPart(message);
93  0 email.setFrom(from);
94  0 email.setTo(to);
95  0 email.setCc(cc);
96  0 email.setBcc(bcc);
97  0 email.setAttachments(attachments);
98  0 return sendMail(email);
99    }
100   
 
101  1 toggle @Override
102    public int sendRawMessage(String from, String to, String rawMessage)
103    {
104  1 Mail email = new Mail();
105  1 email.setFrom(from);
106  1 email.setTo(to);
107   
108  1 getProtectedPlugin().parseRawMessage(rawMessage, email);
109  1 return sendMail(email);
110    }
111   
 
112  0 toggle @Override
113    public int sendMessageFromTemplate(String from, String to, String cc, String bcc, String language,
114    String documentFullName, VelocityContext vcontext)
115    {
116  0 try {
117  0 return getProtectedPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, language, vcontext,
118    this.context);
119    } catch (Exception e) {
120    // If the exception is a null pointer exception there is no message and e.getMessage() is null.
121  0 if (e.getMessage() != null) {
122  0 this.context.put("error", e.getMessage());
123    }
124  0 LOGGER.error("sendMessageFromTemplate", e);
125  0 return -1;
126    }
127    }
128   
129    /**
130    * Uses an XWiki document to build the message subject and context, based on variables stored in a map. Sends the
131    * email.
132    *
133    * @param from Email sender
134    * @param to Email recipient
135    * @param cc Email Carbon Copy
136    * @param bcc Email Hidden Carbon Copy
137    * @param language Language of the email
138    * @param documentFullName Full name of the template to be used (example: XWiki.MyEmailTemplate). The template needs
139    * to have an XWiki.Email object attached
140    * @param parameters variables to be passed to the velocity context
141    * @return 0 on success, -1 on failure. On failure the error message is stored in the XWiki context under the
142    * "error" key.
143    */
 
144  1 toggle public int sendMessageFromTemplate(String from, String to, String cc, String bcc, String language,
145    String documentFullName, Map<String, Object> parameters)
146    {
147  1 try {
148  1 return getProtectedPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, language, parameters,
149    this.context);
150    } catch (Exception e) {
151    // If the exception is a null pointer exception there is no message and e.getMessage() is null.
152  0 if (e.getMessage() != null) {
153  0 this.context.put("error", e.getMessage());
154    }
155  0 LOGGER.error("sendMessageFromTemplate", e);
156  0 return -1;
157    }
158    }
159   
 
160  2 toggle @Override
161    public Mail createMail()
162    {
163  2 return new Mail();
164    }
165   
 
166  2 toggle @Override
167    public int sendMail(Mail mail)
168    {
169  2 int result = 0;
170  2 try {
171  2 getProtectedPlugin().sendMail(mail, this.context);
172    } catch (Exception e) {
173    // If the exception is a null pointer exception there is no message and e.getMessage() is null.
174  0 if (e.getMessage() != null) {
175  0 this.context.put("error", e.getMessage());
176    }
177  0 LOGGER.error("Failed to send email [" + mail.toString() + "]", e);
178  0 result = -1;
179    }
180   
181  2 return result;
182    }
183   
 
184  1 toggle @Override
185    public MailConfiguration createMailConfiguration(XWiki xwiki)
186    {
187  1 return new MailConfiguration(xwiki);
188    }
189   
 
190  1 toggle @Override
191    public int sendMail(Mail mail, MailConfiguration mailConfiguration)
192    {
193  1 int result = 0;
194  1 try {
195  1 getProtectedPlugin().sendMail(mail, mailConfiguration, this.context);
196    } catch (Exception e) {
197    // If the exception is a null pointer exception there is no message and e.getMessage() is null.
198  0 if (e.getMessage() != null) {
199  0 this.context.put("error", e.getMessage());
200    }
201  0 LOGGER.error("Failed to send email [" + mail.toString() + "] using mail configuration ["
202    + mailConfiguration.toString() + "]", e);
203  0 result = -1;
204    }
205   
206  1 return result;
207    }
208    }