1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.mail.script; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
import javax.mail.Session; |
33 |
|
import javax.mail.internet.MimeMessage; |
34 |
|
|
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.component.manager.ComponentLookupException; |
37 |
|
import org.xwiki.mail.MailContentStore; |
38 |
|
import org.xwiki.mail.MailListener; |
39 |
|
import org.xwiki.mail.MailStatus; |
40 |
|
import org.xwiki.mail.MailStatusStore; |
41 |
|
import org.xwiki.mail.MailStorageConfiguration; |
42 |
|
import org.xwiki.mail.MailStoreException; |
43 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
44 |
|
import org.xwiki.security.authorization.Right; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWikiContext; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named("mailstorage") |
56 |
|
@Singleton |
|
|
| 57.4% |
Uncovered Elements: 29 (68) |
Complexity: 20 |
Complexity Density: 0.4 |
|
57 |
|
public class MailStorageScriptService extends AbstractMailScriptService |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String ERROR_KEY = "scriptservice.mailstorage.error"; |
63 |
|
|
64 |
|
private static final String SESSION_BATCHID_KEY = "xwiki.batchId"; |
65 |
|
|
66 |
|
@Inject |
67 |
|
@Named("filesystem") |
68 |
|
private MailContentStore mailContentStore; |
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("database") |
72 |
|
private MailStatusStore mailStatusStore; |
73 |
|
|
74 |
|
@Inject |
75 |
|
private Provider<XWikiContext> xwikiContextProvider; |
76 |
|
|
77 |
|
@Inject |
78 |
|
private ContextualAuthorizationManager authorizationManager; |
79 |
|
|
80 |
|
@Inject |
81 |
|
private MailStorageConfiguration storageConfiguration; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
@param |
88 |
|
@return |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.21 |
|
90 |
3 |
public ScriptMailResult resend(String batchId, String uniqueMessageId)... |
91 |
|
{ |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
3 |
MailListener listener; |
96 |
3 |
try { |
97 |
3 |
listener = this.componentManagerProvider.get().getInstance(MailListener.class, "database"); |
98 |
|
} catch (ComponentLookupException e) { |
99 |
|
|
100 |
1 |
setError(e); |
101 |
|
|
102 |
1 |
return null; |
103 |
|
} |
104 |
|
|
105 |
2 |
MimeMessage message; |
106 |
2 |
try { |
107 |
|
|
108 |
2 |
Session session = this.sessionFactory.create(Collections.singletonMap(SESSION_BATCHID_KEY, batchId)); |
109 |
|
|
110 |
2 |
message = loadMessage(session, batchId, uniqueMessageId); |
111 |
|
|
112 |
1 |
ScriptMailResult scriptMailResult = new ScriptMailResult(this.mailSender.sendAsynchronously( |
113 |
|
Arrays.asList(message), session, listener), listener.getMailStatusResult()); |
114 |
|
|
115 |
|
|
116 |
1 |
scriptMailResult.getStatusResult().waitTillProcessed(Long.MAX_VALUE); |
117 |
|
|
118 |
1 |
return scriptMailResult; |
119 |
|
} catch (MailStoreException e) { |
120 |
|
|
121 |
1 |
setError(e); |
122 |
1 |
return null; |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
@param |
130 |
|
@return@link |
131 |
|
@since |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
133 |
0 |
public MailStatus load(String uniqueMessageId)... |
134 |
|
{ |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
0 |
try { |
139 |
0 |
return this.mailStatusStore.load(uniqueMessageId); |
140 |
|
} catch (MailStoreException e) { |
141 |
|
|
142 |
0 |
setError(e); |
143 |
0 |
return null; |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
@param |
151 |
|
@param |
152 |
|
@param |
153 |
|
@param |
154 |
|
@param |
155 |
|
@return@link |
156 |
|
@since |
157 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
158 |
15 |
public List<MailStatus> load(Map<String, Object> filterMap, int offset, int count, String sortField,... |
159 |
|
boolean sortAscending) |
160 |
|
{ |
161 |
|
|
162 |
15 |
if (this.authorizationManager.hasAccess(Right.ADMIN)) { |
163 |
14 |
try { |
164 |
14 |
return this.mailStatusStore.load(normalizeFilterMap(filterMap), offset, count, |
165 |
|
sortField, sortAscending); |
166 |
|
} catch (MailStoreException e) { |
167 |
|
|
168 |
0 |
setError(e); |
169 |
0 |
return null; |
170 |
|
} |
171 |
|
} else { |
172 |
|
|
173 |
1 |
setError(new MailStoreException("You need Admin rights to load mail statuses")); |
174 |
1 |
return null; |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
@param |
182 |
|
@return |
183 |
|
|
|
|
| 44.4% |
Uncovered Elements: 5 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
184 |
14 |
public long count(Map<String, Object> filterMap)... |
185 |
|
{ |
186 |
|
|
187 |
14 |
if (this.authorizationManager.hasAccess(Right.ADMIN)) { |
188 |
14 |
try { |
189 |
14 |
return this.mailStatusStore.count(normalizeFilterMap(filterMap)); |
190 |
|
} catch (MailStoreException e) { |
191 |
|
|
192 |
0 |
setError(e); |
193 |
0 |
return 0; |
194 |
|
} |
195 |
|
} else { |
196 |
|
|
197 |
0 |
setError(new MailStoreException("You need Admin rights to count mail statuses")); |
198 |
0 |
return 0; |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@param |
207 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
208 |
0 |
public void delete(String batchId)... |
209 |
|
{ |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
0 |
Map<String, Object> filterMap = Collections.<String, Object>singletonMap("batchId", batchId); |
214 |
0 |
List<MailStatus> statuses = load(filterMap, 0, 0, null, false); |
215 |
0 |
if (statuses != null) { |
216 |
0 |
for (MailStatus status : statuses) { |
217 |
0 |
delete(batchId, status.getMessageId()); |
218 |
|
} |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
@param |
226 |
|
@param |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
228 |
0 |
public void delete(String batchId, String uniqueMessageId)... |
229 |
|
{ |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
0 |
try { |
234 |
|
|
235 |
0 |
this.mailStatusStore.delete(uniqueMessageId, Collections.<String, Object>emptyMap()); |
236 |
|
|
237 |
0 |
this.mailContentStore.delete(batchId, uniqueMessageId); |
238 |
|
} catch (MailStoreException e) { |
239 |
|
|
240 |
0 |
setError(e); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
@return |
246 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
247 |
0 |
public MailStorageConfiguration getConfiguration()... |
248 |
|
{ |
249 |
0 |
return this.storageConfiguration; |
250 |
|
} |
251 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
252 |
28 |
private Map<String, Object> normalizeFilterMap(Map<String, Object> filterMap)... |
253 |
|
{ |
254 |
|
|
255 |
|
|
256 |
28 |
Map<String, Object> normalizedMap = new HashMap<>(filterMap); |
257 |
28 |
XWikiContext xwikiContext = this.xwikiContextProvider.get(); |
258 |
28 |
if (!xwikiContext.isMainWiki()) { |
259 |
0 |
normalizedMap.put("wiki", xwikiContext.getWikiId()); |
260 |
|
} |
261 |
28 |
return normalizedMap; |
262 |
|
} |
263 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
264 |
2 |
private MimeMessage loadMessage(Session session, String batchId, String mailId) throws MailStoreException... |
265 |
|
{ |
266 |
2 |
MimeMessage message = this.mailContentStore.load(session, batchId, mailId); |
267 |
1 |
return message; |
268 |
|
} |
269 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
270 |
6 |
@Override... |
271 |
|
protected String getErrorKey() |
272 |
|
{ |
273 |
6 |
return ERROR_KEY; |
274 |
|
} |
275 |
|
} |