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

File MailStatus.java

 

Coverage histogram

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

Code metrics

8
44
23
1
341
140
28
0.64
1.91
23
1.22

Classes

Class Line # Actions
MailStatus 37 44 0% 28 1
0.986666798.7%
 

Contributing tests

This file is covered by 24 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;
21   
22    import java.util.Date;
23   
24    import javax.mail.MessagingException;
25    import javax.mail.internet.InternetAddress;
26   
27    import org.apache.commons.lang3.builder.ToStringBuilder;
28    import org.apache.commons.lang3.exception.ExceptionUtils;
29    import org.xwiki.text.XWikiToStringBuilder;
30   
31    /**
32    * Contains information about a mail (when it was sent, its status, etc).
33    *
34    * @version $Id: 0589283b87376e488ee75b90eff2b198457afa16 $
35    * @since 6.4M3
36    */
 
37    public class MailStatus
38    {
39    /**
40    * @see #getMessageId()
41    */
42    private String uniqueMessageId;
43   
44    /*
45    * @see #getState()
46    */
47    private String state;
48   
49    /*
50    * @see #getBatchID()
51    */
52    private String batchId;
53   
54    /*
55    * @see #getDate()
56    */
57    private Date date;
58   
59    /*
60    * @see #getRecipients()
61    */
62    private String recipients;
63   
64    /*
65    * @see #getType()
66    */
67    private String type;
68   
69    /*
70    * @see #getErrorSummary()
71    */
72    private String errorSummary;
73   
74    /*
75    * @see #getErrorDescription()
76    */
77    private String errorDescription;
78   
79    /*
80    * @see #getWiki()
81    */
82    private String wiki;
83   
84    /**
85    * Default constructor used by Hibernate to load an instance from the Database.
86    */
 
87  42 toggle public MailStatus()
88    {
89    // Empty voluntarily (used by Hibernate)
90    }
91   
92    /**
93    * Constructor initializing the MailStatus with mandatory fields (message id, batch id and recipients are extracted
94    * from the passed message, the date is set as now and the state is passed).
95    * Also sets the Type if set in the passed message.
96    *
97    * @param batchId the identifier of the batch sending the message
98    * @param message the message for which to construct a status
99    * @param state the state of the referenced mail (ready, failed to send, success)
100    * @since 7.4.1
101    */
 
102  51 toggle public MailStatus(String batchId, ExtendedMimeMessage message, MailState state)
103    {
104  51 try {
105  51 setMessageId(message.getUniqueMessageId());
106  51 setBatchId(batchId);
107  51 setType(message.getType());
108  51 setRecipients(InternetAddress.toString(message.getAllRecipients()));
109  51 setState(state);
110  51 setDate(new Date());
111    } catch (MessagingException e) {
112    // This should never happen since the implementation for getHeader() never throws an exception (even
113    // though the interface specifies it can) and similarly getAllRecipients() will also never throw an
114    // exception since the only reason would be if an address is malformed but there's a check when setting
115    // it already in the MimeMessage and thus in practice it cannot happen.
116  0 throw new RuntimeException(String.format(
117    "Unexpected exception constructing the Mail Status for state [%s]", state), e);
118    }
119    }
120   
121    /**
122    * @return the unique message ID used for identifying the mime message matching this status. Between XWiki 7.1rc1
123    * and 7.4, this identifier is equivalent to the message-id header, but you should not rely on this fact. Since
124    * XWiki 7.4.1, this value is equivalent to {@link ExtendedMimeMessage#getUniqueMessageId()}.
125    */
 
126  133 toggle public String getMessageId()
127    {
128  133 return this.uniqueMessageId;
129    }
130   
131    /**
132    * @param messageId see {@link #getMessageId()}
133    */
 
134  102 toggle public void setMessageId(String messageId)
135    {
136  102 this.uniqueMessageId = messageId;
137    }
138   
139    /**
140    * Note: the returned value is a String and not {@link MailState} to allow Hibernate to save that property to the
141    * database.
142    *
143    * @return the state of the mail: ready to be sent, sent successfully, failed to be sent
144    * @see MailState
145    */
 
146  131 toggle public String getState()
147    {
148  131 return this.state;
149    }
150   
151    /**
152    * @param state see {@link #getState()}
153    */
 
154  62 toggle public void setState(MailState state)
155    {
156  62 this.state = state.toString();
157    }
158   
159    /**
160    * Used by Hibernate to load that property from the database.
161    *
162    * @param state see {@link #getState()}
163    */
 
164  40 toggle public void setState(String state)
165    {
166  40 this.state = state;
167    }
168   
169    /**
170    * Used by Hibernate to save that property to the database.
171    *
172    * @return the ID of the batch mail sender
173    */
 
174  114 toggle public String getBatchId()
175    {
176  114 return this.batchId;
177    }
178   
179    /**
180    * Used by Hibernate to load that property from the database.
181    *
182    * @param batchId the ID of the batch mail sender
183    */
 
184  91 toggle public void setBatchId(String batchId)
185    {
186  91 this.batchId = batchId;
187    }
188   
189    /**
190    * Used by Hibernate to save that property to the database.
191    *
192    * @return the date of status of mail
193    */
 
194  61 toggle public Date getDate()
195    {
196  61 return this.date;
197    }
198   
199    /**
200    * Used by Hibernate to load that property from the database.
201    *
202    * @param date of status of mail
203    */
 
204  94 toggle public void setDate(Date date)
205    {
206  94 this.date = date;
207    }
208   
209    /**
210    * Used by Hibernate to save that property to the database (hence the String).
211    *
212    * @return the comma-separated list of email addresses to which the mail was addressed to
213    */
 
214  60 toggle public String getRecipients()
215    {
216  60 return this.recipients;
217    }
218   
219    /**
220    * Used by Hibernate to load that property from the database (hence the reason why it's a String).
221    *
222    * @param recipients see {@link #getRecipients()}
223    */
 
224  91 toggle public void setRecipients(String recipients)
225    {
226  91 this.recipients = recipients;
227    }
228   
229    /**
230    * Used by Hibernate to save that property to the database.
231    *
232    * @return the type of batch mail (Watchlist, news ...)
233    */
 
234  76 toggle public String getType()
235    {
236  76 return this.type;
237    }
238   
239    /**
240    * Used by Hibernate to load that property from the database.
241    *
242    * @param type of batch mail (Watchlist, news ...)
243    */
 
244  89 toggle public void setType(String type)
245    {
246  89 this.type = type;
247    }
248   
249    /**
250    * Used by Hibernate to save that property to the database.
251    *
252    * @return the error message summary when the mail has failed to be sent
253    */
 
254  73 toggle public String getErrorSummary()
255    {
256  73 return this.errorSummary;
257    }
258   
259    /**
260    * Used by Hibernate to save that property to the database.
261    *
262    * @return the error message description (the full stack trace for example) when the mail has failed to be sent
263    */
 
264  36 toggle public String getErrorDescription()
265    {
266  36 return this.errorDescription;
267    }
268   
269    /**
270    * Used by Hibernate to load that property from the database.
271    *
272    * @param errorSummary see {@link #getErrorSummary()}
273    */
 
274  40 toggle public void setErrorSummary(String errorSummary)
275    {
276  40 this.errorSummary = errorSummary;
277    }
278   
279    /**
280    * Used by Hibernate to load that property from the database.
281    *
282    * @param errorDescription see {@link #getErrorDescription()}
283    */
 
284  40 toggle public void setErrorDescription(String errorDescription)
285    {
286  40 this.errorDescription = errorDescription;
287    }
288   
289    /**
290    * @param exception the exception that was encountered during sending mail
291    */
 
292  14 toggle public void setError(Exception exception)
293    {
294  14 this.errorSummary = ExceptionUtils.getRootCauseMessage(exception);
295  14 this.errorDescription = ExceptionUtils.getStackTrace(exception);
296    }
297   
298    /**
299    * Used by Hibernate to save that property to the database.
300    *
301    * @return the wiki in which the message is trying to be sent (can be null, in which case it means the main wiki)
302    */
 
303  72 toggle public String getWiki()
304    {
305  72 return this.wiki;
306    }
307   
308    /**
309    * Used by Hibernate to load that property from the database.
310    *
311    * @param wiki see {@link #getWiki()}
312    */
 
313  53 toggle public void setWiki(String wiki)
314    {
315  53 this.wiki = wiki;
316    }
317   
 
318  6 toggle @Override
319    public String toString()
320    {
321  6 ToStringBuilder builder = new XWikiToStringBuilder(this);
322  6 builder.append("messageId", getMessageId());
323  6 builder.append("batchId", getBatchId());
324  6 builder.append("state", getState());
325  6 builder.append("date", getDate());
326  6 builder.append("recipients", getRecipients());
327  6 if (getType() != null) {
328  4 builder.append("type", getType());
329    }
330  6 if (getErrorSummary() != null) {
331  3 builder.append("errorSummary", getErrorSummary());
332    }
333  6 if (getErrorDescription() != null) {
334  3 builder.append("errorDescription", getErrorDescription());
335    }
336  6 if (getWiki() != null) {
337  2 builder.append("wiki", getWiki());
338    }
339  6 return builder.toString();
340    }
341    }