From a25e7bd56c128fb2a1c673abf735b3e64706f9a8 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 26 Jun 2016 00:51:33 +0500 Subject: Added notifications via email Added notifications settings --- .../api/resource/DevicePermissionResource.java | 8 +- src/org/traccar/api/resource/DeviceResource.java | 11 +- .../api/resource/GroupPermissionResource.java | 8 +- src/org/traccar/api/resource/GroupResource.java | 11 +- .../traccar/api/resource/NotificationResource.java | 44 ++++++ src/org/traccar/api/resource/UserResource.java | 21 ++- src/org/traccar/database/DataManager.java | 24 +++ src/org/traccar/database/NotificationManager.java | 139 ++++++++++++++++- src/org/traccar/model/Notification.java | 40 +++++ src/org/traccar/model/User.java | 14 +- .../notification/NotificationFormatter.java | 164 +++++++++++++++++++++ src/org/traccar/notification/NotificationMail.java | 133 +++++++++++++++++ src/org/traccar/web/WebServer.java | 4 +- 13 files changed, 596 insertions(+), 25 deletions(-) create mode 100644 src/org/traccar/api/resource/NotificationResource.java create mode 100644 src/org/traccar/model/Notification.java create mode 100644 src/org/traccar/notification/NotificationFormatter.java create mode 100644 src/org/traccar/notification/NotificationMail.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java index 2ef436f11..7de050074 100644 --- a/src/org/traccar/api/resource/DevicePermissionResource.java +++ b/src/org/traccar/api/resource/DevicePermissionResource.java @@ -38,7 +38,9 @@ public class DevicePermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -47,7 +49,9 @@ public class DevicePermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index d6032bb1e..d6c53dc04 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -60,7 +60,9 @@ public class DeviceResource extends BaseResource { Context.getDataManager().addDevice(entity); Context.getDataManager().linkDevice(getUserId(), entity.getId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -70,6 +72,9 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDataManager().updateDevice(entity); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -80,7 +85,9 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDataManager().removeDevice(id); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java index 564a379d2..1fbf37a2b 100644 --- a/src/org/traccar/api/resource/GroupPermissionResource.java +++ b/src/org/traccar/api/resource/GroupPermissionResource.java @@ -38,7 +38,9 @@ public class GroupPermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -47,7 +49,9 @@ public class GroupPermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index dda9ab03b..957f39ebe 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -59,7 +59,9 @@ public class GroupResource extends BaseResource { Context.getDataManager().addGroup(entity); Context.getDataManager().linkGroup(getUserId(), entity.getId()); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -69,6 +71,9 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDataManager().updateGroup(entity); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.ok(entity).build(); } @@ -79,7 +84,9 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDataManager().removeGroup(id); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java new file mode 100644 index 000000000..236e352e1 --- /dev/null +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -0,0 +1,44 @@ +package org.traccar.api.resource; + +import java.sql.SQLException; +import java.util.Collection; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Notification; + +@Path("users/notifications") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class NotificationResource extends BaseResource { + + @GET + public Collection get(@QueryParam("all") boolean all, + @QueryParam("userId") long userId) throws SQLException { + if (all) { + return Context.getNotificationManager().getAllNotifications(); + } + if (userId == 0) { + userId = getUserId(); + } + Context.getPermissionsManager().checkUser(getUserId(), userId); + return Context.getNotificationManager().getUserNotifications(userId); + } + + @POST + public Response update(Notification entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getNotificationManager().updateNotification(entity); + return Response.ok(entity).build(); + } +} diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 7cc4ed09a..cfe338b56 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -52,7 +52,12 @@ public class UserResource extends BaseResource { } Context.getDataManager().addUser(entity); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } + if (Context.getNotificationManager() != null) { + Context.getNotificationManager().refresh(); + } return Response.ok(entity).build(); } @@ -66,7 +71,12 @@ public class UserResource extends BaseResource { } Context.getDataManager().updateUser(entity); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } + if (Context.getNotificationManager() != null) { + Context.getNotificationManager().refresh(); + } return Response.ok(entity).build(); } @@ -76,7 +86,12 @@ public class UserResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), id); Context.getDataManager().removeUser(id); Context.getPermissionsManager().refresh(); - Context.getGeofenceManager().refresh(); + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refresh(); + } + if (Context.getNotificationManager() != null) { + Context.getNotificationManager().refresh(); + } return Response.noContent().build(); } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 860bf1b84..031703023 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -52,6 +52,7 @@ import org.traccar.model.Geofence; import org.traccar.model.Group; import org.traccar.model.GroupGeofence; import org.traccar.model.GroupPermission; +import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.User; @@ -644,4 +645,27 @@ public class DataManager implements IdentityManager { .setLong("geofenceId", geofenceId) .executeUpdate(); } + + public Collection getNotifications() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectNotifications")) + .executeQuery(Notification.class); + } + + public void addNotification(Notification notification) throws SQLException { + notification.setId(QueryBuilder.create(dataSource, getQuery("database.insertNotification"), true) + .setObject(notification) + .executeUpdate()); + } + + public void updateNotification(Notification notification) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.updateNotification")) + .setObject(notification) + .executeUpdate(); + } + + public void removeNotification(Notification notification) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.deleteNotification")) + .setLong("id", notification.getId()) + .executeUpdate(); + } } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index ab6b2230a..ae91d81cc 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -15,21 +15,35 @@ */ package org.traccar.database; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.sql.SQLException; import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Event; +import org.traccar.model.Notification; import org.traccar.model.Position; +import org.traccar.notification.NotificationMail; public class NotificationManager { private final DataManager dataManager; + private final Map> userNotifications = new HashMap<>(); + + private final ReadWriteLock notificationsLock = new ReentrantReadWriteLock(); + public NotificationManager(DataManager dataManager) { this.dataManager = dataManager; + refresh(); } public void updateEvent(Event event, Position position) { @@ -43,7 +57,15 @@ public class NotificationManager { for (Long userId : users) { if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null && Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { - Context.getConnectionManager().updateEvent(userId, event, position); + Notification notification = getUserNotificationByType(userId, event.getType()); + if (notification != null) { + if (notification.getAttributes().containsKey("web")) { + Context.getConnectionManager().updateEvent(userId, event, position); + } + if (notification.getAttributes().containsKey("mail")) { + NotificationMail.sendMailAsync(userId, event, position); + } + } } } } @@ -54,4 +76,119 @@ public class NotificationManager { updateEvent(event, position); } } + + private Set getUserNotificationsUnsafe(long userId) { + if (!userNotifications.containsKey(userId)) { + userNotifications.put(userId, new HashSet()); + } + return userNotifications.get(userId); + } + + public Set getUserNotifications(long userId) { + notificationsLock.readLock().lock(); + try { + return getUserNotificationsUnsafe(userId); + } finally { + notificationsLock.readLock().unlock(); + } + } + + public final void refresh() { + if (dataManager != null) { + try { + notificationsLock.writeLock().lock(); + try { + userNotifications.clear(); + for (Notification notification : dataManager.getNotifications()) { + getUserNotificationsUnsafe(notification.getUserId()).add(notification); + } + } finally { + notificationsLock.writeLock().unlock(); + } + } catch (SQLException error) { + Log.warning(error); + } + } + } + + public Notification getUserNotificationByType(long userId, String type) { + notificationsLock.readLock().lock(); + try { + for (Notification notification : getUserNotificationsUnsafe(userId)) { + if (notification.getType().equals(type)) { + return notification; + } + } + } finally { + notificationsLock.readLock().unlock(); + } + return null; + } + + public void updateNotification(Notification notification) { + Notification cachedNotification = getUserNotificationByType(notification.getUserId(), notification.getType()); + if (cachedNotification != null) { + if (!cachedNotification.getAttributes().equals(notification.getAttributes())) { + if (notification.getAttributes().isEmpty()) { + try { + dataManager.removeNotification(cachedNotification); + } catch (SQLException error) { + Log.warning(error); + } + notificationsLock.writeLock().lock(); + try { + getUserNotificationsUnsafe(notification.getUserId()).remove(cachedNotification); + } finally { + notificationsLock.writeLock().unlock(); + } + } else { + notificationsLock.writeLock().lock(); + try { + cachedNotification.setAttributes(notification.getAttributes()); + } finally { + notificationsLock.writeLock().unlock(); + } + try { + dataManager.updateNotification(cachedNotification); + } catch (SQLException error) { + Log.warning(error); + } + } + } else { + notification.setId(cachedNotification.getId()); + } + } else if (!notification.getAttributes().isEmpty()) { + try { + dataManager.addNotification(notification); + } catch (SQLException error) { + Log.warning(error); + } + notificationsLock.writeLock().lock(); + try { + getUserNotificationsUnsafe(notification.getUserId()).add(notification); + } finally { + notificationsLock.writeLock().unlock(); + } + } + } + + public Set getAllNotifications() { + + Set notifications = new HashSet<>(); + long id = 0; + Field[] fields = Event.class.getDeclaredFields(); + for (Field field : fields) { + if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) { + try { + Notification notification = new Notification(); + notification.setType(field.get(null).toString()); + notification.setId(id++); + notifications.add(notification); + } catch (IllegalArgumentException | IllegalAccessException error) { + Log.warning(error); + } + } + } + return notifications; + } } diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java new file mode 100644 index 000000000..0664d6a00 --- /dev/null +++ b/src/org/traccar/model/Notification.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.model; + +public class Notification extends Extensible { + + private long userId; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + private String type; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + +} diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 287de121c..aa73cfcff 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,7 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.helper.Hashing; -public class User { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class User extends Extensible { private String name; diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java new file mode 100644 index 000000000..edfa70f4d --- /dev/null +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -0,0 +1,164 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.notification; + +import java.util.Formatter; +import java.util.Locale; + +import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public final class NotificationFormatter { + + private NotificationFormatter() { + } + + public static final String TITLE_TEMPLATE_TYPE_COMMAND_RESULT = "%1$s: command result received"; + public static final String MESSAGE_TEMPLATE_TYPE_COMMAND_RESULT = "Device: %1$s%n" + + "Result: %3$s%n" + + "Time: %2$tc%n"; + + public static final String TITLE_TEMPLATE_TYPE_DEVICE_ONLINE = "%1$s: online"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE = "Device: %1$s%n" + + "Online%n" + + "Time: %2$tc%n"; + public static final String TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE = "%1$s: offline"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE = "Device: %1$s%n" + + "Offline%n" + + "Time: %2$tc%n"; + + public static final String TITLE_TEMPLATE_TYPE_DEVICE_MOVING = "%1$s: moving"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING = "Device: %1$s%n" + + "Moving%n" + + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; + public static final String TITLE_TEMPLATE_TYPE_DEVICE_STOPPED = "%1$s: stopped"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED = "Device: %1$s%n" + + "Stopped%n" + + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; + + public static final String TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "%1$s: exeeds the speed"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "Device: %1$s%n" + + "Exeeds the speed: %5$f%n" + + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; + + public static final String TITLE_TEMPLATE_TYPE_GEOFENCE_ENTER = "%1$s: has entered geofence"; + public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER = "Device: %1$s%n" + + "Has entered geofence: %5$s%n" + + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; + public static final String TITLE_TEMPLATE_TYPE_GEOFENCE_EXIT = "%1$s: has exited geofence"; + public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT = "Device: %1$s%n" + + "Has exited geofence: %5$s%n" + + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; + + public static String formatTitle(long userId, Event event, Position position) { + Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); + StringBuilder stringBuilder = new StringBuilder(); + 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; + } + String result = formatter.toString(); + formatter.close(); + return result; + } + + public static String formatMessage(long userId, Event event, Position position) { + Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); + StringBuilder stringBuilder = new StringBuilder(); + 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; + } + String result = formatter.toString(); + formatter.close(); + return result; + } +} diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java new file mode 100644 index 000000000..7189b531b --- /dev/null +++ b/src/org/traccar/notification/NotificationMail.java @@ -0,0 +1,133 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.notification; + +import java.sql.SQLException; +import java.util.Properties; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.traccar.Config; +import org.traccar.Context; +import org.traccar.database.DataManager; +import org.traccar.helper.Log; +import org.traccar.model.Event; +import org.traccar.model.Position; +import org.traccar.model.User; + +public final class NotificationMail { + + private NotificationMail() { + } + + public static void sendMailSync(long userId, Event event, Position position) { + Config config = Context.getConfig(); + DataManager dataManager = Context.getDataManager(); + + Properties mailServerProperties; + Session mailSession; + MimeMessage mailMessage; + + String from = null; + String username = null; + 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) { + 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; + } + + mailSession = Session.getDefaultInstance(mailServerProperties, null); + + 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(); + + } catch (MessagingException | SQLException error) { + Log.warning(error); + } + } + + public static void sendMailAsync(final long userId, final Event event, final Position position) { + Runnable runnableSend = new Runnable() { + public void run() { + sendMailSync(userId, event, position); + } + }; + + new Thread(runnableSend).start(); + } +} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index c06ee5d35..5527e80f3 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -41,6 +41,7 @@ import org.traccar.api.resource.SessionResource; import org.traccar.api.resource.DevicePermissionResource; import org.traccar.api.resource.UserResource; import org.traccar.api.resource.GroupResource; +import org.traccar.api.resource.NotificationResource; import org.traccar.api.resource.DeviceResource; import org.traccar.api.resource.PositionResource; import org.traccar.api.resource.CommandTypeResource; @@ -155,7 +156,8 @@ public class WebServer { GroupPermissionResource.class, DevicePermissionResource.class, UserResource.class, GroupResource.class, DeviceResource.class, PositionResource.class, CommandTypeResource.class, EventResource.class, GeofenceResource.class, - DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class); + DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class, + NotificationResource.class); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); -- cgit v1.2.3 From 6c0c508ef8afe602a12dbeb2b68ea708fdffedd2 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 26 Jun 2016 00:56:19 +0500 Subject: Fixed missed license and wrong EOL --- .../traccar/api/resource/NotificationResource.java | 103 ++++++++++++--------- 1 file changed, 59 insertions(+), 44 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 236e352e1..5bec7fd85 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -1,44 +1,59 @@ -package org.traccar.api.resource; - -import java.sql.SQLException; -import java.util.Collection; - -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.Notification; - -@Path("users/notifications") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class NotificationResource extends BaseResource { - - @GET - public Collection get(@QueryParam("all") boolean all, - @QueryParam("userId") long userId) throws SQLException { - if (all) { - return Context.getNotificationManager().getAllNotifications(); - } - if (userId == 0) { - userId = getUserId(); - } - Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getNotificationManager().getUserNotifications(userId); - } - - @POST - public Response update(Notification entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getNotificationManager().updateNotification(entity); - return Response.ok(entity).build(); - } -} +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import java.sql.SQLException; +import java.util.Collection; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Notification; + +@Path("users/notifications") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class NotificationResource extends BaseResource { + + @GET + public Collection get(@QueryParam("all") boolean all, + @QueryParam("userId") long userId) throws SQLException { + if (all) { + return Context.getNotificationManager().getAllNotifications(); + } + if (userId == 0) { + userId = getUserId(); + } + Context.getPermissionsManager().checkUser(getUserId(), userId); + return Context.getNotificationManager().getUserNotifications(userId); + } + + @POST + public Response update(Notification entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getNotificationManager().updateNotification(entity); + return Response.ok(entity).build(); + } +} -- cgit v1.2.3 From 395779143dd61df7ba526ee7a80a91094291a30b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 27 Jun 2016 15:13:10 +0500 Subject: - Removed commented parameters from release configs - Style fixes --- setup/unix/traccar.xml | 14 --- setup/windows/traccar.xml | 14 --- .../notification/NotificationFormatter.java | 130 ++++++++++----------- src/org/traccar/notification/NotificationMail.java | 110 ++++++++--------- 4 files changed, 117 insertions(+), 151 deletions(-) (limited to 'src/org/traccar') 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 @@ true - - org.h2.Driver 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 @@ true - - org.h2.Driver 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); -- cgit v1.2.3 From 85987e35e6f331e60ac1bdeb43668356809b9b90 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 27 Jun 2016 18:38:47 +0500 Subject: Move some code to functions --- src/org/traccar/notification/NotificationMail.java | 117 +++++++++++---------- 1 file changed, 64 insertions(+), 53 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 7033c4a11..443018221 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -30,6 +30,7 @@ import org.traccar.Context; import org.traccar.database.DataManager; import org.traccar.helper.Log; import org.traccar.model.Event; +import org.traccar.model.Extensible; import org.traccar.model.Position; import org.traccar.model.User; @@ -38,81 +39,91 @@ public final class NotificationMail { private NotificationMail() { } - public static void sendMailSync(long userId, Event event, Position position) { + private static Properties getConfigProperies() { Config config = Context.getConfig(); + Properties result = new Properties(); + String host = config.getString("mail.smtp.host", null); + if (host != null) { + result.put("mail.smtp.host", host); + result.put("mail.smtp.port", config.getString("mail.smtp.port", "25")); + + if (config.getBoolean("mail.smtp.starttls.enable")) { + result.put("mail.smtp.starttls.enable", + config.getBoolean("mail.smtp.starttls.enable")); + } else if (config.getBoolean("mail.smtp.ssl.enable")) { + result.put("mail.smtp.socketFactory.port", + result.getProperty("mail.smtp.port")); + result.put("mail.smtp.socketFactory.class", + "javax.net.ssl.SSLSocketFactory"); + } + + result.put("mail.smtp.auth", config.getBoolean("mail.smtp.auth")); + result.put("mail.smtp.user", config.getString("mail.smtp.username", null)); + result.put("mail.smtp.password", config.getString("mail.smtp.password", null)); + result.put("mail.smtp.from", config.getString("mail.smtp.from", null)); + } + return result; + } + + private static Properties getAttributesProperties(Extensible object) { + Properties result = new Properties(); + + if (object.getAttributes().containsKey("mail.smtp.host")) { + result.put("mail.smtp.host", object.getAttributes().get("mail.smtp.host")); + String port = (String) object.getAttributes().get("mail.smtp.port"); + result.put("mail.smtp.port", (port != null) ? port : "25"); + if (object.getAttributes().containsKey("mail.smtp.starttls.enable")) { + boolean tls = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.starttls.enable")); + result.put("mail.smtp.starttls.enable", tls); + } else if (object.getAttributes().containsKey("mail.smtp.ssl.enable")) { + boolean ssl = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.ssl.enable")); + if (ssl) { + result.put("mail.smtp.socketFactory.port", + result.getProperty("mail.smtp.port")); + result.put("mail.smtp.socketFactory.class", + "javax.net.ssl.SSLSocketFactory"); + } + } + boolean auth = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.auth")); + result.put("mail.smtp.auth", auth); + + result.put("mail.smtp.username", object.getAttributes().get("mail.smtp.username")); + result.put("mail.smtp.password", object.getAttributes().get("mail.smtp.password")); + result.put("mail.smtp.from", object.getAttributes().get("mail.smtp.from")); + } + return result; + } + + public static void sendMailSync(long userId, Event event, Position position) { DataManager dataManager = Context.getDataManager(); Properties mailServerProperties; Session mailSession; MimeMessage mailMessage; - String from = null; - String username = null; - 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) { - mailServerProperties.put("mail.smtp.socketFactory.port", - mailServerProperties.getProperty("mail.smtp.port")); - mailServerProperties.put("mail.smtp.socketFactory.class", - "javax.net.ssl.SSLSocketFactory"); - } + mailServerProperties = getConfigProperies(); + if (!mailServerProperties.containsKey("mail.smtp.host")) { + mailServerProperties = getAttributesProperties(user); + if (!mailServerProperties.containsKey("mail.smtp.host")) { + 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); 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.connect(mailServerProperties.getProperty("mail.smtp.host"), + mailServerProperties.getProperty("mail.smtp.username"), + mailServerProperties.getProperty("mail.smtp.password")); transport.sendMessage(mailMessage, mailMessage.getAllRecipients()); transport.close(); -- cgit v1.2.3 From 61b2486353c742afe6214f0a0c5c2e9956ea3b97 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 28 Jun 2016 07:06:51 +0500 Subject: - Removed unlikely check - Fixed indentation --- src/org/traccar/notification/NotificationFormatter.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 2b648ef4c..37bd7848c 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -127,25 +127,25 @@ public final class NotificationFormatter { break; case Event.TYPE_DEVICE_MOVING: formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName(), position.getFixTime(), - position.getLatitude(), position.getLongitude()); + 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()); + 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()); + 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() : ""); + position.getLatitude(), position.getLongitude(), + 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() : ""); + position.getLatitude(), position.getLongitude(), + Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName()); break; default: formatter.format("Unknown type"); -- cgit v1.2.3