diff options
author | Anton Tananaev <anton@traccar.org> | 2022-07-14 18:42:28 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-07-14 18:42:28 -0700 |
commit | 204e98895b3cd9a37ceea1760917c07606b36b10 (patch) | |
tree | b73a44da2aa98d0dab3870efb59be09ae5ffaaf1 /src/main/java/org | |
parent | 900601111466aa861db8ae77b71b95aec6c8c82e (diff) | |
download | trackermap-server-204e98895b3cd9a37ceea1760917c07606b36b10.tar.gz trackermap-server-204e98895b3cd9a37ceea1760917c07606b36b10.tar.bz2 trackermap-server-204e98895b3cd9a37ceea1760917c07606b36b10.zip |
Migrate SMTP settings
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/traccar/config/Config.java | 31 | ||||
-rw-r--r-- | src/main/java/org/traccar/config/Keys.java | 96 | ||||
-rw-r--r-- | src/main/java/org/traccar/database/MailManager.java | 110 | ||||
-rw-r--r-- | src/main/java/org/traccar/notification/PropertiesProvider.java | 27 |
4 files changed, 165 insertions, 99 deletions
diff --git a/src/main/java/org/traccar/config/Config.java b/src/main/java/org/traccar/config/Config.java index 5f95c16d9..c73be6475 100644 --- a/src/main/java/org/traccar/config/Config.java +++ b/src/main/java/org/traccar/config/Config.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.InvalidPropertiesFormatException; +import java.util.Objects; import java.util.Properties; @Singleton @@ -71,8 +72,7 @@ public class Config { return hasKey(key.getKey()); } - @Deprecated - public boolean hasKey(String key) { + private boolean hasKey(String key) { return useEnvironmentVariables && System.getenv().containsKey(getEnvironmentVariableName(key)) || properties.containsKey(key); } @@ -102,12 +102,7 @@ public class Config { } public boolean getBoolean(ConfigKey<Boolean> key) { - return getBoolean(key.getKey()); - } - - @Deprecated - public boolean getBoolean(String key) { - return Boolean.parseBoolean(getString(key)); + return Boolean.parseBoolean(getString(key.getKey())); } public int getInteger(ConfigKey<Integer> key) { @@ -116,11 +111,7 @@ public class Config { return Integer.parseInt(value); } else { Integer defaultValue = key.getDefaultValue(); - if (defaultValue != null) { - return defaultValue; - } else { - return 0; - } + return Objects.requireNonNullElse(defaultValue, 0); } } @@ -139,11 +130,7 @@ public class Config { return Long.parseLong(value); } else { Long defaultValue = key.getDefaultValue(); - if (defaultValue != null) { - return defaultValue; - } else { - return 0; - } + return Objects.requireNonNullElse(defaultValue, 0L); } } @@ -153,11 +140,7 @@ public class Config { return Double.parseDouble(value); } else { Double defaultValue = key.getDefaultValue(); - if (defaultValue != null) { - return defaultValue; - } else { - return 0; - } + return Objects.requireNonNullElse(defaultValue, 0.0); } } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 2e1a14eaf..7ab45312f 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -801,6 +801,102 @@ public final class Keys { "templates"); /** + * Force SMTP settings from the config file and ignore user attributes. + */ + public static final ConfigKey<Boolean> MAIL_SMTP_IGNORE_USER_CONFIG = new BooleanConfigKey( + "mail.smtp.ignoreUserConfig", + List.of(KeyType.CONFIG)); + + /** + * The SMTP server to connect to. + */ + public static final ConfigKey<String> MAIL_SMTP_HOST = new StringConfigKey( + "mail.smtp.host", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * The SMTP server port to connect. Defaults to 25. + */ + public static final ConfigKey<Integer> MAIL_SMTP_PORT = new IntegerConfigKey( + "mail.smtp.port", + List.of(KeyType.CONFIG, KeyType.USER), + 25); + + /** + * Email transport protocol. Default value is "smtp". + */ + public static final ConfigKey<String> MAIL_TRANSPORT_PROTOCOL = new StringConfigKey( + "mail.transport.protocol", + List.of(KeyType.CONFIG, KeyType.USER), + "smtp"); + + /** + * If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a + * TLS-protected connection before issuing any login commands. + */ + public static final ConfigKey<Boolean> MAIL_SMTP_STARTTLS_ENABLE = new BooleanConfigKey( + "mail.smtp.starttls.enable", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * If true, requires the use of the STARTTLS command. If the server doesn't support the STARTTLS command, or the + * command fails, the connect method will fail. + */ + public static final ConfigKey<Boolean> MAIL_SMTP_STARTTLS_REQUIRED = new BooleanConfigKey( + "mail.smtp.starttls.required", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * If set to true, use SSL to connect and use the SSL port by default. + */ + public static final ConfigKey<Boolean> MAIL_SMTP_SSL_ENABLE = new BooleanConfigKey( + "mail.smtp.ssl.enable", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * If set to "*", all hosts are trusted. If set to a whitespace separated list of hosts, those hosts are trusted. + * Otherwise, trust depends on the certificate the server presents. + */ + public static final ConfigKey<String> MAIL_SMTP_SSL_TRUST = new StringConfigKey( + "mail.smtp.ssl.trust", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * Specifies the SSL protocols that will be enabled for SSL connections. + */ + public static final ConfigKey<String> MAIL_SMTP_SSL_PROTOCOLS = new StringConfigKey( + "mail.smtp.ssl.protocols", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * SMTP connection username. + */ + public static final ConfigKey<String> MAIL_SMTP_USERNAME = new StringConfigKey( + "mail.smtp.username", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * SMTP connection password. + */ + public static final ConfigKey<String> MAIL_SMTP_PASSWORD = new StringConfigKey( + "mail.smtp.password", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * Email address to use for SMTP MAIL command. + */ + public static final ConfigKey<String> MAIL_SMTP_FROM = new StringConfigKey( + "mail.smtp.from", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** + * The personal name for the email from address. + */ + public static final ConfigKey<String> MAIL_SMTP_FROM_NAME = new StringConfigKey( + "mail.smtp.fromName", + List.of(KeyType.CONFIG, KeyType.USER)); + + /** * SMS API service full URL. Enables SMS commands and notifications. */ public static final ConfigKey<String> SMS_HTTP_URL = new StringConfigKey( diff --git a/src/main/java/org/traccar/database/MailManager.java b/src/main/java/org/traccar/database/MailManager.java index 72b8b72c1..ec1681dcb 100644 --- a/src/main/java/org/traccar/database/MailManager.java +++ b/src/main/java/org/traccar/database/MailManager.java @@ -17,6 +17,8 @@ package org.traccar.database; import org.traccar.config.Config; +import org.traccar.config.ConfigKey; +import org.traccar.config.Keys; import org.traccar.model.User; import org.traccar.notification.PropertiesProvider; @@ -37,6 +39,8 @@ import java.util.Properties; public final class MailManager { + private static final String CONTENT_TYPE = "text/html; charset=utf-8"; + private final Config config; private final StatisticsManager statisticsManager; @@ -46,59 +50,48 @@ public final class MailManager { this.statisticsManager = statisticsManager; } - private static Properties getProperties(PropertiesProvider provider) { - Properties properties = new Properties(); - String host = provider.getString("mail.smtp.host"); - if (host != null) { - properties.put("mail.transport.protocol", provider.getString("mail.transport.protocol", "smtp")); - properties.put("mail.smtp.host", host); - properties.put("mail.smtp.port", String.valueOf(provider.getInteger("mail.smtp.port", 25))); - - Boolean starttlsEnable = provider.getBoolean("mail.smtp.starttls.enable"); - if (starttlsEnable != null) { - properties.put("mail.smtp.starttls.enable", String.valueOf(starttlsEnable)); - } - Boolean starttlsRequired = provider.getBoolean("mail.smtp.starttls.required"); - if (starttlsRequired != null) { - properties.put("mail.smtp.starttls.required", String.valueOf(starttlsRequired)); - } - - Boolean sslEnable = provider.getBoolean("mail.smtp.ssl.enable"); - if (sslEnable != null) { - properties.put("mail.smtp.ssl.enable", String.valueOf(sslEnable)); - } - String sslTrust = provider.getString("mail.smtp.ssl.trust"); - if (sslTrust != null) { - properties.put("mail.smtp.ssl.trust", sslTrust); - } + private static void copyBooleanProperty( + Properties properties, PropertiesProvider provider, ConfigKey<Boolean> key) { + Boolean value = provider.getBoolean(key); + if (value != null) { + properties.put(key.getKey(), String.valueOf(value)); + } + } - String sslProtocols = provider.getString("mail.smtp.ssl.protocols"); - if (sslProtocols != null) { - properties.put("mail.smtp.ssl.protocols", sslProtocols); - } + private static void copyStringProperty( + Properties properties, PropertiesProvider provider, ConfigKey<String> key) { + String value = provider.getString(key); + if (value != null) { + properties.put(key.getKey(), value); + } + } - String username = provider.getString("mail.smtp.username"); - if (username != null) { - properties.put("mail.smtp.username", username); - } - String password = provider.getString("mail.smtp.password"); - if (password != null) { - properties.put("mail.smtp.password", password); - } - String from = provider.getString("mail.smtp.from"); - if (from != null) { - properties.put("mail.smtp.from", from); - } - String fromName = provider.getString("mail.smtp.fromName"); - if (fromName != null) { - properties.put("mail.smtp.fromName", fromName); - } + private static Properties getProperties(PropertiesProvider provider) { + String host = provider.getString(Keys.MAIL_SMTP_HOST); + if (host != null) { + Properties properties = new Properties(); + + properties.put(Keys.MAIL_TRANSPORT_PROTOCOL.getKey(), provider.getString(Keys.MAIL_TRANSPORT_PROTOCOL)); + properties.put(Keys.MAIL_SMTP_HOST.getKey(), host); + properties.put(Keys.MAIL_SMTP_PORT.getKey(), String.valueOf(provider.getInteger(Keys.MAIL_SMTP_PORT))); + + copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_STARTTLS_ENABLE); + copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_STARTTLS_REQUIRED); + copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_SSL_ENABLE); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_SSL_TRUST); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_SSL_PROTOCOLS); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_USERNAME); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_PASSWORD); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_FROM); + copyStringProperty(properties, provider, Keys.MAIL_SMTP_FROM_NAME); + + return properties; } - return properties; + return null; } public boolean getEmailEnabled() { - return config.hasKey("mail.smtp.host"); + return config.hasKey(Keys.MAIL_SMTP_HOST); } public void sendMessage( @@ -108,24 +101,25 @@ public final class MailManager { public void sendMessage( User user, String subject, String body, MimeBodyPart attachment) throws MessagingException { + Properties properties = null; - if (!config.getBoolean("mail.smtp.ignoreUserConfig")) { + if (!config.getBoolean(Keys.MAIL_SMTP_IGNORE_USER_CONFIG)) { properties = getProperties(new PropertiesProvider(user)); } - if (properties == null || !properties.containsKey("mail.smtp.host")) { + if (properties == null) { properties = getProperties(new PropertiesProvider(config)); } - if (!properties.containsKey("mail.smtp.host")) { - throw new RuntimeException("No SMTP configuration found"); + if (properties == null) { + throw new MessagingException("No SMTP configuration found"); } Session session = Session.getInstance(properties); MimeMessage message = new MimeMessage(session); - String from = properties.getProperty("mail.smtp.from"); + String from = properties.getProperty(Keys.MAIL_SMTP_FROM.getKey()); if (from != null) { - String fromName = properties.getProperty("mail.smtp.fromName"); + String fromName = properties.getProperty(Keys.MAIL_SMTP_FROM_NAME.getKey()); if (fromName != null) { try { message.setFrom(new InternetAddress(from, fromName)); @@ -145,21 +139,21 @@ public final class MailManager { Multipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); - messageBodyPart.setContent(body, "text/html; charset=utf-8"); + messageBodyPart.setContent(body, CONTENT_TYPE); multipart.addBodyPart(messageBodyPart); multipart.addBodyPart(attachment); message.setContent(multipart); } else { - message.setContent(body, "text/html; charset=utf-8"); + message.setContent(body, CONTENT_TYPE); } try (Transport transport = session.getTransport()) { statisticsManager.registerMail(); transport.connect( - properties.getProperty("mail.smtp.host"), - properties.getProperty("mail.smtp.username"), - properties.getProperty("mail.smtp.password")); + properties.getProperty(Keys.MAIL_SMTP_HOST.getKey()), + properties.getProperty(Keys.MAIL_SMTP_USERNAME.getKey()), + properties.getProperty(Keys.MAIL_SMTP_PASSWORD.getKey())); transport.sendMessage(message, message.getAllRecipients()); } } diff --git a/src/main/java/org/traccar/notification/PropertiesProvider.java b/src/main/java/org/traccar/notification/PropertiesProvider.java index f0078feef..4ffad432c 100644 --- a/src/main/java/org/traccar/notification/PropertiesProvider.java +++ b/src/main/java/org/traccar/notification/PropertiesProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.traccar.notification; import org.traccar.config.Config; +import org.traccar.config.ConfigKey; import org.traccar.model.ExtendedModel; public class PropertiesProvider { @@ -32,36 +33,28 @@ public class PropertiesProvider { this.extendedModel = extendedModel; } - public String getString(String key) { + public String getString(ConfigKey<String> key) { if (config != null) { return config.getString(key); } else { - return extendedModel.getString(key); + return extendedModel.getString(key.getKey()); } } - public String getString(String key, String defaultValue) { - String value = getString(key); - if (value == null) { - value = defaultValue; - } - return value; - } - - public int getInteger(String key, int defaultValue) { + public int getInteger(ConfigKey<Integer> key) { if (config != null) { - return config.getInteger(key, defaultValue); + return config.getInteger(key); } else { - Object result = extendedModel.getAttributes().get(key); + Object result = extendedModel.getAttributes().get(key.getKey()); if (result != null) { return result instanceof String ? Integer.parseInt((String) result) : (Integer) result; } else { - return defaultValue; + return key.getDefaultValue(); } } } - public Boolean getBoolean(String key) { + public Boolean getBoolean(ConfigKey<Boolean> key) { if (config != null) { if (config.hasKey(key)) { return config.getBoolean(key); @@ -69,7 +62,7 @@ public class PropertiesProvider { return null; } } else { - Object result = extendedModel.getAttributes().get(key); + Object result = extendedModel.getAttributes().get(key.getKey()); if (result != null) { return result instanceof String ? Boolean.valueOf((String) result) : (Boolean) result; } else { |