| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.mail.internal.configuration; |
| 21 |
|
|
| 22 |
|
import java.io.ByteArrayInputStream; |
| 23 |
|
import java.io.InputStream; |
| 24 |
|
import java.util.ArrayList; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Properties; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Named; |
| 30 |
|
import javax.inject.Singleton; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
| 33 |
|
import org.apache.commons.lang3.StringUtils; |
| 34 |
|
import org.slf4j.Logger; |
| 35 |
|
import org.xwiki.component.annotation.Component; |
| 36 |
|
import org.xwiki.configuration.ConfigurationSource; |
| 37 |
|
import org.xwiki.mail.MailSenderConfiguration; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@version |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@Component |
| 52 |
|
@Singleton |
| |
|
| 99% |
Uncovered Elements: 1 (102) |
Complexity: 26 |
Complexity Density: 0.39 |
|
| 53 |
|
public class DefaultMailSenderConfiguration implements MailSenderConfiguration |
| 54 |
|
{ |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
public static final String JAVAMAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
public static final String JAVAMAIL_SMTP_HOST = "mail.smtp.host"; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
public static final String JAVAMAIL_SMTP_PORT = "mail.smtp.port"; |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
public static final String JAVAMAIL_SMTP_USERNAME = "mail.smtp.user"; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
public static final String JAVAMAIL_SMTP_FROM = "mail.smtp.from"; |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
public static final String JAVAMAIL_SMTP_AUTH = "mail.smtp.auth"; |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
private static final String PREFIX = "mail.sender."; |
| 89 |
|
|
| 90 |
|
private static final int DEFAULT_PORT = 25; |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
private static final long DEFAULT_SEND_WAIT_TIME = 8 * 1000L; |
| 97 |
|
|
| 98 |
|
private static final String FROM_PROPERTY = "from"; |
| 99 |
|
private static final String BCC_PROPERTY = "bcc"; |
| 100 |
|
private static final String HOST_PROPERTY = "host"; |
| 101 |
|
private static final String PORT_PROPERTY = "port"; |
| 102 |
|
private static final String USERNAME_PROPERTY = "username"; |
| 103 |
|
private static final String PASSWORD_PROPERTY = "password"; |
| 104 |
|
private static final String PROPERTIES_PROPERTY = "properties"; |
| 105 |
|
private static final String SEND_WAIT_TIME = "sendWaitTime"; |
| 106 |
|
|
| 107 |
|
@Inject |
| 108 |
|
private Logger logger; |
| 109 |
|
|
| 110 |
|
@Inject |
| 111 |
|
@Named("mailsend") |
| 112 |
|
private ConfigurationSource mailConfigSource; |
| 113 |
|
|
| 114 |
|
@Inject |
| 115 |
|
@Named("documents") |
| 116 |
|
private ConfigurationSource documentsSource; |
| 117 |
|
|
| 118 |
|
@Inject |
| 119 |
|
@Named("xwikiproperties") |
| 120 |
|
private ConfigurationSource xwikiPropertiesSource; |
| 121 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 122 |
38 |
@Override... |
| 123 |
|
public String getHost() |
| 124 |
|
{ |
| 125 |
38 |
String host; |
| 126 |
|
|
| 127 |
|
|
| 128 |
38 |
host = this.mailConfigSource.getProperty(HOST_PROPERTY, |
| 129 |
|
this.documentsSource.getProperty("smtp_server", String.class)); |
| 130 |
|
|
| 131 |
|
|
| 132 |
38 |
if (host == null) { |
| 133 |
21 |
host = this.xwikiPropertiesSource.getProperty(PREFIX + HOST_PROPERTY, "localhost"); |
| 134 |
|
} |
| 135 |
|
|
| 136 |
38 |
return host; |
| 137 |
|
} |
| 138 |
|
|
| |
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 139 |
40 |
@Override... |
| 140 |
|
public int getPort() |
| 141 |
|
{ |
| 142 |
40 |
Integer port = null; |
| 143 |
|
|
| 144 |
|
|
| 145 |
40 |
String portAsString = this.documentsSource.getProperty("smtp_port"); |
| 146 |
40 |
if (!StringUtils.isEmpty(portAsString)) { |
| 147 |
1 |
try { |
| 148 |
1 |
port = this.mailConfigSource.getProperty(PORT_PROPERTY, Integer.parseInt(portAsString)); |
| 149 |
|
} catch (NumberFormatException e) { |
| 150 |
0 |
port = DEFAULT_PORT; |
| 151 |
|
} |
| 152 |
|
} else { |
| 153 |
39 |
port = this.mailConfigSource.getProperty(PORT_PROPERTY, Integer.class); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
40 |
if (port == null) { |
| 158 |
22 |
port = this.xwikiPropertiesSource.getProperty(PREFIX + PORT_PROPERTY, DEFAULT_PORT); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
40 |
return port; |
| 162 |
|
} |
| 163 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 164 |
113 |
@Override... |
| 165 |
|
public String getUsername() |
| 166 |
|
{ |
| 167 |
113 |
String username; |
| 168 |
|
|
| 169 |
|
|
| 170 |
113 |
username = this.mailConfigSource.getProperty(USERNAME_PROPERTY, |
| 171 |
|
this.documentsSource.getProperty("smtp_server_username", String.class)); |
| 172 |
|
|
| 173 |
|
|
| 174 |
113 |
if (username == null) { |
| 175 |
112 |
username = this.xwikiPropertiesSource.getProperty(PREFIX + USERNAME_PROPERTY, String.class); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
113 |
return username; |
| 179 |
|
} |
| 180 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 181 |
2 |
@Override... |
| 182 |
|
public String getPassword() |
| 183 |
|
{ |
| 184 |
2 |
String password; |
| 185 |
|
|
| 186 |
|
|
| 187 |
2 |
password = this.mailConfigSource.getProperty(PASSWORD_PROPERTY, |
| 188 |
|
this.documentsSource.getProperty("smtp_server_password", String.class)); |
| 189 |
|
|
| 190 |
|
|
| 191 |
2 |
if (password == null) { |
| 192 |
1 |
password = this.xwikiPropertiesSource.getProperty(PREFIX + PASSWORD_PROPERTY, String.class); |
| 193 |
|
} |
| 194 |
|
|
| 195 |
2 |
return password; |
| 196 |
|
} |
| 197 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 198 |
8 |
@Override... |
| 199 |
|
public List<String> getBCCAddresses() |
| 200 |
|
{ |
| 201 |
8 |
List<String> bccAddresses = new ArrayList<>(); |
| 202 |
|
|
| 203 |
|
|
| 204 |
8 |
String bccAsString = this.mailConfigSource.getProperty(BCC_PROPERTY, String.class); |
| 205 |
|
|
| 206 |
|
|
| 207 |
8 |
if (bccAsString == null) { |
| 208 |
7 |
bccAsString = this.xwikiPropertiesSource.getProperty(PREFIX + BCC_PROPERTY, String.class); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
8 |
if (bccAsString != null) { |
| 213 |
1 |
for (String address : StringUtils.split(bccAsString, ',')) { |
| 214 |
2 |
bccAddresses.add(StringUtils.trim(address)); |
| 215 |
|
} |
| 216 |
|
} |
| 217 |
|
|
| 218 |
8 |
return bccAddresses; |
| 219 |
|
} |
| 220 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 221 |
74 |
@Override... |
| 222 |
|
public String getFromAddress() |
| 223 |
|
{ |
| 224 |
74 |
String from; |
| 225 |
|
|
| 226 |
|
|
| 227 |
74 |
from = this.mailConfigSource.getProperty(FROM_PROPERTY, |
| 228 |
|
this.documentsSource.getProperty("admin_email", String.class)); |
| 229 |
|
|
| 230 |
|
|
| 231 |
74 |
if (from == null) { |
| 232 |
71 |
from = this.xwikiPropertiesSource.getProperty(PREFIX + FROM_PROPERTY, String.class); |
| 233 |
|
} |
| 234 |
|
|
| 235 |
74 |
return from; |
| 236 |
|
} |
| 237 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 238 |
42 |
@Override... |
| 239 |
|
public Properties getAdditionalProperties() |
| 240 |
|
{ |
| 241 |
42 |
Properties properties; |
| 242 |
|
|
| 243 |
|
|
| 244 |
42 |
String extraPropertiesAsString = this.mailConfigSource.getProperty(PROPERTIES_PROPERTY, |
| 245 |
|
this.documentsSource.getProperty("javamail_extra_props", String.class)); |
| 246 |
|
|
| 247 |
|
|
| 248 |
42 |
if (extraPropertiesAsString == null) { |
| 249 |
38 |
properties = this.xwikiPropertiesSource.getProperty(PREFIX + PROPERTIES_PROPERTY, Properties.class); |
| 250 |
|
} else { |
| 251 |
|
|
| 252 |
4 |
InputStream is = new ByteArrayInputStream(extraPropertiesAsString.getBytes()); |
| 253 |
4 |
properties = new Properties(); |
| 254 |
4 |
try { |
| 255 |
4 |
properties.load(is); |
| 256 |
|
} catch (Exception e) { |
| 257 |
|
|
| 258 |
|
|
| 259 |
1 |
this.logger.warn("Error while parsing mail properties [{}]. Root cause [{}]. Ignoring configuration...", |
| 260 |
|
extraPropertiesAsString, ExceptionUtils.getRootCauseMessage(e)); |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| 264 |
42 |
return properties; |
| 265 |
|
} |
| 266 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 267 |
43 |
@Override... |
| 268 |
|
public Properties getAllProperties() |
| 269 |
|
{ |
| 270 |
43 |
Properties properties = new Properties(); |
| 271 |
43 |
addProperty(properties, JAVAMAIL_TRANSPORT_PROTOCOL, "smtp"); |
| 272 |
43 |
addProperty(properties, JAVAMAIL_SMTP_HOST, getHost()); |
| 273 |
43 |
addProperty(properties, JAVAMAIL_SMTP_USERNAME, getUsername()); |
| 274 |
43 |
addProperty(properties, JAVAMAIL_SMTP_FROM, getFromAddress()); |
| 275 |
43 |
addProperty(properties, JAVAMAIL_SMTP_PORT, Integer.toString(getPort())); |
| 276 |
|
|
| 277 |
|
|
| 278 |
43 |
if (usesAuthentication()) { |
| 279 |
1 |
properties.put(JAVAMAIL_SMTP_AUTH, "true"); |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
43 |
properties.putAll(getAdditionalProperties()); |
| 287 |
|
|
| 288 |
43 |
return properties; |
| 289 |
|
} |
| 290 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 291 |
215 |
private void addProperty(Properties properties, String key, String value)... |
| 292 |
|
{ |
| 293 |
215 |
if (value != null) { |
| 294 |
137 |
properties.setProperty(key, value); |
| 295 |
|
} |
| 296 |
|
} |
| 297 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 298 |
83 |
@Override... |
| 299 |
|
public boolean usesAuthentication() |
| 300 |
|
{ |
| 301 |
83 |
return !StringUtils.isEmpty(getUsername()) && !StringUtils.isEmpty(getPassword()); |
| 302 |
|
} |
| 303 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 304 |
4 |
@Override... |
| 305 |
|
public String getScriptServicePermissionCheckerHint() |
| 306 |
|
{ |
| 307 |
4 |
return this.xwikiPropertiesSource.getProperty(PREFIX + "scriptServiceCheckerHint", "programmingrights"); |
| 308 |
|
} |
| 309 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 310 |
7 |
@Override... |
| 311 |
|
public long getSendWaitTime() |
| 312 |
|
{ |
| 313 |
7 |
Long waitTime = this.mailConfigSource.getProperty(SEND_WAIT_TIME); |
| 314 |
|
|
| 315 |
7 |
if (waitTime == null) { |
| 316 |
1 |
waitTime = this.xwikiPropertiesSource.getProperty(PREFIX + SEND_WAIT_TIME, DEFAULT_SEND_WAIT_TIME); |
| 317 |
|
} |
| 318 |
|
|
| 319 |
7 |
return waitTime; |
| 320 |
|
} |
| 321 |
|
} |