diff options
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 91 | ||||
-rw-r--r-- | src/org/traccar/notification/MailMessage.java | 36 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 98 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 142 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 52 | ||||
-rw-r--r-- | src/org/traccar/notification/PropertiesProvider.java | 64 |
6 files changed, 0 insertions, 483 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java deleted file mode 100644 index ac37f980c..000000000 --- a/src/org/traccar/notification/EventForwarder.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2016 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. - * 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 com.fasterxml.jackson.core.JsonProcessingException; -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.Device; -import org.traccar.model.Event; -import org.traccar.model.Geofence; -import org.traccar.model.Position; - -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -public final class EventForwarder { - - private String url; - private String header; - - public EventForwarder() { - url = Context.getConfig().getString("event.forward.url", "http://localhost/"); - header = Context.getConfig().getString("event.forward.header", ""); - } - - private static final String KEY_POSITION = "position"; - private static final String KEY_EVENT = "event"; - private static final String KEY_GEOFENCE = "geofence"; - private static final String KEY_DEVICE = "device"; - - public void forwardEvent(Event event, Position position) { - - BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); - - requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); - if (!header.equals("")) { - String[] headerLines = header.split("\\r?\\n"); - for (String headerLine: headerLines) { - String[] splitedLine = headerLine.split(":", 2); - if (splitedLine.length == 2) { - requestBuilder.setHeader(splitedLine[0].trim(), splitedLine[1].trim()); - } - } - } - - requestBuilder.setBody(preparePayload(event, position)); - requestBuilder.execute(); - } - - private byte[] preparePayload(Event event, Position position) { - Map<String, Object> data = new HashMap<>(); - data.put(KEY_EVENT, event); - if (position != null) { - data.put(KEY_POSITION, position); - } - if (event.getDeviceId() != 0) { - Device device = Context.getIdentityManager().getById(event.getDeviceId()); - if (device != null) { - data.put(KEY_DEVICE, device); - } - } - if (event.getGeofenceId() != 0) { - Geofence geofence = (Geofence) Context.getGeofenceManager().getById(event.getGeofenceId()); - if (geofence != null) { - data.put(KEY_GEOFENCE, geofence); - } - } - try { - return Context.getObjectMapper().writeValueAsString(data).getBytes(StandardCharsets.UTF_8); - } catch (JsonProcessingException e) { - Log.warning(e); - return null; - } - } - -} diff --git a/src/org/traccar/notification/MailMessage.java b/src/org/traccar/notification/MailMessage.java deleted file mode 100644 index 0fce43740..000000000 --- a/src/org/traccar/notification/MailMessage.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 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; - -public class MailMessage { - - private String subject; - private String body; - - public MailMessage(String subject, String body) { - this.subject = subject; - this.body = body; - } - - public String getSubject() { - return subject; - } - - public String getBody() { - return body; - } -} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java deleted file mode 100644 index 8da819430..000000000 --- a/src/org/traccar/notification/NotificationFormatter.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.Locale; - -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.exception.ResourceNotFoundException; -import org.apache.velocity.tools.generic.DateTool; -import org.apache.velocity.tools.generic.NumberTool; -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.Device; -import org.traccar.model.Event; -import org.traccar.model.Position; -import org.traccar.model.User; -import org.traccar.reports.ReportUtils; - -public final class NotificationFormatter { - - private NotificationFormatter() { - } - - public static VelocityContext prepareContext(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); - Device device = Context.getIdentityManager().getById(event.getDeviceId()); - - VelocityContext velocityContext = new VelocityContext(); - velocityContext.put("user", user); - velocityContext.put("device", device); - velocityContext.put("event", event); - if (position != null) { - velocityContext.put("position", position); - velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId)); - velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId)); - velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId)); - } - if (event.getGeofenceId() != 0) { - velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); - } - String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); - if (driverUniqueId != null) { - velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId)); - } - velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); - velocityContext.put("dateTool", new DateTool()); - velocityContext.put("numberTool", new NumberTool()); - velocityContext.put("timezone", ReportUtils.getTimezone(userId)); - velocityContext.put("locale", Locale.getDefault()); - return velocityContext; - } - - public static Template getTemplate(Event event, String path) { - Template template; - try { - template = Context.getVelocityEngine().getTemplate(path + event.getType() + ".vm", - StandardCharsets.UTF_8.name()); - } catch (ResourceNotFoundException error) { - Log.warning(error); - template = Context.getVelocityEngine().getTemplate(path + "unknown.vm", - StandardCharsets.UTF_8.name()); - } - return template; - } - - public static MailMessage formatMailMessage(long userId, Event event, Position position) { - VelocityContext velocityContext = prepareContext(userId, event, position); - StringWriter writer = new StringWriter(); - getTemplate(event, Context.getConfig().getString("mail.templatesPath", "mail") + "/") - .merge(velocityContext, writer); - return new MailMessage((String) velocityContext.get("subject"), writer.toString()); - } - - public static String formatSmsMessage(long userId, Event event, Position position) { - VelocityContext velocityContext = prepareContext(userId, event, position); - StringWriter writer = new StringWriter(); - getTemplate(event, Context.getConfig().getString("sms.templatesPath", "sms") + "/") - .merge(velocityContext, writer); - return writer.toString(); - } -} diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java deleted file mode 100644 index 8707b10da..000000000 --- a/src/org/traccar/notification/NotificationMail.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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 { - - private NotificationMail() { - } - - 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))); - - String starttlsEnable = provider.getString("mail.smtp.starttls.enable"); - if (starttlsEnable != null) { - properties.put("mail.smtp.starttls.enable", Boolean.parseBoolean(starttlsEnable)); - } - String starttlsRequired = provider.getString("mail.smtp.starttls.required"); - if (starttlsRequired != null) { - properties.put("mail.smtp.starttls.required", Boolean.parseBoolean(starttlsRequired)); - } - - String sslEnable = provider.getString("mail.smtp.ssl.enable"); - if (sslEnable != null) { - properties.put("mail.smtp.ssl.enable", Boolean.parseBoolean(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; - } - - public static void sendMailSync(long userId, Event event, Position position) throws MessagingException { - 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); - - String from = properties.getProperty("mail.smtp.from"); - if (from != null) { - message.setFrom(new InternetAddress(from)); - } - - message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); - message.setSubject(mailMessage.getSubject()); - message.setSentDate(new Date()); - message.setContent(mailMessage.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(); - } - } - - public static void sendMailAsync(final long userId, final Event event, final Position position) { - new Thread(new Runnable() { - public void run() { - try { - sendMailSync(userId, event, position); - } catch (MessagingException error) { - Log.warning(error); - } - } - }).start(); - } - -} diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java deleted file mode 100644 index 8c0265af4..000000000 --- a/src/org/traccar/notification/NotificationSms.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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 com.cloudhopper.smpp.type.RecoverablePduException; -import com.cloudhopper.smpp.type.SmppChannelException; -import com.cloudhopper.smpp.type.SmppTimeoutException; -import com.cloudhopper.smpp.type.UnrecoverablePduException; - -public final class NotificationSms { - - private NotificationSms() { - } - - public static void sendSmsAsync(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { - Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); - } - } - - public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { - User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { - Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); - } - } -} diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java deleted file mode 100644 index c5ba688e8..000000000 --- a/src/org/traccar/notification/PropertiesProvider.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2016 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. - * 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.Config; -import org.traccar.model.ExtendedModel; - -public class PropertiesProvider { - - private Config config; - - private ExtendedModel extendedModel; - - public PropertiesProvider(Config config) { - this.config = config; - } - - public PropertiesProvider(ExtendedModel extendedModel) { - this.extendedModel = extendedModel; - } - - public String getString(String key) { - if (config != null) { - return config.getString(key); - } else { - return extendedModel.getString(key); - } - } - - 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) { - if (config != null) { - return config.getInteger(key, defaultValue); - } else { - Object result = extendedModel.getAttributes().get(key); - if (result != null) { - return result instanceof String ? Integer.parseInt((String) result) : (Integer) result; - } else { - return defaultValue; - } - } - } - -} |