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

File UsersAndGroupsMimeMessageIterator.java

 

Coverage histogram

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

Code metrics

2
17
5
1
113
60
7
0.41
3.4
5
1.4

Classes

Class Line # Actions
UsersAndGroupsMimeMessageIterator 43 17 0% 7 2
0.916666791.7%
 

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.usersandgroups;
21   
22    import java.util.Iterator;
23    import java.util.Map;
24   
25    import javax.mail.Address;
26    import javax.mail.Message;
27    import javax.mail.MessagingException;
28    import javax.mail.internet.MimeMessage;
29   
30    import org.xwiki.context.Execution;
31    import org.xwiki.mail.ExtendedMimeMessage;
32    import org.xwiki.mail.MimeMessageFactory;
33    import org.xwiki.model.reference.DocumentReferenceResolver;
34   
35    /**
36    * Generate messages from a list of group references, a list of user references and a list of email addresses. Handles
37    * duplicates so that an email address is sent the message only once.
38    *
39    * @version $Id: 17d964ec1e54be1a5bda008219baf4b2f04c6e11 $
40    * @since 6.4.2
41    * @since 7.0M2
42    */
 
43    public class UsersAndGroupsMimeMessageIterator implements Iterator<MimeMessage>, Iterable<MimeMessage>
44    {
45    private MimeMessageFactory<MimeMessage> factory;
46   
47    private Iterator<Address> addressIterator;
48   
49    private Map<String, Object> parameters;
50   
51    /**
52    * @param source the list of group and user references from which to extract the list of recipients and a list of
53    * emails to send the messages to
54    * @param factory the factory to use to create a single MimeMessage
55    * @param parameters the parameters from which to extract the factory source and factory parameters
56    * @param explicitDocumentReferenceResolver the resolver to use for transforming group member strings into
57    * {@link org.xwiki.model.reference.DocumentReference}
58    * @param execution the component used to access the {@link com.xpn.xwiki.XWikiContext} we use to call oldcore APIs
59    * @throws MessagingException if one the passed email addresses is invalid (note that we're not parsing emails in
60    * strict mode and thus it's unlikely any exception will be raised in practice)
61    */
 
62  4 toggle public UsersAndGroupsMimeMessageIterator(Map<String, Object> source,
63    MimeMessageFactory<MimeMessage> factory, Map<String, Object> parameters,
64    DocumentReferenceResolver<String> explicitDocumentReferenceResolver, Execution execution)
65    throws MessagingException
66    {
67  4 this.addressIterator = new AddressUserIterator(UsersAndGroupsSource.parse(source),
68    explicitDocumentReferenceResolver, execution);
69  4 this.factory = factory;
70  4 this.parameters = parameters;
71    }
72   
 
73  1 toggle @Override
74    public Iterator<MimeMessage> iterator()
75    {
76  1 return this;
77    }
78   
 
79  7 toggle @Override
80    public boolean hasNext()
81    {
82  7 return this.addressIterator.hasNext();
83    }
84   
 
85  5 toggle @Override
86    public MimeMessage next()
87    {
88  5 MimeMessage message;
89  5 Address address = this.addressIterator.next();
90   
91  5 try {
92  5 Map<String, Object> factoryParameters = (Map<String, Object>) this.parameters.get("parameters");
93  5 message = this.factory.createMessage(this.parameters.get("source"), factoryParameters);
94  4 message.addRecipient(Message.RecipientType.TO, address);
95   
96    // Set the Message Type if passed in parameters
97  4 String type = (String) this.parameters.get("type");
98  4 if (type != null) {
99  2 ExtendedMimeMessage.wrap(message).setType(type);
100    }
101    } catch (MessagingException e) {
102  1 throw new RuntimeException("Failed to create Mime Message for recipient " + address, e);
103    }
104   
105  4 return message;
106    }
107   
 
108  0 toggle @Override
109    public void remove()
110    {
111  0 throw new UnsupportedOperationException("remove");
112    }
113    }