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

File SerializedFilesMimeMessageIterator.java

 

Coverage histogram

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

Code metrics

0
14
3
1
102
54
5
0.36
4.67
3
1.67

Classes

Class Line # Actions
SerializedFilesMimeMessageIterator 43 14 0% 5 0
1.0100%
 

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.files;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.FileNotFoundException;
25    import java.util.Map;
26   
27    import javax.mail.MessagingException;
28   
29    import org.slf4j.Logger;
30    import org.slf4j.LoggerFactory;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.environment.Environment;
34    import org.xwiki.mail.ExtendedMimeMessage;
35    import org.xwiki.mail.internal.factory.AbstractMessageIterator;
36   
37    /**
38    * Generate messages from a list of files.
39    *
40    * @version $Id: a8d2459bc785c14d558887fbe50d05e8f1677a7b $
41    * @since 6.4M3
42    */
 
43    public class SerializedFilesMimeMessageIterator extends AbstractMessageIterator
44    {
45    private static final Logger LOGGER = LoggerFactory.getLogger(SerializedFilesMimeMessageIterator.class);
46   
47    /**
48    * The mails store directory name.
49    */
50    private static final String ROOT_DIRECTORY = "mailstore";
51   
52    private final File[] files;
53   
54    private ComponentManager componentManager;
55   
56    private Environment environment;
57   
58    private File batchDirectory;
59   
60    /**
61    * @param batchId the name of the directory that contains serialized MimeMessages
62    * @param parameters the parameters from which to extract the session
63    * @param componentManager used to dynamically load components
64    * @throws MessagingException when an error occurs when retrieving messages
65    */
 
66  3 toggle public SerializedFilesMimeMessageIterator(String batchId, Map<String, Object> parameters,
67    ComponentManager componentManager) throws MessagingException
68    {
69  3 this.componentManager = componentManager;
70  3 try {
71  3 this.environment = this.componentManager.getInstance(Environment.class);
72    } catch (ComponentLookupException e) {
73  1 throw new MessagingException("Failed to find an Environment Component", e);
74    }
75  2 this.batchDirectory =
76    new File(new File(this.environment.getPermanentDirectory(), ROOT_DIRECTORY), batchId);
77  2 this.files = this.batchDirectory.listFiles();
78  2 this.iteratorSize = this.files.length;
79  2 this.parameters = parameters;
80    }
81   
 
82  4 toggle @Override
83    protected ExtendedMimeMessage createMessageInternal() throws MessagingException
84    {
85  4 File file = this.files[this.position];
86  4 try {
87  4 FileInputStream emailStream = new FileInputStream(file);
88    // Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
89    // given a valid Session when it's deserialized from the mail content store for sending.
90  3 return new ExtendedMimeMessage(null, emailStream);
91    } catch (FileNotFoundException e) {
92  1 throw new MessagingException(
93    String.format("Failed to create mime message from file [%s]", file.getPath()), e);
94    }
95    }
96   
 
97  1 toggle @Override
98    protected Logger getLogger()
99    {
100  1 return LOGGER;
101    }
102    }