1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.mailsender

File MailConfiguration.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

22
57
17
1
217
144
32
0.56
3.35
17
1.88

Classes

Class Line # Actions
MailConfiguration 39 57 0% 32 53
0.4479166644.8%
 

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 com.xpn.xwiki.plugin.mailsender;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.util.Properties;
26    import java.util.Map.Entry;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.xwiki.mail.MailSenderConfiguration;
30   
31    import com.xpn.xwiki.api.XWiki;
32    import com.xpn.xwiki.web.Utils;
33   
34    /**
35    * Represents a Mail Server configuration.
36    *
37    * @version $Id: 62a60f0cc837788875ac8e031dc18a43ce3523f7 $
38    */
 
39    public class MailConfiguration
40    {
41    private int port;
42   
43    private String host;
44   
45    private String from;
46   
47    private String smtpUsername;
48   
49    private String smtpPassword;
50   
51    private Properties extraProperties;
52   
53    /**
54    * New Mail Sender module's configuration.
55    */
56    private MailSenderConfiguration mailSenderConfiguration;
57   
 
58  4 toggle public MailConfiguration()
59    {
60  4 this.mailSenderConfiguration = Utils.getComponent(MailSenderConfiguration.class);
61  4 setPort(25);
62  4 setHost("localhost");
63    }
64   
 
65  4 toggle public MailConfiguration(XWiki xwiki)
66    {
67  4 this();
68   
69  4 String smtpServer = this.mailSenderConfiguration.getHost();
70  4 if (!StringUtils.isBlank(smtpServer)) {
71  4 setHost(smtpServer);
72    }
73   
74  4 int port = this.mailSenderConfiguration.getPort();
75  4 setPort(port);
76   
77  4 String from = this.mailSenderConfiguration.getFromAddress();
78  4 if (!StringUtils.isBlank(from)) {
79  0 setFrom(from);
80    }
81   
82  4 String smtpServerUsername = this.mailSenderConfiguration.getUsername();
83  4 String smtpServerPassword = this.mailSenderConfiguration.getPassword();
84  4 if (!StringUtils.isEmpty(smtpServerUsername) && !StringUtils.isEmpty(smtpServerPassword)) {
85  0 setSmtpUsername(smtpServerUsername);
86  0 setSmtpPassword(smtpServerPassword);
87    }
88   
89  4 Properties javaMailExtraProps = this.mailSenderConfiguration.getAdditionalProperties();
90  4 if (!javaMailExtraProps.isEmpty()) {
91  0 setExtraProperties(javaMailExtraProps);
92    }
93    }
94   
 
95  8 toggle public void setHost(String host)
96    {
97  8 this.host = host;
98    }
99   
 
100  5 toggle public String getHost()
101    {
102  5 return this.host;
103    }
104   
 
105  8 toggle public void setPort(int port)
106    {
107  8 this.port = port;
108    }
109   
 
110  5 toggle public int getPort()
111    {
112  5 return this.port;
113    }
114   
 
115  1 toggle public void setFrom(String from)
116    {
117  1 this.from = from;
118    }
119   
 
120  6 toggle public String getFrom()
121    {
122  6 return this.from;
123    }
124   
 
125  0 toggle public void setSmtpUsername(String smtpUsername)
126    {
127  0 this.smtpUsername = smtpUsername;
128    }
129   
 
130  8 toggle public String getSmtpUsername()
131    {
132  8 return this.smtpUsername;
133    }
134   
 
135  0 toggle public void setSmtpPassword(String smtpPassword)
136    {
137  0 this.smtpPassword = smtpPassword;
138    }
139   
 
140  0 toggle public String getSmtpPassword()
141    {
142  0 return this.smtpPassword;
143    }
144   
 
145  8 toggle public boolean usesAuthentication()
146    {
147  8 return !StringUtils.isEmpty(getSmtpUsername()) && !StringUtils.isEmpty(getSmtpPassword());
148    }
149   
 
150  0 toggle public void setExtraProperties(Properties extraProperties)
151    {
152  0 this.extraProperties = extraProperties;
153    }
154   
 
155  0 toggle public void setExtraProperties(String extraPropertiesString)
156    {
157  0 if (StringUtils.isEmpty(extraPropertiesString)) {
158  0 this.extraProperties = null;
159    } else {
160  0 InputStream is = new ByteArrayInputStream(extraPropertiesString.getBytes());
161  0 this.extraProperties = new Properties();
162  0 try {
163  0 this.extraProperties.load(is);
164    } catch (IOException e) {
165    // Shouldn't ever occur...
166  0 throw new RuntimeException("Error configuring mail connection.", e);
167    }
168    }
169    }
170   
171    /**
172    * Add extraProperties to an external Properties object
173    *
174    * @param externalProperties
175    * @param overwrite
176    */
 
177  4 toggle public void appendExtraPropertiesTo(Properties externalProperties, boolean overwrite)
178    {
179    // sanity check
180  4 if (externalProperties == null) {
181  0 throw new IllegalArgumentException("externalProperties can't be null");
182    }
183   
184  4 if (this.extraProperties != null && this.extraProperties.size() > 0) {
185  0 for (Entry<Object, Object> e : this.extraProperties.entrySet()) {
186  0 String propName = (String) e.getKey();
187  0 String propValue = (String) e.getValue();
188  0 if (overwrite || externalProperties.getProperty(propName) == null) {
189  0 externalProperties.setProperty(propName, propValue);
190    }
191    }
192    }
193    }
194   
 
195  0 toggle @Override
196    public String toString()
197    {
198  0 StringBuilder buffer = new StringBuilder();
199   
200  0 if (getHost() != null) {
201  0 buffer.append("Host [" + getHost() + "]");
202    }
203   
204  0 if (getFrom() != null) {
205  0 buffer.append(", From [" + getFrom() + "]");
206    }
207   
208  0 buffer.append(", Port [" + getPort() + "]");
209   
210  0 if (usesAuthentication()) {
211  0 buffer.append(", Username [" + getSmtpUsername() + "]");
212  0 buffer.append(", Password [*****]");
213    }
214   
215  0 return buffer.toString();
216    }
217    }