diff options
-rw-r--r-- | setup/unix/traccar.xml | 14 | ||||
-rw-r--r-- | setup/windows/traccar.xml | 14 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 130 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 110 |
4 files changed, 117 insertions, 151 deletions
diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index 890229d00..c5759f87f 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -28,20 +28,6 @@ <entry key='event.geofenceHandler'>true</entry> - <!--<entry key='mail.smtp.host'>smtp.example.com</entry> - for STARTTLS - <entry key='mail.smtp.port'>587</entry> - <entry key='mail.smtp.starttls.enable'>true</entry> - for SSL - <entry key='mail.smtp.port'>465</entry> - <entry key='mail.smtp.ssl.enable'>true</entry> - - <entry key='mail.smtp.from'>traccar@example.com</entry> - - <entry key='mail.smtp.auth'>true</entry> - <entry key='mail.smtp.username'>traccar@example.com</entry> - <entry key='mail.smtp.password'>password</entry>--> - <!-- DATABASE CONFIG --> <entry key='database.driver'>org.h2.Driver</entry> diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index 0f50dc0b2..1d573f282 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -28,20 +28,6 @@ <entry key='event.geofenceHandler'>true</entry>
- <!--<entry key='mail.smtp.host'>smtp.example.com</entry>
- for STARTTLS
- <entry key='mail.smtp.port'>587</entry>
- <entry key='mail.smtp.starttls.enable'>true</entry>
- for SSL
- <entry key='mail.smtp.port'>465</entry>
- <entry key='mail.smtp.ssl.enable'>true</entry>
-
- <entry key='mail.smtp.from'>traccar@example.com</entry>
-
- <entry key='mail.smtp.auth'>true</entry>
- <entry key='mail.smtp.username'>traccar@example.com</entry>
- <entry key='mail.smtp.password'>password</entry>-->
-
<!-- DATABASE CONFIG -->
<entry key='database.driver'>org.h2.Driver</entry>
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index edfa70f4d..2b648ef4c 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -76,24 +76,33 @@ public final class NotificationFormatter { Formatter formatter = new Formatter(stringBuilder, Locale.getDefault()); switch (event.getType()) { - case Event.TYPE_COMMAND_RESULT : formatter.format(TITLE_TEMPLATE_TYPE_COMMAND_RESULT, device.getName()); - break; - case Event.TYPE_DEVICE_ONLINE : formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName()); - break; - case Event.TYPE_DEVICE_OFFLINE : formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName()); - break; - case Event.TYPE_DEVICE_MOVING : formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName()); - break; - case Event.TYPE_DEVICE_STOPPED : formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_STOPPED, device.getName()); - break; - case Event.TYPE_DEVICE_OVERSPEED: formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName()); - break; - case Event.TYPE_GEOFENCE_ENTER : formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName()); - break; - case Event.TYPE_GEOFENCE_EXIT : formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_EXIT, device.getName()); - break; - default : formatter.format("Unknown type"); - break; + case Event.TYPE_COMMAND_RESULT: + formatter.format(TITLE_TEMPLATE_TYPE_COMMAND_RESULT, device.getName()); + break; + case Event.TYPE_DEVICE_ONLINE: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName()); + break; + case Event.TYPE_DEVICE_OFFLINE: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName()); + break; + case Event.TYPE_DEVICE_MOVING: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName()); + break; + case Event.TYPE_DEVICE_STOPPED: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_STOPPED, device.getName()); + break; + case Event.TYPE_DEVICE_OVERSPEED: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName()); + break; + case Event.TYPE_GEOFENCE_ENTER: + formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName()); + break; + case Event.TYPE_GEOFENCE_EXIT: + formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_EXIT, device.getName()); + break; + default: + formatter.format("Unknown type"); + break; } String result = formatter.toString(); formatter.close(); @@ -106,56 +115,41 @@ public final class NotificationFormatter { Formatter formatter = new Formatter(stringBuilder, Locale.getDefault()); switch (event.getType()) { - case Event.TYPE_COMMAND_RESULT : formatter.format(MESSAGE_TEMPLATE_TYPE_COMMAND_RESULT, - device.getName(), - event.getServerTime(), - position.getAttributes().get("result")); - break; - case Event.TYPE_DEVICE_ONLINE : formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE, - device.getName(), - event.getServerTime()); - break; - case Event.TYPE_DEVICE_OFFLINE : formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE, - device.getName(), - event.getServerTime()); - break; - case Event.TYPE_DEVICE_MOVING : formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING, - device.getName(), - position.getFixTime(), - position.getLatitude(), - position.getLongitude()); - break; - case Event.TYPE_DEVICE_STOPPED : formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED, - device.getName(), - position.getFixTime(), - position.getLatitude(), - position.getLongitude()); - break; - case Event.TYPE_DEVICE_OVERSPEED: formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED, - device.getName(), - position.getFixTime(), - position.getLatitude(), - position.getLongitude(), - position.getSpeed()); - break; - case Event.TYPE_GEOFENCE_ENTER : formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER, - device.getName(), - position.getFixTime(), - position.getLatitude(), - position.getLongitude(), - (Context.getGeofenceManager() != null) - ? Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName() : ""); - break; - case Event.TYPE_GEOFENCE_EXIT : formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT, - device.getName(), - position.getFixTime(), - position.getLatitude(), - position.getLongitude(), - (Context.getGeofenceManager() != null) - ? Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName() : ""); - break; - default : formatter.format("Unknown type"); - break; + case Event.TYPE_COMMAND_RESULT: + formatter.format(MESSAGE_TEMPLATE_TYPE_COMMAND_RESULT, device.getName(), event.getServerTime(), + position.getAttributes().get("result")); + break; + case Event.TYPE_DEVICE_ONLINE: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName(), event.getServerTime()); + break; + case Event.TYPE_DEVICE_OFFLINE: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName(), event.getServerTime()); + break; + case Event.TYPE_DEVICE_MOVING: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude()); + break; + case Event.TYPE_DEVICE_STOPPED: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude()); + break; + case Event.TYPE_DEVICE_OVERSPEED: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude(), position.getSpeed()); + break; + case Event.TYPE_GEOFENCE_ENTER: + formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude(), Context.getGeofenceManager() != null + ? Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName() : ""); + break; + case Event.TYPE_GEOFENCE_EXIT: + formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude(), Context.getGeofenceManager() != null + ? Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName() : ""); + break; + default: + formatter.format("Unknown type"); + break; } String result = formatter.toString(); formatter.close(); diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 7189b531b..7033c4a11 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -51,70 +51,70 @@ public final class NotificationMail { String password = null; try { - User user = dataManager.getUser(userId); - - mailServerProperties = new Properties(); - String host = config.getString("mail.smtp.host", null); - if (host != null) { - mailServerProperties.put("mail.smtp.host", host); - mailServerProperties.put("mail.smtp.port", config.getString("mail.smtp.port", "25")); - - if (config.getBoolean("mail.smtp.starttls.enable")) { - mailServerProperties.put("mail.smtp.starttls.enable", - config.getBoolean("mail.smtp.starttls.enable")); - } else if (config.getBoolean("mail.smtp.ssl.enable")) { - mailServerProperties.put("mail.smtp.socketFactory.port", - mailServerProperties.getProperty("mail.smtp.port")); - mailServerProperties.put("mail.smtp.socketFactory.class", - "javax.net.ssl.SSLSocketFactory"); - } - - mailServerProperties.put("mail.smtp.auth", config.getBoolean("mail.smtp.auth")); - username = config.getString("mail.smtp.username", null); - password = config.getString("mail.smtp.password", null); - from = config.getString("mail.smtp.from", null); - } else if (user.getAttributes().containsKey("mail.smtp.host")) { - mailServerProperties.put("mail.smtp.host", user.getAttributes().get("mail.smtp.host")); - String port = (String) user.getAttributes().get("mail.smtp.port"); - mailServerProperties.put("mail.smtp.port", (port != null) ? port : "25"); - if (user.getAttributes().containsKey("mail.smtp.starttls.enable")) { - boolean tls = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.starttls.enable")); - mailServerProperties.put("mail.smtp.starttls.enable", tls); - } else if (user.getAttributes().containsKey("mail.smtp.ssl.enable")) { - boolean ssl = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.ssl.enable")); - if (ssl) { + User user = dataManager.getUser(userId); + + mailServerProperties = new Properties(); + String host = config.getString("mail.smtp.host", null); + if (host != null) { + mailServerProperties.put("mail.smtp.host", host); + mailServerProperties.put("mail.smtp.port", config.getString("mail.smtp.port", "25")); + + if (config.getBoolean("mail.smtp.starttls.enable")) { + mailServerProperties.put("mail.smtp.starttls.enable", + config.getBoolean("mail.smtp.starttls.enable")); + } else if (config.getBoolean("mail.smtp.ssl.enable")) { mailServerProperties.put("mail.smtp.socketFactory.port", mailServerProperties.getProperty("mail.smtp.port")); mailServerProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); } + + mailServerProperties.put("mail.smtp.auth", config.getBoolean("mail.smtp.auth")); + username = config.getString("mail.smtp.username", null); + password = config.getString("mail.smtp.password", null); + from = config.getString("mail.smtp.from", null); + } else if (user.getAttributes().containsKey("mail.smtp.host")) { + mailServerProperties.put("mail.smtp.host", user.getAttributes().get("mail.smtp.host")); + String port = (String) user.getAttributes().get("mail.smtp.port"); + mailServerProperties.put("mail.smtp.port", (port != null) ? port : "25"); + if (user.getAttributes().containsKey("mail.smtp.starttls.enable")) { + boolean tls = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.starttls.enable")); + mailServerProperties.put("mail.smtp.starttls.enable", tls); + } else if (user.getAttributes().containsKey("mail.smtp.ssl.enable")) { + boolean ssl = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.ssl.enable")); + if (ssl) { + mailServerProperties.put("mail.smtp.socketFactory.port", + mailServerProperties.getProperty("mail.smtp.port")); + mailServerProperties.put("mail.smtp.socketFactory.class", + "javax.net.ssl.SSLSocketFactory"); + } + } + boolean auth = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.auth")); + mailServerProperties.put("mail.smtp.auth", auth); + + username = (String) user.getAttributes().get("mail.smtp.username"); + password = (String) user.getAttributes().get("mail.smtp.password"); + from = (String) user.getAttributes().get("mail.smtp.from"); + } else { + return; } - boolean auth = Boolean.parseBoolean((String) user.getAttributes().get("mail.smtp.auth")); - mailServerProperties.put("mail.smtp.auth", auth); - - username = (String) user.getAttributes().get("mail.smtp.username"); - password = (String) user.getAttributes().get("mail.smtp.password"); - from = (String) user.getAttributes().get("mail.smtp.from"); - } else { - return; - } - mailSession = Session.getDefaultInstance(mailServerProperties, null); + mailSession = Session.getDefaultInstance(mailServerProperties, null); - mailMessage = new MimeMessage(mailSession); + mailMessage = new MimeMessage(mailSession); - if (from != null) { - mailMessage.setFrom(new InternetAddress(from)); - } - mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress( - Context.getDataManager().getUser(userId).getEmail())); - mailMessage.setSubject(NotificationFormatter.formatTitle(userId, event, position)); - mailMessage.setText(NotificationFormatter.formatMessage(userId, event, position)); - - Transport transport = mailSession.getTransport("smtp"); - transport.connect(mailServerProperties.getProperty("mail.smtp.host"), username, password); - transport.sendMessage(mailMessage, mailMessage.getAllRecipients()); - transport.close(); + if (from != null) { + mailMessage.setFrom(new InternetAddress(from)); + } + mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress( + Context.getDataManager().getUser(userId).getEmail())); + mailMessage.setSubject(NotificationFormatter.formatTitle(userId, event, position)); + mailMessage.setText(NotificationFormatter.formatMessage(userId, event, position)); + + Transport transport = mailSession.getTransport("smtp"); + transport.connect(mailServerProperties.getProperty("mail.smtp.host"), username, password); + transport.sendMessage(mailMessage, mailMessage.getAllRecipients()); + transport.close(); } catch (MessagingException | SQLException error) { Log.warning(error); |