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

File DefaultWatchListNotifier.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
51
5
1
240
145
10
0.2
10.2
5
2

Classes

Class Line # Actions
DefaultWatchListNotifier 66 51 0% 10 11
0.8225806482.3%
 

Contributing tests

No tests hitting this source file were found.

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.watchlist.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collection;
25    import java.util.Collections;
26    import java.util.Date;
27    import java.util.HashMap;
28    import java.util.Iterator;
29    import java.util.List;
30    import java.util.Map;
31   
32    import javax.inject.Inject;
33    import javax.inject.Named;
34    import javax.inject.Provider;
35    import javax.inject.Singleton;
36    import javax.mail.Session;
37    import javax.mail.internet.MimeMessage;
38   
39    import org.apache.commons.collections4.iterators.IteratorIterable;
40    import org.xwiki.component.annotation.Component;
41    import org.xwiki.mail.MailListener;
42    import org.xwiki.mail.MailSender;
43    import org.xwiki.mail.MailSenderConfiguration;
44    import org.xwiki.mail.MimeMessageFactory;
45    import org.xwiki.mail.SessionFactory;
46    import org.xwiki.model.reference.DocumentReference;
47    import org.xwiki.model.reference.DocumentReferenceResolver;
48    import org.xwiki.model.reference.WikiReference;
49    import org.xwiki.watchlist.internal.api.WatchListEvent;
50    import org.xwiki.watchlist.internal.api.WatchListException;
51    import org.xwiki.watchlist.internal.api.WatchListNotifier;
52    import org.xwiki.watchlist.internal.notification.EventsAndSubscribersSource;
53    import org.xwiki.watchlist.internal.notification.WatchListEventMimeMessageFactory;
54   
55    import com.xpn.xwiki.XWikiContext;
56    import com.xpn.xwiki.XWikiException;
57    import com.xpn.xwiki.api.Attachment;
58   
59    /**
60    * Default implementation for {@link WatchListNotifier}. The current implementation offers email notifications only.
61    *
62    * @version $Id: db4738c241822e7bad3c2d41a954b9c2e920c81e $
63    */
64    @Component
65    @Singleton
 
66    public class DefaultWatchListNotifier implements WatchListNotifier
67    {
68    /**
69    * Previous fire time velocity context variable.
70    */
71    public static final String PREVIOUS_FIRE_TIME_VARIABLE = "previousFireTime";
72   
73    /**
74    * Wiki page which contains the default watchlist email template.
75    */
76    public static final String DEFAULT_EMAIL_TEMPLATE = "XWiki.WatchListMessage";
77   
78    /**
79    * Extra attachments to use when rendering the message template.
80    */
81    public static final String TEMPLATE_ATTACHMENTS = "attachments";
82   
83    /**
84    * XWiki User Class.
85    */
86    public static final String XWIKI_USER_CLASS = "XWiki.XWikiUsers";
87   
88    /**
89    * XWiki User Class email property.
90    */
91    public static final String XWIKI_USER_CLASS_EMAIL_PROP = "email";
92   
93    /**
94    * Context provider.
95    */
96    @Inject
97    private Provider<XWikiContext> contextProvider;
98   
99    /**
100    * Email service configuration.
101    */
102    @Inject
103    private MailSenderConfiguration mailConfiguration;
104   
105    @Inject
106    @Named(WatchListEventMimeMessageFactory.FACTORY_ID)
107    private MimeMessageFactory<Iterator<MimeMessage>> messageFactory;
108   
109    @Inject
110    private MailSender mailSender;
111   
112    @Inject
113    @Named("database")
114    private Provider<MailListener> mailListenerProvider;
115   
116    @Inject
117    private SessionFactory sessionFactory;
118   
119    @Inject
120    @Named("explicit")
121    private DocumentReferenceResolver<String> explicitDocumentReferenceResolver;
122   
 
123  0 toggle @Override
124    @Deprecated
125    public void sendNotification(String subscriber, List<WatchListEvent> events, String templateDocument,
126    Date previousFireTime) throws XWikiException
127    {
128  0 Map<String, Object> notificationData = new HashMap<>();
129  0 notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateDocument);
130  0 notificationData.put(PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
131   
132  0 try {
133  0 this.sendNotification(Arrays.asList(subscriber), events, notificationData);
134    } catch (WatchListException e) {
135  0 throw new XWikiException("", e);
136    }
137    }
138   
 
139  30 toggle @Override
140    public void sendNotification(Collection<String> subscribers, List<WatchListEvent> events,
141    Map<String, Object> notificationData) throws WatchListException
142    {
143  30 try {
144    // FIXME: Temporary, until we move to all references.
145  30 List<DocumentReference> subscriberReferences = getSubscriberReferences(subscribers);
146   
147    // Source
148  30 Map<String, Object> source = new HashMap<>();
149  30 source.put(EventsAndSubscribersSource.SUBSCRIBERS_PARAMETER, subscriberReferences);
150  30 source.put(EventsAndSubscribersSource.EVENTS_PARAMETER, events);
151   
152    // Parameters
153  30 Map<String, Object> parameters = new HashMap<>();
154  30 parameters.put(WatchListEventMimeMessageFactory.HINT_PARAMETER, "template");
155  30 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER,
156    notificationData.get(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER));
157  30 parameters.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER,
158    notificationData.get(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER));
159  30 parameters.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER,
160    notificationData.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER));
161  30 Map<String, Object> templateFactoryParameters = getTemplateFactoryParameters(notificationData);
162  30 parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, templateFactoryParameters);
163   
164    // Create the message iterator and the other mail sender parameters.
165  30 Iterator<MimeMessage> messageIterator = messageFactory.createMessage(source, parameters);
166  30 Session session = this.sessionFactory.create(Collections.<String, String>emptyMap());
167  30 MailListener mailListener = mailListenerProvider.get();
168   
169    // Pass it to the message sender to send it asynchronously.
170    // FIXME: !? There must be a better way instead of using IteratorIterable.
171  30 mailSender.sendAsynchronously(new IteratorIterable<MimeMessage>(messageIterator), session, mailListener);
172    } catch (Exception e) {
173  0 throw new WatchListException(String.format("Failed to send notification to subscribers [%s]", subscribers),
174    e);
175    }
176    }
177   
 
