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

File UsersAndGroupsSource.java

 

Coverage histogram

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

Code metrics

6
35
9
1
163
92
12
0.34
3.89
9
1.33

Classes

Class Line # Actions
UsersAndGroupsSource 43 35 0% 12 7
0.8686%
 

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.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.mail.Address;
28    import javax.mail.MessagingException;
29    import javax.mail.internet.InternetAddress;
30   
31    import org.apache.commons.lang3.builder.ToStringBuilder;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.text.XWikiToStringBuilder;
34   
35    /**
36    * Represents the inputs of the {@link UsersAndGroupsMimeMessageFactory}: a list of user and group references
37    * + a list of email addresses, and optional exclusion lists.
38    *
39    * @version $Id: 1a5983f70f6d64a2441b148a943a9311fff2f374 $
40    * @since 6.4.2
41    * @since 7.0M2
42    */
 
43    public class UsersAndGroupsSource
44    {
45    private List<DocumentReference> includedUserAndGroupReferences;
46   
47    private List<DocumentReference> excludedUserAndGroupReferences;
48   
49    private List<Address> includedAddresses;
50   
51    private List<Address> excludedAdresses;
52   
53    /**
54    * @param includedUserAndGroupReferences see {@link #getIncludedUserAndGroupReferences()}
55    * @param excludedUserAndGroupReferences see {@link #getExcludedUserAndGroupReferences()}
56    * @param includedAddresses see {@link #getIncludedAddresses()}
57    * @param excludedAddresses see {@link #getExcludedAddresses()}
58    */
 
59  4 toggle public UsersAndGroupsSource(List<DocumentReference> includedUserAndGroupReferences,
60    List<DocumentReference> excludedUserAndGroupReferences, List<Address> includedAddresses,
61    List<Address> excludedAddresses)
62    {
63  4 this.includedUserAndGroupReferences = includedUserAndGroupReferences;
64  4 this.excludedUserAndGroupReferences = excludedUserAndGroupReferences;
65  4 this.includedAddresses = includedAddresses;
66  4 this.excludedAdresses = excludedAddresses;
67    }
68   
69    /**
70    * @return the list of user and group references to iterate over
71    */
 
72  4 toggle public List<DocumentReference> getIncludedUserAndGroupReferences()
73    {
74  4 return this.includedUserAndGroupReferences;
75    }
76   
77    /**
78    * @return the list of user and group references to exclude
79    */
 
80  4 toggle public List<DocumentReference> getExcludedUserAndGroupReferences()
81    {
82  4 return this.excludedUserAndGroupReferences;
83    }
84   
85    /**
86    * @return the list of email addresses to iterate over
87    */
 
88  4 toggle public List<Address> getIncludedAddresses()
89    {
90  4 return this.includedAddresses;
91    }
92   
93    /**
94    * @return the list of email addresses to exclude
95    */
 
96  4 toggle public List<Address> getExcludedAddresses()
97    {
98  4 return this.excludedAdresses;
99    }
100   
101    /**
102    * @param sourceMap a Map containing the list of user + group references + a list of email addresses to iterate over
103    * (with optional excludes). The supported map keys are {@code users}, {@code groups}, {@code emails}
104    * {@code excludedUsers}, {@code excludedGroups} and {@code excludedEmails}
105    * @return the typed instance representing the inputs passed
106    * @throws MessagingException if one the passed email addresses is invalid (note that we're not parsing emails in
107    * strict mode and thus it's unlikely any exception will be raised in practice)
108    */
 
109  4 toggle public static UsersAndGroupsSource parse(Map<String, Object> sourceMap) throws MessagingException
110    {
111  4 List<DocumentReference> includedUserAndGroupReferences =
112    extractUserAndGroupReferences("users", "groups", sourceMap);
113  4 List<DocumentReference> excludedUserAndGroupReferences =
114    extractUserAndGroupReferences("excludedUsers", "excludedGroups", sourceMap);
115   
116  4 List<Address> includedAddresses = extractAddresses("emails", sourceMap);
117  4 List<Address> excludedAddresses = extractAddresses("excludedEmails", sourceMap);
118   
119  4 return new UsersAndGroupsSource(includedUserAndGroupReferences, excludedUserAndGroupReferences,
120    includedAddresses, excludedAddresses);
121    }
122   
 
123  8 toggle private static List<Address> extractAddresses(String key, Map<String, Object> sourceMap) throws MessagingException
124    {
125  8 List<Address> addresses;
126  8 List<String> emails = (List<String>) sourceMap.get(key);
127  8 if (emails != null) {
128  1 addresses = new ArrayList<>();
129  1 for (String email : emails) {
130  1 addresses.add(InternetAddress.parse(email)[0]);
131    }
132    } else {
133  7 addresses = Collections.emptyList();
134    }
135  8 return addresses;
136    }
137   
 
138  8 toggle private static List<DocumentReference> extractUserAndGroupReferences(String userKey, String groupKey,
139    Map<String, Object> sourceMap)
140    {
141  8 List<DocumentReference> userAndGroupReferences = new ArrayList<>();
142  8 List<DocumentReference> userReferences = (List<DocumentReference>) sourceMap.get(userKey);
143  8 if (userReferences != null) {
144  4 userAndGroupReferences.addAll(userReferences);
145    }
146  8 List<DocumentReference> groupReferences = (List<DocumentReference>) sourceMap.get(groupKey);
147  8 if (groupReferences != null) {
148  1 userAndGroupReferences.addAll(groupReferences);
149    }
150  8 return userAndGroupReferences;
151    }
152   
 
153  0 toggle @Override
154    public String toString()
155    {
156  0 ToStringBuilder builder = new XWikiToStringBuilder(this);
157  0 builder.append("includedUserAndGroupsReferences", getIncludedUserAndGroupReferences());
158  0 builder.append("excludedUserAndGroupsReferences", getExcludedUserAndGroupReferences());
159  0 builder.append("includedAddresses", getIncludedAddresses());
160  0 builder.append("excludedAddresses", getExcludedAddresses());
161  0 return builder.toString();
162    }
163    }