1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.mail.internal.factory.attachment; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.activation.DataHandler; |
28 |
|
import javax.activation.DataSource; |
29 |
|
import javax.activation.FileDataSource; |
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Singleton; |
33 |
|
import javax.mail.MessagingException; |
34 |
|
import javax.mail.internet.MimeBodyPart; |
35 |
|
|
36 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
37 |
|
import org.slf4j.Logger; |
38 |
|
import org.xwiki.component.annotation.Component; |
39 |
|
import org.xwiki.component.phase.Initializable; |
40 |
|
import org.xwiki.component.phase.InitializationException; |
41 |
|
import org.xwiki.environment.Environment; |
42 |
|
import org.xwiki.mail.internal.factory.AbstractMimeBodyPartFactory; |
43 |
|
|
44 |
|
import com.xpn.xwiki.api.Attachment; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("xwiki/attachment") |
54 |
|
@Singleton |
|
|
| 92.9% |
Uncovered Elements: 2 (28) |
Complexity: 6 |
Complexity Density: 0.26 |
|
55 |
|
public class AttachmentMimeBodyPartFactory extends AbstractMimeBodyPartFactory<Attachment> implements Initializable |
56 |
|
{ |
57 |
|
private static final String HEADERS_PARAMETER_KEY = "headers"; |
58 |
|
|
59 |
|
@Inject |
60 |
|
private Environment environment; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@Inject |
66 |
|
private Logger logger; |
67 |
|
|
68 |
|
private File temporaryDirectory; |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
70 |
9 |
@Override... |
71 |
|
public void initialize() throws InitializationException |
72 |
|
{ |
73 |
9 |
this.temporaryDirectory = new File(this.environment.getTemporaryDirectory(), "mail"); |
74 |
9 |
this.temporaryDirectory.mkdirs(); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
77 |
25 |
@Override... |
78 |
|
public MimeBodyPart create(Attachment attachment, Map<String, Object> parameters) throws MessagingException |
79 |
|
{ |
80 |
|
|
81 |
25 |
MimeBodyPart attachmentPart = new MimeBodyPart(); |
82 |
|
|
83 |
|
|
84 |
25 |
DataSource source = createTemporaryAttachmentDataSource(attachment); |
85 |
24 |
attachmentPart.setDataHandler(new DataHandler(source)); |
86 |
|
|
87 |
24 |
attachmentPart.setHeader("Content-Type", attachment.getMimeType()); |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
24 |
attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">"); |
93 |
|
|
94 |
24 |
attachmentPart.setFileName(attachment.getFilename()); |
95 |
|
|
96 |
|
|
97 |
24 |
addHeaders(attachmentPart, parameters); |
98 |
|
|
99 |
24 |
return attachmentPart; |
100 |
|
} |
101 |
|
|
|
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
102 |
25 |
private DataSource createTemporaryAttachmentDataSource(Attachment attachment) throws MessagingException... |
103 |
|
{ |
104 |
25 |
File temporaryAttachmentFile; |
105 |
25 |
FileOutputStream fos = null; |
106 |
25 |
try { |
107 |
25 |
temporaryAttachmentFile = File.createTempFile("attachment", ".tmp", this.temporaryDirectory); |
108 |
25 |
temporaryAttachmentFile.deleteOnExit(); |
109 |
25 |
fos = new FileOutputStream(temporaryAttachmentFile); |
110 |
25 |
fos.write(attachment.getContent()); |
111 |
|
} catch (Exception e) { |
112 |
1 |
throw new MessagingException( |
113 |
|
String.format("Failed to save attachment [%s] to the file system", attachment.getFilename()), e); |
114 |
|
} finally { |
115 |
25 |
try { |
116 |
25 |
if (fos != null) { |
117 |
25 |
fos.close(); |
118 |
|
} |
119 |
|
} catch (IOException e) { |
120 |
|
|
121 |
0 |
this.logger.warn("Failed to close the temporary file attachment when sending an email. " |
122 |
|
+ "Root reason: [{}]", ExceptionUtils.getRootCauseMessage(e)); |
123 |
|
} |
124 |
|
} |
125 |
24 |
return new FileDataSource(temporaryAttachmentFile); |
126 |
|
} |
127 |
|
} |