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

File UsersMimeMessageIterator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
23
4
1
119
69
6
0.26
5.75
4
1.5

Classes

Class Line # Actions
UsersMimeMessageIterator 48 23 0% 6 6
0.7931034679.3%
 

Contributing tests

This file is covered by 1 test. .

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.users;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import javax.mail.Message;
26    import javax.mail.MessagingException;
27    import javax.mail.internet.InternetAddress;
28    import javax.mail.internet.MimeMessage;
29   
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.mail.ExtendedMimeMessage;
36    import org.xwiki.mail.MimeMessageFactory;
37    import org.xwiki.mail.internal.factory.AbstractMessageIterator;
38    import org.xwiki.model.reference.DocumentReference;
39   
40    /**
41    * Generate messages from a user references.
42    *
43    * @version $Id: 0c780a2f343011f34e4555f0a1cb2effbd50fec4 $
44    * @since 6.4M3
45    * @deprecated starting with 6.4.2 this is replaced by the {@code usersandroups} Mime Message Factory
46    */
47    @Deprecated
 
48    public class UsersMimeMessageIterator extends AbstractMessageIterator
49    {
50    private static final Logger LOGGER = LoggerFactory.getLogger(UsersMimeMessageIterator.class);
51   
52    private DocumentAccessBridge documentAccessBridge;
53   
54    private final List<DocumentReference> users;
55   
56    private ComponentManager componentManager;
57   
58    /**
59    * @param userReferences the list of recipients
60    * @param factory to create MimeMessage
61    * @param parameters the parameters from which to extract the session, source and the headers
62    * @param componentManager used to dynamically load components
63    * @throws MessagingException when an error occurs
64    */
 
65  1 toggle public UsersMimeMessageIterator(List<DocumentReference> userReferences,
66    MimeMessageFactory<MimeMessage> factory, Map<String, Object> parameters,
67    ComponentManager componentManager) throws MessagingException
68    {
69  1 this.factory = factory;
70  1 this.parameters = parameters;
71  1 this.iteratorSize = userReferences.size();
72  1 this.users = userReferences;
73  1 this.componentManager = componentManager;
74  1 this.documentAccessBridge = getAccessBridge();
75    }
76   
 
77  1 toggle private DocumentAccessBridge getAccessBridge() throws MessagingException
78    {
79  1 DocumentAccessBridge accessBridge;
80  1 try {
81  1 accessBridge = this.componentManager.getInstance(DocumentAccessBridge.class);
82    } catch (ComponentLookupException e) {
83  0 throw new MessagingException("Failed to find default Document bridge ", e);
84    }
85  1 return accessBridge;
86    }
87   
 
88  3 toggle @Override
89    protected ExtendedMimeMessage createMessageInternal() throws MessagingException
90    {
91  3 ExtendedMimeMessage mimeMessage;
92   
93  3 DocumentReference userReference = users.get(this.position);
94   
95    // If the user has no email address then return a null Mime Message so that it's skipped
96  3 Object emailObject = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference
97    .getWikiReference().getName(), "XWiki", "XWikiUsers"), "email");
98  3 if (emailObject != null) {
99  3 String email = emailObject.toString();
100   
101  3 Map<String, Object> parameters = (Map<String, Object>) this.parameters.get("parameters");
102   
103  3 mimeMessage =
104    ExtendedMimeMessage.wrap(this.factory.createMessage(this.parameters.get("source"), parameters));
105  3 mimeMessage.addRecipient(Message.RecipientType.TO, InternetAddress.parse(email)[0]);
106    } else {
107  0 getLogger().warn("User [{}] has no email defined. Email has not been sent to that user.", userReference);
108  0 mimeMessage = null;
109    }
110   
111  3 return mimeMessage;
112    }
113   
 
114  0 toggle @Override
115    protected Logger getLogger()
116    {
117  0 return LOGGER;
118    }
119    }