diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-07-27 18:29:03 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-27 18:29:03 +1200 |
commit | da0e33ee765053e14b7172bae0f3acd74c2e26eb (patch) | |
tree | f099eff33a6bcd3917add19fec9fce7e849e819f /src/org/traccar/notification | |
parent | e4ec06498f63ca31d8fd7332523bd997f3fe44a0 (diff) | |
parent | 47a5fc014282314ef8c2b719a961c01c6cd2b822 (diff) | |
download | trackermap-server-da0e33ee765053e14b7172bae0f3acd74c2e26eb.tar.gz trackermap-server-da0e33ee765053e14b7172bae0f3acd74c2e26eb.tar.bz2 trackermap-server-da0e33ee765053e14b7172bae0f3acd74c2e26eb.zip |
Merge pull request #3984 from Abyss777/organize_notificators
Move notificators to appropriate package and rename.
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 132 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationNull.java | 34 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 57 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationWeb.java | 30 | ||||
-rw-r--r-- | src/org/traccar/notification/Notificator.java | 40 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificatorManager.java | 10 |
6 files changed, 6 insertions, 297 deletions
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java deleted file mode 100644 index c2ee67299..000000000 --- a/src/org/traccar/notification/NotificationMail.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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.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 java.util.Date; - -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.Event; -import org.traccar.model.Position; -import org.traccar.model.User; - -public final class NotificationMail extends Notificator { - - 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); - } - - String sslProtocols = provider.getString("mail.smtp.ssl.protocols"); - if (sslProtocols != null) { - properties.put("mail.smtp.ssl.protocols", sslProtocols); - } - - 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); - } - } - return properties; - } - - @Override - public void sendSync(long userId, Event event, Position position) throws MessageException { - User user = Context.getPermissionsManager().getUser(userId); - - Properties properties = null; - if (!Context.getConfig().getBoolean("mail.smtp.ignoreUserConfig")) { - properties = getProperties(new PropertiesProvider(user)); - } - if (properties == null || !properties.containsKey("mail.smtp.host")) { - properties = getProperties(new PropertiesProvider(Context.getConfig())); - } - if (!properties.containsKey("mail.smtp.host")) { - Log.warning("No SMTP configuration found"); - return; - } - - Session session = Session.getInstance(properties); - - MimeMessage message = new MimeMessage(session); - - try { - String from = properties.getProperty("mail.smtp.from"); - if (from != null) { - message.setFrom(new InternetAddress(from)); - } - - message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - FullMessage fullMessage = NotificationFormatter.formatFullMessage(userId, event, position); - message.setSubject(fullMessage.getSubject()); - message.setSentDate(new Date()); - message.setContent(fullMessage.getBody(), "text/html; charset=utf-8"); - - Transport transport = session.getTransport(); - try { - Context.getStatisticsManager().registerMail(); - transport.connect( - properties.getProperty("mail.smtp.host"), - properties.getProperty("mail.smtp.username"), - properties.getProperty("mail.smtp.password")); - transport.sendMessage(message, message.getAllRecipients()); - } finally { - transport.close(); - } - } catch (MessagingException e) { - throw new MessageException(e); - } - } - -} diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java deleted file mode 100644 index 3ee954c24..000000000 --- a/src/org/traccar/notification/NotificationNull.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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 org.traccar.helper.Log; -import org.traccar.model.Event; -import org.traccar.model.Position; - -public final class NotificationNull extends Notificator { - - @Override - public void sendAsync(long userId, Event event, Position position) { - Log.warning("You are using null notificatior, please check your configuration, notification not sent"); - } - - @Override - public void sendSync(long userId, Event event, Position position) { - Log.warning("You are using null notificatior, please check your configuration, notification not sent"); - } -} diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java deleted file mode 100644 index ed651ac11..000000000 --- a/src/org/traccar/notification/NotificationSms.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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 org.traccar.Context; -import org.traccar.model.Event; -import org.traccar.model.Position; -import org.traccar.model.User; -import org.traccar.sms.SmsManager; - -public final class NotificationSms extends Notificator { - - private final SmsManager smsManager; - - public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { - final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); - if (smsClass.length() > 0) { - smsManager = (SmsManager) Class.forName(smsClass).newInstance(); - } else { - smsManager = Context.getSmsManager(); - } - } - - @Override - public void sendAsync(long userId, Event event, Position position) { - final User user = Context.getPermissionsManager().getUser(userId); - if (user.getPhone() != null) { - Context.getStatisticsManager().registerSms(); - smsManager.sendMessageAsync(user.getPhone(), - NotificationFormatter.formatShortMessage(userId, event, position), false); - } - } - - @Override - public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { - final User user = Context.getPermissionsManager().getUser(userId); - if (user.getPhone() != null) { - Context.getStatisticsManager().registerSms(); - smsManager.sendMessageSync(user.getPhone(), - NotificationFormatter.formatShortMessage(userId, event, position), false); - } - } -} diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java deleted file mode 100644 index afc401d24..000000000 --- a/src/org/traccar/notification/NotificationWeb.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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 org.traccar.Context; -import org.traccar.model.Event; -import org.traccar.model.Position; - -public final class NotificationWeb extends Notificator { - - @Override - public void sendSync(long userId, Event event, Position position) { - Context.getConnectionManager().updateEvent(userId, event); - } - -} diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java deleted file mode 100644 index d912b445d..000000000 --- a/src/org/traccar/notification/Notificator.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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 org.traccar.helper.Log; -import org.traccar.model.Event; -import org.traccar.model.Position; - -public abstract class Notificator { - - public void sendAsync(final long userId, final Event event, final Position position) { - new Thread(new Runnable() { - public void run() { - try { - sendSync(userId, event, position); - } catch (MessageException | InterruptedException error) { - Log.warning(error); - } - } - }).start(); - } - - public abstract void sendSync(long userId, Event event, Position position) - throws MessageException, InterruptedException; - -} diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 147de47d3..a3b13ef25 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -24,15 +24,17 @@ import java.util.Set; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Typed; +import org.traccar.notificators.NotificatorNull; +import org.traccar.notificators.Notificator; public final class NotificatorManager { - private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notification.NotificationWeb"; - private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notification.NotificationMail"; - private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notification.NotificationSms"; + private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notificators.NotificatorWeb"; + private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notificators.NotificatorMail"; + private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notificators.NotificatorSms"; private final Map<String, Notificator> notificators = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + private static final Notificator NULL_NOTIFICATOR = new NotificatorNull(); public NotificatorManager() { final String[] types = Context.getConfig().getString("notificator.types", "").split(","); |