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.Collections; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Provider; |
27 |
|
import javax.mail.MessagingException; |
28 |
|
import javax.mail.internet.MimeMessage; |
29 |
|
|
30 |
|
import org.xwiki.component.manager.ComponentLookupException; |
31 |
|
import org.xwiki.component.manager.ComponentManager; |
32 |
|
import org.xwiki.context.Execution; |
33 |
|
import org.xwiki.mail.MailListener; |
34 |
|
import org.xwiki.mail.MailSender; |
35 |
|
import org.xwiki.mail.MailSenderConfiguration; |
36 |
|
import org.xwiki.mail.SessionFactory; |
37 |
|
import org.xwiki.script.service.ScriptService; |
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 77.3% |
Uncovered Elements: 5 (22) |
Complexity: 8 |
Complexity Density: 0.5 |
|
43 |
|
public abstract class AbstractMailScriptService implements ScriptService |
44 |
|
{ |
45 |
|
@Inject |
46 |
|
protected MailSender mailSender; |
47 |
|
|
48 |
|
@Inject |
49 |
|
@Named("context") |
50 |
|
protected Provider<ComponentManager> componentManagerProvider; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@Inject |
56 |
|
protected Execution execution; |
57 |
|
|
58 |
|
@Inject |
59 |
|
protected SessionFactory sessionFactory; |
60 |
|
|
61 |
|
@Inject |
62 |
|
protected MailSenderConfiguration senderConfiguration; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@param |
68 |
|
@param@link |
69 |
|
@param |
70 |
|
@return |
71 |
|
|
|
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
72 |
6 |
protected ScriptMailResult sendAsynchronously(Iterable<? extends MimeMessage> messages, MailListener listener,... |
73 |
|
boolean checkPermissions) |
74 |
|
{ |
75 |
6 |
if (checkPermissions) |
76 |
|
{ |
77 |
6 |
try { |
78 |
6 |
checkPermissions(); |
79 |
|
} catch (MessagingException e) { |
80 |
|
|
81 |
0 |
setError(e); |
82 |
|
|
83 |
0 |
return null; |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
6 |
return new ScriptMailResult(this.mailSender.sendAsynchronously(messages, this.sessionFactory.create( |
90 |
|
Collections.<String, String>emptyMap()), listener), listener.getMailStatusResult()); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@throws |
97 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
98 |
6 |
private void checkPermissions() throws MessagingException... |
99 |
|
{ |
100 |
|
|
101 |
6 |
ScriptServicePermissionChecker checker; |
102 |
6 |
String hint = this.senderConfiguration.getScriptServicePermissionCheckerHint(); |
103 |
6 |
try { |
104 |
6 |
checker = this.componentManagerProvider.get().getInstance(ScriptServicePermissionChecker.class, hint); |
105 |
|
} catch (ComponentLookupException e) { |
106 |
|
|
107 |
|
|
108 |
0 |
throw new MessagingException(String.format("Failed to locate Permission Checker [%s]. " |
109 |
|
+ "The mail has not been sent.", hint), e); |
110 |
|
} |
111 |
|
|
112 |
6 |
try { |
113 |
6 |
checker.check(); |
114 |
|
} catch (MessagingException e) { |
115 |
0 |
throw new MessagingException(String.format("Not authorized by the Permission Checker [%s] to send mail! " |
116 |
|
+ "No mail has been sent.", hint), e); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
@return |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
9 |
public Exception getLastError()... |
132 |
|
{ |
133 |
9 |
return (Exception) this.execution.getContext().getProperty(getErrorKey()); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
@link |
138 |
|
|
139 |
|
@param |
140 |
|
@see |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
142 |
3 |
protected void setError(Exception e)... |
143 |
|
{ |
144 |
3 |
this.execution.getContext().setProperty(getErrorKey(), e); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
@return |
149 |
|
|
150 |
|
protected abstract String getErrorKey(); |
151 |
|
} |