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.io.IOException; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.mail.Address; |
28 |
|
import javax.mail.BodyPart; |
29 |
|
import javax.mail.Message; |
30 |
|
import javax.mail.MessagingException; |
31 |
|
import javax.mail.Multipart; |
32 |
|
import javax.mail.internet.MimeMessage; |
33 |
|
import javax.mail.internet.MimeMultipart; |
34 |
|
|
35 |
|
import org.xwiki.component.manager.ComponentLookupException; |
36 |
|
import org.xwiki.component.manager.ComponentManager; |
37 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
38 |
|
import org.xwiki.context.Execution; |
39 |
|
import org.xwiki.mail.ExtendedMimeMessage; |
40 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
41 |
|
|
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
|
|
| 80.5% |
Uncovered Elements: 15 (77) |
Complexity: 25 |
Complexity Density: 0.43 |
|
48 |
|
public class ScriptMimeMessage extends ExtendedMimeMessage |
49 |
|
{ |
50 |
|
private ComponentManager componentManager; |
51 |
|
|
52 |
|
private Execution execution; |
53 |
|
|
54 |
|
|
55 |
|
@param |
56 |
|
@param@link |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
61 |
2 |
ScriptMimeMessage(MimeMessage sourceMessage, Execution execution, ComponentManager componentManager)... |
62 |
|
throws MessagingException |
63 |
|
{ |
64 |
2 |
super(sourceMessage); |
65 |
2 |
this.execution = execution; |
66 |
2 |
this.componentManager = componentManager; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
@param |
71 |
|
@param@link |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
76 |
10 |
ScriptMimeMessage(Execution execution, ComponentManager componentManager)... |
77 |
|
{ |
78 |
10 |
super(); |
79 |
10 |
this.execution = execution; |
80 |
10 |
this.componentManager = componentManager; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@return |
87 |
|
|
88 |
|
@param |
89 |
|
@param |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
5 |
public BodyPart addPart(String mimeType, Object content)... |
92 |
|
{ |
93 |
5 |
return addPart(mimeType, content, Collections.<String, Object>emptyMap()); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@return |
100 |
|
|
101 |
|
@param |
102 |
|
@param |
103 |
|
@param |
104 |
|
|
105 |
|
|
106 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
|
107 |
6 |
public BodyPart addPart(String mimeType, Object content, Map<String, Object> parameters)... |
108 |
|
{ |
109 |
6 |
BodyPart bodyPart; |
110 |
|
|
111 |
6 |
try { |
112 |
6 |
MimeBodyPartFactory factory = getBodyPartFactory(mimeType, content.getClass()); |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
6 |
Map<String, Object> enhancedParameters = new HashMap<>(); |
117 |
6 |
enhancedParameters.put("mimetype", mimeType); |
118 |
6 |
enhancedParameters.putAll(parameters); |
119 |
|
|
120 |
6 |
Multipart multipart = getMultipart(); |
121 |
6 |
bodyPart = factory.create(content, enhancedParameters); |
122 |
6 |
multipart.addBodyPart(bodyPart); |
123 |
|
} catch (Exception e) { |
124 |
|
|
125 |
0 |
setError(e); |
126 |
0 |
bodyPart = null; |
127 |
|
} |
128 |
|
|
129 |
6 |
return bodyPart; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
135 |
6 |
@Override... |
136 |
|
public void setSubject(String subject) |
137 |
|
{ |
138 |
6 |
try { |
139 |
6 |
super.setSubject(subject); |
140 |
|
} catch (Exception e) { |
141 |
0 |
setError(e); |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
148 |
7 |
@Override... |
149 |
|
public void setFrom(Address address) |
150 |
|
{ |
151 |
7 |
try { |
152 |
7 |
super.setFrom(address); |
153 |
|
} catch (Exception e) { |
154 |
0 |
setError(e); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@param |
161 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
162 |
8 |
@Override... |
163 |
|
public void addRecipients(Message.RecipientType type, Address[] addresses) |
164 |
|
{ |
165 |
8 |
try { |
166 |
8 |
super.addRecipients(type, addresses); |
167 |
|
} catch (Exception e) { |
168 |
0 |
setError(e); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
@param |
175 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
176 |
1 |
@Override... |
177 |
|
public void addRecipient(Message.RecipientType type, Address address) |
178 |
|
{ |
179 |
1 |
try { |
180 |
1 |
super.addRecipient(type, address); |
181 |
|
} catch (Exception e) { |
182 |
0 |
setError(e); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@param |
190 |
|
@param |
191 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
192 |
3 |
@Override... |
193 |
|
public void addHeader(String name, String value) |
194 |
|
{ |
195 |
3 |
try { |
196 |
3 |
super.addHeader(name, value); |
197 |
|
} catch (Exception e) { |
198 |
0 |
setError(e); |
199 |
|
} |
200 |
|
} |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
0 |
private void setError(Exception e)... |
203 |
|
{ |
204 |
0 |
this.execution.getContext().setProperty(MailSenderScriptService.ERROR_KEY, e); |
205 |
|
} |
206 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 4 |
Complexity Density: 0.44 |
|
207 |
6 |
private MimeBodyPartFactory getBodyPartFactory(String mimeType, Class contentClass) throws MessagingException... |
208 |
|
{ |
209 |
6 |
MimeBodyPartFactory factory; |
210 |
6 |
try { |
211 |
|
|
212 |
6 |
factory = getSpecificBodyPartFactory(mimeType, contentClass); |
213 |
|
} catch (ComponentLookupException e) { |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
6 |
if (String.class.isAssignableFrom(contentClass)) { |
218 |
6 |
try { |
219 |
6 |
factory = this.componentManager.getInstance( |
220 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class)); |
221 |
|
} catch (ComponentLookupException eee) { |
222 |
|
|
223 |
|
|
224 |
0 |
throw new MessagingException(String.format( |
225 |
|
"Failed to find default Mime Body Part Factory for mime type [%s] and Content type [%s]", |
226 |
|
mimeType, contentClass.getName()), e); |
227 |
|
} |
228 |
|
} else { |
229 |
0 |
throw new MessagingException(String.format( |
230 |
|
"Failed to a Mime Body Part Factory matching the mime type [%s] and the Content type [%s]", |
231 |
|
mimeType, contentClass.getName()), e); |
232 |
|
} |
233 |
|
} |
234 |
6 |
return factory; |
235 |
|
} |
236 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
237 |
6 |
private MimeBodyPartFactory getSpecificBodyPartFactory(String mimeType, Class contentClass)... |
238 |
|
throws ComponentLookupException |
239 |
|
{ |
240 |
6 |
MimeBodyPartFactory factory; |
241 |
6 |
try { |
242 |
|
|
243 |
6 |
factory = this.componentManager.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, |
244 |
|
contentClass), String.format("%s/secure", mimeType)); |
245 |
|
} catch (ComponentLookupException e) { |
246 |
|
|
247 |
6 |
factory = this.componentManager.getInstance( |
248 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, contentClass), mimeType); |
249 |
|
} |
250 |
0 |
return factory; |
251 |
|
} |
252 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
253 |
6 |
private Multipart getMultipart() throws MessagingException, IOException... |
254 |
|
{ |
255 |
6 |
Multipart multipart; |
256 |
6 |
if (isEmpty()) { |
257 |
5 |
multipart = new MimeMultipart("mixed"); |
258 |
5 |
setContent(multipart); |
259 |
|
} else { |
260 |
1 |
Object contentObject = getContent(); |
261 |
1 |
if (contentObject instanceof Multipart) { |
262 |
1 |
multipart = (Multipart) contentObject; |
263 |
|
} else { |
264 |
0 |
throw new MessagingException(String.format("Unknown mail content type [%s]: [%s]", |
265 |
|
contentObject.getClass().getName(), contentObject)); |
266 |
|
} |
267 |
|
} |
268 |
6 |
return multipart; |
269 |
|
} |
270 |
|
} |