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

File GroupMimeMessageIterator.java

 

Coverage histogram

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

Code metrics

2
40
6
1
168
108
11
0.28
6.67
6
1.83

Classes

Class Line # Actions
GroupMimeMessageIterator 53 40 0% 11 9
0.812581.2%
 

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.group;
21   
22    import java.util.Map;
23   
24    import javax.mail.Message;
25    import javax.mail.MessagingException;
26    import javax.mail.internet.MimeMessage;
27   
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.context.Execution;
34    import org.xwiki.mail.ExtendedMimeMessage;
35    import org.xwiki.mail.MimeMessageFactory;
36    import org.xwiki.mail.internal.factory.AbstractMessageIterator;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.DocumentReferenceResolver;
40    import org.xwiki.model.reference.EntityReference;
41   
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.XWikiException;
44   
45    /**
46    * Generate messages from a group reference.
47    *
48    * @version $Id: e9023ab075b3c6bbd1df111b67d546fecb641702 $
49    * @since 6.4M3
50    * @deprecated starting with 6.4.2 this is replaced by the {@code usersandroups} Mime Message Factory
51    */
52    @Deprecated
 
53    public class GroupMimeMessageIterator extends AbstractMessageIterator
54    {
55    private static final Logger LOGGER = LoggerFactory.getLogger(GroupMimeMessageIterator.class);
56   
57    private static final String USER_SPACE = "XWiki";
58   
59    private static final EntityReference GROUPS_CLASS =
60    new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference(USER_SPACE, EntityType.SPACE));
61   
62    private DocumentAccessBridge documentAccessBridge;
63   
64    private DocumentReferenceResolver<String> stringResolver;
65   
66    private DocumentReference groupReference;
67   
68    private ComponentManager componentManager;
69   
70    /**
71    * @param groupReference the group that contains list of recipients
72    * @param factory the factory to use to create a single MimeMessage
73    * @param parameters the parameters from which to extract the session, source and the headers
74    * @param componentManager used to dynamically load components
75    * @throws MessagingException when an error occurs when retrieving the number of users
76    */
 
77  1 toggle public GroupMimeMessageIterator(DocumentReference groupReference,
78    MimeMessageFactory<MimeMessage> factory, Map<String, Object> parameters,
79    ComponentManager componentManager) throws MessagingException
80    {
81  1 this.factory = factory;
82  1 this.parameters = parameters;
83  1 this.groupReference = groupReference;
84  1 this.componentManager = componentManager;
85   
86  1 XWikiContext context = getXWikiContext();
87  1 try {
88  1 this.iteratorSize = context.getWiki().getDocument(groupReference, context).getXObjects(GROUPS_CLASS).size();
89    } catch (XWikiException e) {
90  0 throw new MessagingException(String.format(
91    "Failed to find number of [%s] objects in group Document [%s]", GROUPS_CLASS, groupReference), e);
92    }
93  1 this.documentAccessBridge = getAccessBridge();
94   
95  1 this.stringResolver = getResolver();
96    }
97   
 
98  3 toggle @Override
99    protected ExtendedMimeMessage createMessageInternal() throws MessagingException
100    {
101  3 ExtendedMimeMessage mimeMessage;
102   
103  3 DocumentReference groupsClassReference = this.stringResolver.resolve(USER_SPACE + ".XWikiGroups");
104   
105  3 String userFullName = this.documentAccessBridge.getProperty(this.groupReference, groupsClassReference,
106    this.position, "member").toString();
107   
108  3 DocumentReference userReference = this.stringResolver.resolve(userFullName);
109   
110    // If the user has no email address then return a null Mime Message so that it's skipped
111  3 Object emailObject = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference
112    .getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "email");
113  3 if (emailObject != null) {
114  3 String email = emailObject.toString();
115   
116  3 Map<String, Object> parameters = (Map<String, Object>) this.parameters.get("parameters");
117   
118  3 mimeMessage =
119    ExtendedMimeMessage.wrap(this.factory.createMessage(this.parameters.get("source"), parameters));
120  3 mimeMessage.addRecipients(Message.RecipientType.TO, email);
121    } else {
122  0 getLogger().warn("User [{}] has no email defined. Email has not been sent to that user.", userReference);
123  0 mimeMessage = null;
124    }
125   
126  3 return mimeMessage;
127    }
128   
 
129  0 toggle @Override
130    protected Logger getLogger()
131    {
132  0 return LOGGER;
133    }
134   
 
135  1 toggle private DocumentAccessBridge getAccessBridge() throws MessagingException
136    {
137  1 DocumentAccessBridge accessBridge;
138  1 try {
139  1 accessBridge = this.componentManager.getInstance(DocumentAccessBridge.class);
140    } catch (ComponentLookupException e) {
141  0 throw new MessagingException("Failed to find default Document bridge ", e);
142    }
143  1 return accessBridge;
144    }
145   
 
146  1 toggle private XWikiContext getXWikiContext() throws MessagingException
147    {
148  1 XWikiContext xWikiContext;
149  1 try {
150  1 Execution execution = this.componentManager.getInstance(Execution.class);
151  1 xWikiContext = (XWikiContext) execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
152    } catch (ComponentLookupException e) {
153  0 throw new MessagingException("Failed to find default Execution context", e);
154    }
155  1 return xWikiContext;
156    }
157   
 
158  1 toggle private DocumentReferenceResolver<String> getResolver() throws MessagingException
159    {
160  1 DocumentReferenceResolver<String> resolver;
161  1 try {
162  1 resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
163    } catch (ComponentLookupException e) {
164  0 throw new MessagingException("Failed to find default Document resolver", e);
165    }
166  1 return resolver;
167    }
168    }