1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.mail.templates; |
35 |
|
|
36 |
|
import info.magnolia.module.mail.MailException; |
37 |
|
import info.magnolia.module.mail.MailTemplate; |
38 |
|
|
39 |
|
import java.net.URL; |
40 |
|
import java.util.List; |
41 |
|
|
42 |
|
import javax.activation.DataHandler; |
43 |
|
import javax.activation.DataSource; |
44 |
|
import javax.activation.FileDataSource; |
45 |
|
import javax.activation.URLDataSource; |
46 |
|
import javax.mail.BodyPart; |
47 |
|
import javax.mail.internet.MimeBodyPart; |
48 |
|
import javax.mail.internet.MimeMultipart; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@author |
62 |
|
|
|
|
| 88.9% |
Uncovered Elements: 7 (63) |
Complexity: 14 |
Complexity Density: 0.33 |
|
63 |
|
public abstract class MgnlMultipartEmail extends MgnlEmail { |
64 |
|
|
65 |
|
private static final String CONTENT_ID = "Content-ID"; |
66 |
|
|
67 |
|
private static final String RELATED = "related"; |
68 |
|
|
69 |
|
private static final String MIXED = "mixed"; |
70 |
|
|
71 |
|
protected MimeMultipart multipartMixed; |
72 |
|
|
73 |
|
protected MimeMultipart multipartRelated; |
74 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
75 |
|
public boolean isMultipart() {... |
76 |
|
|
77 |
|
return (this.multipartMixed != null); |
78 |
|
} |
79 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
80 |
|
public MimeMultipart getMailMultipart() {... |
81 |
|
|
82 |
|
return this.multipartMixed; |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
19 |
public MgnlMultipartEmail(MailTemplate template) {... |
86 |
19 |
super(template); |
87 |
|
} |
88 |
|
|
|
|
| 89.7% |
Uncovered Elements: 3 (29) |
Complexity: 6 |
Complexity Density: 0.29 |
|
89 |
12 |
@Override... |
90 |
|
public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException { |
91 |
|
|
92 |
12 |
try { |
93 |
12 |
if (!isMultipart()) { |
94 |
7 |
turnOnMultipart(); |
95 |
|
} |
96 |
12 |
MimeBodyPart attachmentBodyPart = new MimeBodyPart(); |
97 |
12 |
String key = attachment.getName(); |
98 |
12 |
log.info("Found new attachment with name :" + key); |
99 |
|
|
100 |
|
|
101 |
12 |
URL url = attachment.getUrl(); |
102 |
12 |
String name = attachment.getFileName(); |
103 |
12 |
String contentType = attachment.getContentType(); |
104 |
|
|
105 |
|
|
106 |
12 |
attachmentBodyPart.setDisposition(attachment.getDisposition() + "; filename=\"" + name + "\""); |
107 |
|
|
108 |
|
|
109 |
12 |
DataSource fds = url.getProtocol().startsWith("file:") |
110 |
|
? new FileDataSource(url.getFile()) |
111 |
|
: new URLDataSource(url); |
112 |
|
|
113 |
12 |
attachmentBodyPart.setDataHandler(new DataHandler(fds)); |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
12 |
if (!key.startsWith("<")) { |
118 |
12 |
key = "<" + key + ">"; |
119 |
|
} |
120 |
12 |
attachmentBodyPart.setHeader(CONTENT_ID, key); |
121 |
|
|
122 |
|
|
123 |
12 |
attachmentBodyPart.setHeader(CONTENT_TYPE, contentType + "; name=\"" + name + "\""); |
124 |
|
|
125 |
|
|
126 |
12 |
if (attachment.getMimeMultipart().equals(RELATED)) { |
127 |
4 |
this.multipartRelated.addBodyPart(attachmentBodyPart); |
128 |
|
} else { |
129 |
8 |
this.multipartMixed.addBodyPart(attachmentBodyPart); |
130 |
|
} |
131 |
|
|
132 |
12 |
return attachmentBodyPart; |
133 |
|
} catch (Exception e) { |
134 |
0 |
throw new MailException(e.getMessage(), e); |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 85.7% |
Uncovered Elements: 3 (21) |
Complexity: 5 |
Complexity Density: 0.33 |
|
138 |
7 |
private void turnOnMultipart() {... |
139 |
|
|
140 |
7 |
try { |
141 |
7 |
if (this.multipartMixed == null) { |
142 |
7 |
this.multipartMixed = new MimeMultipart(MIXED); |
143 |
7 |
this.multipartRelated = new MimeMultipart(RELATED); |
144 |
7 |
MimeBodyPart relatedBodyPart = new MimeBodyPart(); |
145 |
7 |
relatedBodyPart.setContent(multipartRelated); |
146 |
7 |
this.multipartMixed.addBodyPart(relatedBodyPart); |
147 |
|
} |
148 |
5 |
if (this.getContent() != null) { |
149 |
|
|
150 |
5 |
Object o = this.getContent(); |
151 |
5 |
if (o instanceof String) { |
152 |
5 |
BodyPart messageBodyPart = new MimeBodyPart(); |
153 |
5 |
messageBodyPart.setContent(o, getContentType()); |
154 |
5 |
this.multipartRelated.addBodyPart(messageBodyPart, 0); |
155 |
|
} |
156 |
|
} |
157 |
5 |
this.setContent(this.multipartMixed); |
158 |
|
} catch (Exception e) { |
159 |
2 |
log.info(e.getMessage()); |
160 |
|
} |
161 |
|
} |
162 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
163 |
13 |
@Override... |
164 |
|
public void setBody(String body) throws Exception { |
165 |
|
|
166 |
13 |
this.setContent(body, getContentType()); |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
13 |
this.multipartMixed = null; |
171 |
13 |
this.multipartRelated = null; |
172 |
|
|
173 |
|
|
174 |
13 |
final List<MailAttachment> attachments = super.getTemplate().getAttachments(); |
175 |
13 |
if (attachments != null) { |
176 |
13 |
setAttachments(attachments); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
} |