178  30 toggle private List<DocumentReference> getSubscriberReferences(Collection<String> subscribers)
179    {
180  30 List<DocumentReference> result = new ArrayList<>();
181   
182  30 XWikiContext context = contextProvider.get();
183  30 WikiReference currentWikiReference = new WikiReference(context.getWikiId());
184   
185  30 for (String subscriber : subscribers) {
186  30 DocumentReference subscriberReference =
187    explicitDocumentReferenceResolver.resolve(subscriber, currentWikiReference);
188  30 result.add(subscriberReference);
189    }
190  30 return result;
191    }
192   
 
193  30 toggle private Map<String, Object> getTemplateFactoryParameters(Map<String, Object> notificationData)
194    {
195  30 Map<String, Object> parameters = new HashMap<String, Object>();
196   
197  30 XWikiContext context = contextProvider.get();
198   
199    // Prepare email template (wiki page) context
200  30 Map<String, Object> velocityVariables = new HashMap<>();
201  30 Date previousFireTime = (Date) notificationData.get(PREVIOUS_FIRE_TIME_VARIABLE);
202  30 if (previousFireTime != null) {
203  1 velocityVariables.put(PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
204    }
205    // Note: The remaining bindings / variables that are context dependent will be updated for each subscriber by
206    // the iterator, since they are different for each subscriber.
207    // Add to parameters
208  30 parameters.put("velocityVariables", velocityVariables);
209   
210    // Get the wiki's default language (default en).
211  30 String language = context.getWiki().getXWikiPreference("default_language", "en", context);
212  30 parameters.put("language", language);
213   
214    // Add the template document's attachments to the email.
215  30 parameters.put("includeTemplateAttachments", true);
216  30 List<Attachment> attachments = (List<Attachment>) notificationData.get(TEMPLATE_ATTACHMENTS);
217  30 if (attachments != null) {
218  0 parameters.put(TEMPLATE_ATTACHMENTS, attachments);
219    }
220   
221    // Set the mail's type to "watchlist".
222  30 parameters.put("type", "watchlist");
223   
224    // Get from email address from the configuration (default : mailer@xwiki.localdomain.com)
225  30 String from = getFromAddress();
226  30 parameters.put("from", from);
227   
228  30 return parameters;
229    }
230   
 
231  30 toggle private String getFromAddress()
232    {
233    // Get from email address from the configuration (default : mailer@xwiki.localdomain.com)
234  30 String from = mailConfiguration.getFromAddress();
235  30 if (from == null) {
236  30 from = "mailer@xwiki.localdomain.com";
237    }
238  30 return from;
239    }
240    }