1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.mail; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
|
24 |
|
import javax.mail.MessagingException; |
25 |
|
import javax.mail.Session; |
26 |
|
import javax.mail.internet.InternetAddress; |
27 |
|
import javax.mail.internet.MimeMessage; |
28 |
|
|
29 |
|
import org.bouncycastle.crypto.digests.SHA1Digest; |
30 |
|
import org.bouncycastle.util.encoders.Base64; |
31 |
|
|
32 |
|
|
33 |
|
@link |
34 |
|
|
35 |
|
@version |
36 |
|
@since |
37 |
|
|
|
|
| 95.9% |
Uncovered Elements: 3 (74) |
Complexity: 27 |
Complexity Density: 0.57 |
|
38 |
|
public class ExtendedMimeMessage extends MimeMessage |
39 |
|
{ |
40 |
|
private static final ThreadLocal<SHA1Digest> SHA1_DIGEST = new ThreadLocal<SHA1Digest>(); |
41 |
|
|
42 |
|
private static final String MESSAGE_ID_HEADER = "Message-ID"; |
43 |
|
private static final String TO_HEADER = "To"; |
44 |
|
private static final String XMAIL_TYPE_HEADER = "X-MailType"; |
45 |
|
|
46 |
|
private String uniqueMessageId; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
45 |
public ExtendedMimeMessage()... |
55 |
|
{ |
56 |
45 |
super((Session) null); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@param |
64 |
|
@throws |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
21 |
public ExtendedMimeMessage(Session session, InputStream is) throws MessagingException... |
67 |
|
{ |
68 |
21 |
super(session, is); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
@param@link |
73 |
|
@throws@link |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
23 |
public ExtendedMimeMessage(MimeMessage source) throws MessagingException... |
76 |
|
{ |
77 |
23 |
super(source); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
@link@link |
82 |
|
|
83 |
|
@param@link |
84 |
|
@return@link |
85 |
|
@throws |
86 |
|
|
87 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
88 |
31 |
public static ExtendedMimeMessage wrap(MimeMessage message)... |
89 |
|
{ |
90 |
31 |
if (message instanceof ExtendedMimeMessage) { |
91 |
18 |
return (ExtendedMimeMessage) message; |
92 |
|
} else { |
93 |
13 |
try { |
94 |
13 |
return new ExtendedMimeMessage(message); |
95 |
|
} catch (MessagingException e) { |
96 |
|
|
97 |
0 |
throw new RuntimeException("Unexpected exception while wrapping a MimeMessage into an extended one", e); |
98 |
|
} |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
@return |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
8 |
public boolean isEmpty()... |
106 |
|
{ |
107 |
8 |
return this.dh == null; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@param |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 2 |
Complexity Density: 1 |
|
116 |
18 |
public void setType(String mailType)... |
117 |
|
{ |
118 |
18 |
try { |
119 |
18 |
addHeader(XMAIL_TYPE_HEADER, mailType); |
120 |
|
} catch (MessagingException e) { |
121 |
|
|
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
@link |
127 |
|
|
128 |
|
@return |
129 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
130 |
45 |
public String getType()... |
131 |
|
{ |
132 |
45 |
try { |
133 |
45 |
return getHeader(XMAIL_TYPE_HEADER, null); |
134 |
|
} catch (MessagingException e) { |
135 |
|
|
136 |
0 |
return null; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
@param |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
145 |
5 |
public void setMessageId(String messageId)... |
146 |
|
{ |
147 |
5 |
try { |
148 |
5 |
ensureSaved(); |
149 |
5 |
setHeader(MESSAGE_ID_HEADER, messageId); |
150 |
|
} catch (MessagingException e) { |
151 |
|
|
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@return |
159 |
|
@throws |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
161 |
25 |
public boolean ensureSaved() throws MessagingException... |
162 |
|
{ |
163 |
25 |
if (!this.saved) { |
164 |
4 |
this.saveChanges(); |
165 |
4 |
return true; |
166 |
|
} |
167 |
21 |
return false; |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 4 |
Complexity Density: 1.33 |
|
170 |
145 |
@Override... |
171 |
|
public void setHeader(String name, String value) throws MessagingException |
172 |
|
{ |
173 |
145 |
if (uniqueMessageId != null && MESSAGE_ID_HEADER.equals(name) || TO_HEADER.equals(name)) { |
174 |
|
|
175 |
28 |
uniqueMessageId = null; |
176 |
|
} |
177 |
145 |
super.setHeader(name, value); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
@return |
186 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
187 |
114 |
public String getUniqueMessageId()... |
188 |
|
{ |
189 |
114 |
if (uniqueMessageId == null) { |
190 |
59 |
try { |
191 |
59 |
StringBuilder sb = new StringBuilder(getNotNullMessageId()); |
192 |
59 |
String recipients = InternetAddress.toString(getAllRecipients()); |
193 |
59 |
if (recipients != null) { |
194 |
38 |
sb.append(':').append(recipients); |
195 |
|
} |
196 |
59 |
uniqueMessageId = digest(sb.toString()); |
197 |
|
} catch (MessagingException e) { |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
0 |
throw new RuntimeException("Unexpected exception while computing a unique id for a MimeMessage", e); |
203 |
|
} |
204 |
|
} |
205 |
114 |
return uniqueMessageId; |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
208 |
59 |
private String getNotNullMessageId() throws MessagingException... |
209 |
|
{ |
210 |
59 |
String messageId = getMessageID(); |
211 |
59 |
if (messageId == null) { |
212 |
8 |
saveChanges(); |
213 |
8 |
messageId = getMessageID(); |
214 |
|
} |
215 |
59 |
return messageId; |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
218 |
59 |
private String digest(String data)... |
219 |
|
{ |
220 |
59 |
SHA1Digest digest = SHA1_DIGEST.get(); |
221 |
59 |
if (digest == null) { |
222 |
21 |
digest = new SHA1Digest(); |
223 |
21 |
SHA1_DIGEST.set(new SHA1Digest()); |
224 |
|
} |
225 |
59 |
byte[] bytes = data.getBytes(); |
226 |
59 |
digest.update(bytes, 0, bytes.length); |
227 |
59 |
byte[] dig = new byte[digest.getDigestSize()]; |
228 |
59 |
digest.doFinal(dig, 0); |
229 |
59 |
return Base64.toBase64String(dig); |
230 |
|
} |
231 |
|
} |