1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
61 |
|
|
62 |
|
@version |
63 |
|
|
64 |
|
@Component |
65 |
|
@Singleton |
|
|
| 82.3% |
Uncovered Elements: 11 (62) |
Complexity: 10 |
Complexity Density: 0.2 |
|
66 |
|
public class DefaultWatchListNotifier implements WatchListNotifier |
67 |
|
{ |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public static final String PREVIOUS_FIRE_TIME_VARIABLE = "previousFireTime"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
public static final String DEFAULT_EMAIL_TEMPLATE = "XWiki.WatchListMessage"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
public static final String TEMPLATE_ATTACHMENTS = "attachments"; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
public static final String XWIKI_USER_CLASS = "XWiki.XWikiUsers"; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
public static final String XWIKI_USER_CLASS_EMAIL_PROP = "email"; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
private Provider<XWikiContext> contextProvider; |
98 |
|
|
99 |
|
|
100 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
123 |
0 |
@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 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 2 |
Complexity Density: 0.12 |
|
139 |
30 |
@Override... |
140 |
|
public void sendNotification(Collection<String> subscribers, List<WatchListEvent> events, |
141 |
|
Map<String, Object> notificationData) throws WatchListException |
142 |
|
{ |
143 |
30 |
try { |
144 |
|
|
145 |
30 |
List<DocumentReference> subscriberReferences = getSubscriberReferences(subscribers); |
146 |
|
|
147 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
170 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
178 |
30 |
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 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 3 |
Complexity Density: 0.18 |
|
193 |
30 |
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 |
|
|
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 |
|
|
206 |
|
|
207 |
|
|
208 |
30 |
parameters.put("velocityVariables", velocityVariables); |
209 |
|
|
210 |
|
|
211 |
30 |
String language = context.getWiki().getXWikiPreference("default_language", "en", context); |
212 |
30 |
parameters.put("language", language); |
213 |
|
|
214 |
|
|
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 |
|
|
222 |
30 |
parameters.put("type", "watchlist"); |
223 |
|
|
224 |
|
|
225 |
30 |
String from = getFromAddress(); |
226 |
30 |
parameters.put("from", from); |
227 |
|
|
228 |
30 |
return parameters; |
229 |
|
} |
230 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
231 |
30 |
private String getFromAddress()... |
232 |
|
{ |
233 |
|
|
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 |
|
} |