1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.attachment

File AttachmentMimeBodyPartFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
23
3
1
127
75
6
0.26
7.67
3
2

Classes

Class Line # Actions
AttachmentMimeBodyPartFactory 55 23 0% 6 2
0.928571492.9%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Creates an attachment Body Part from an {@link Attachment} object. This will be added to a Multi Part message.
48    *
49    * @version $Id: a5e7c9f71fa884fd30653c0b2e45a08b4792119b $
50    * @since 6.1M2
51    */
52    @Component
53    @Named("xwiki/attachment")
54    @Singleton
 
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    * Provides access to the logger.
64    */
65    @Inject
66    private Logger logger;
67   
68    private File temporaryDirectory;
69   
 
70  9 toggle @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   
 
77  25 toggle @Override
78    public MimeBodyPart create(Attachment attachment, Map<String, Object> parameters) throws MessagingException
79    {
80    // Create the attachment part of the email
81  25 MimeBodyPart attachmentPart = new MimeBodyPart();
82   
83    // Save the attachment to a temporary file on the file system and wrap it in a Java Mail Data Source.
84  25 DataSource source = createTemporaryAttachmentDataSource(attachment);
85  24 attachmentPart.setDataHandler(new DataHandler(source));
86   
87  24 attachmentPart.setHeader("Content-Type", attachment.getMimeType());
88   
89    // Add a content-id so that we can uniquely reference this attachment. This is used for example to
90    // display the attachment inline in some mail HTML content.
91    // Note: According to http://tools.ietf.org/html/rfc2392 the id must be enclosed in angle brackets.
92  24 attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">");
93   
94  24 attachmentPart.setFileName(attachment.getFilename());
95   
96    // Handle headers passed as parameter
97  24 addHeaders(attachmentPart, parameters);
98   
99  24 return attachmentPart;
100    }
101   
 
102  25 toggle 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    // Only an error at closing, we continue
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    }