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

File FileSystemMailContentStore.java

 
saveMessageThrowsMailStoreExceptionWhenError: Failed to save message (id [ar1vm0Wca42E/dDn3dsH8ogs3/s=]...
loadMessageThrowsMailStoreExceptionWhenError: Failed to load message (id [ar1vm0Wca42E/dDn3dsH8ogs3/s=]...
 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
28
7
1
140
95
12
0.43
4
7
1.71

Classes

Class Line # Actions
FileSystemMailContentStore 51 28 0% 12 4
0.891891989.2%
 

Contributing tests

This file is covered by 13 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;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.FileOutputStream;
25    import java.io.InputStream;
26    import java.io.UnsupportedEncodingException;
27    import java.net.URLEncoder;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32    import javax.mail.Session;
33   
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.phase.Initializable;
36    import org.xwiki.component.phase.InitializationException;
37    import org.xwiki.environment.Environment;
38    import org.xwiki.mail.ExtendedMimeMessage;
39    import org.xwiki.mail.MailContentStore;
40    import org.xwiki.mail.MailStoreException;
41   
42    /**
43    * Stores mail content on the file system.
44    *
45    * @version $Id: 06179ade225b2b3ed36ad1ae3bb9ffe9ef9202aa $
46    * @since 6.4M3
47    */
48    @Component
49    @Named("filesystem")
50    @Singleton
 
51    public class FileSystemMailContentStore implements MailContentStore, Initializable
52    {
53    /**
54    * The subdirectory in the permanent directory where we store mails.
55    */
56    public static final String ROOT_DIRECTORY = "mails";
57   
58    private File rootDirectory;
59   
60    @Inject
61    private Environment environment;
62   
 
63  20 toggle @Override
64    public void initialize() throws InitializationException
65    {
66  20 rootDirectory = new File(this.environment.getPermanentDirectory(), ROOT_DIRECTORY);
67    }
68   
 
69  21 toggle @Override
70    public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException
71    {
72  21 String uniqueMessageId = message.getUniqueMessageId();
73  21 File messageFile = getMessageFile(batchId, uniqueMessageId);
74  21 try {
75    // Unsaved message may have their message-ID header to be modified during serialization.
76    // We ensure that the message was saved, and we save it if not saved yet, getting again the identifier
77    // to be sure we have the right ones.
78  21 if (message.ensureSaved()) {
79  1 uniqueMessageId = message.getUniqueMessageId();
80  1 messageFile = getMessageFile(batchId, uniqueMessageId);
81    }
82  21 Test failure here message.writeTo(new FileOutputStream(messageFile));
83    } catch (Exception e) {
84  0 Test failure here throw new MailStoreException(String.format(
85    "Failed to save message (id [%s], batch id [%s]) into file [%s]",
86    uniqueMessageId, batchId, messageFile), e);
87    }
88    }
89   
 
90  19 toggle @Override
91    public ExtendedMimeMessage load(Session session, String batchId, String uniqueMessageId) throws MailStoreException
92    {
93  19 File messageFile = null;
94  19 try {
95  19 messageFile = getMessageFile(batchId, uniqueMessageId);
96  19 Test failure here InputStream is = new FileInputStream(messageFile);
97  18 return new ExtendedMimeMessage(session, is);
98    } catch (Exception e) {
99  0 Test failure here throw new MailStoreException(String.format(
100    "Failed to load message (id [%s], batch id [%s]) from file [%s]",
101    uniqueMessageId, batchId, messageFile), e);
102    }
103    }
104   
 
105  7 toggle @Override
106    public void delete(String batchId, String uniqueMessageId) throws MailStoreException
107    {
108  7 File messageFile = null;
109  7 try {
110  7 messageFile = getMessageFile(batchId, uniqueMessageId);
111  7 messageFile.delete();
112    // Also remove the directory. Note that it'll succeed only the directory is empty which is what we want.
113  7 getBatchDirectory(batchId).delete();
114    } catch (Exception e) {
115  0 throw new MailStoreException(String.format(
116    "Failed to delete message (id [%s], batch id [%s]) file [%s]",
117    uniqueMessageId, batchId, messageFile), e);
118    }
119    }
120   
 
121  55 toggle private File getBatchDirectory(String batchId)
122    {
123  55 File batchDirectory = new File(rootDirectory, getURLEncoded(batchId));
124  55 batchDirectory.mkdirs();
125  55 return batchDirectory;
126    }
127   
 
128  48 toggle private File getMessageFile(String batchId, String uniqueMessageId) {
129  48 return new File(getBatchDirectory(batchId), getURLEncoded(uniqueMessageId));
130    }
131   
 
132  103 toggle private static String getURLEncoded(final String toEncode)
133    {
134  103 try {
135  103 return URLEncoder.encode(toEncode, "UTF-8");
136    } catch (UnsupportedEncodingException ex) {
137  0 throw new RuntimeException("UTF-8 not available, this Java VM is not standards compliant!");
138    }
139    }
140    }