From bb289a69fa4d292378c5c534e10985be65b2e392 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 25 Mar 2018 22:16:09 -0300 Subject: generalization for notifications processing --- src/org/traccar/BaseProtocol.java | 8 +-- src/org/traccar/Context.java | 15 ++++-- .../traccar/api/resource/NotificationResource.java | 16 ++---- src/org/traccar/database/CommandsManager.java | 6 +-- src/org/traccar/database/NotificationManager.java | 26 +++------ src/org/traccar/model/Notification.java | 18 +++++++ .../notification/NotificationException.java | 25 +++++++++ src/org/traccar/notification/NotificationMail.java | 62 +++++++++------------- src/org/traccar/notification/NotificationNull.java | 31 +++++++++++ src/org/traccar/notification/NotificationSms.java | 27 ++++------ src/org/traccar/notification/NotificationWeb.java | 31 +++++++++++ src/org/traccar/notification/Notificator.java | 40 ++++++++++++++ .../traccar/notification/NotificatorManager.java | 51 ++++++++++++++++++ src/org/traccar/smpp/SmppClient.java | 60 ++++++++++++--------- src/org/traccar/sms/SMSException.java | 28 ++++++++++ src/org/traccar/sms/SMSManager.java | 8 +++ 16 files changed, 331 insertions(+), 121 deletions(-) create mode 100644 src/org/traccar/notification/NotificationException.java create mode 100644 src/org/traccar/notification/NotificationNull.java create mode 100644 src/org/traccar/notification/NotificationWeb.java create mode 100644 src/org/traccar/notification/Notificator.java create mode 100644 src/org/traccar/notification/NotificatorManager.java create mode 100644 src/org/traccar/sms/SMSException.java create mode 100644 src/org/traccar/sms/SMSManager.java (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 07adbea5e..7265bd9c1 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -92,18 +92,18 @@ public abstract class BaseProtocol implements Protocol { @Override public void sendTextCommand(String destAddress, Command command) throws Exception { - if (Context.getSmppManager() != null) { + if (Context.getSmsManager() != null) { if (command.getType().equals(Command.TYPE_CUSTOM)) { - Context.getSmppManager().sendMessageSync(destAddress, command.getString(Command.KEY_DATA), true); + Context.getSmsManager().sendMessageSync(destAddress, command.getString(Command.KEY_DATA), true); } else if (supportedTextCommands.contains(command.getType()) && textCommandEncoder != null) { - Context.getSmppManager().sendMessageSync(destAddress, + Context.getSmsManager().sendMessageSync(destAddress, (String) textCommandEncoder.encodeCommand(command), true); } else { throw new RuntimeException( "Command " + command.getType() + " is not supported in protocol " + getName()); } } else { - throw new RuntimeException("SMPP client is not enabled"); + throw new RuntimeException("SMS is not enabled"); } } diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 09e3c619b..c9ac4ec12 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -74,7 +74,7 @@ import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.MultiPartEventForwarder; import org.traccar.reports.model.TripsConfig; -import org.traccar.smpp.SmppClient; +import org.traccar.sms.SMSManager; import org.traccar.web.WebServer; public final class Context { @@ -238,10 +238,10 @@ public final class Context { return statisticsManager; } - private static SmppClient smppClient; + private static SMSManager smsManager; - public static SmppClient getSmppManager() { - return smppClient; + public static SMSManager getSmsManager() { + return smsManager; } private static MotionEventHandler motionEventHandler; @@ -388,7 +388,12 @@ public final class Context { statisticsManager = new StatisticsManager(); if (config.getBoolean("sms.smpp.enable")) { - smppClient = new SmppClient(); + final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); + try { + smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); + } catch (ClassNotFoundException e) { + Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); + } } } diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 540f02926..2a398f40d 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -17,7 +17,6 @@ package org.traccar.api.resource; import java.util.Collection; -import javax.mail.MessagingException; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -31,13 +30,9 @@ import org.traccar.api.ExtendedObjectResource; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; -import org.traccar.notification.NotificationMail; -import org.traccar.notification.NotificationSms; +import org.traccar.notification.NotificationException; +import org.traccar.notification.NotificatorManager; -import com.cloudhopper.smpp.type.RecoverablePduException; -import com.cloudhopper.smpp.type.SmppChannelException; -import com.cloudhopper.smpp.type.SmppTimeoutException; -import com.cloudhopper.smpp.type.UnrecoverablePduException; @Path("notifications") @Produces(MediaType.APPLICATION_JSON) @@ -56,10 +51,9 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") - public Response testMessage() throws MessagingException, RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { - NotificationMail.sendMailSync(getUserId(), new Event("test", 0), null); - NotificationSms.sendSmsSync(getUserId(), new Event("test", 0), null); + public Response testMessage() throws NotificationException, InterruptedException { + NotificatorManager.getMail().sendSync(getUserId(), new Event("test", 0), null); + NotificatorManager.getSms().sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index 9ceb995ef..2e68fae5a 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -58,10 +58,10 @@ public class CommandsManager extends ExtendedObjectManager { BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); protocol.sendTextCommand(phone, command); } else if (command.getType().equals(Command.TYPE_CUSTOM)) { - if (Context.getSmppManager() != null) { - Context.getSmppManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true); + if (Context.getSmsManager() != null) { + Context.getSmsManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true); } else { - throw new RuntimeException("SMPP client is not enabled"); + throw new RuntimeException("SMS is not enabled"); } } else { throw new RuntimeException("Command " + command.getType() + " is not supported"); diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 1c59a8666..37c6b720c 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,8 +32,7 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Typed; -import org.traccar.notification.NotificationMail; -import org.traccar.notification.NotificationSms; +import org.traccar.notification.NotificatorManager; public class NotificationManager extends ExtendedObjectManager { @@ -83,29 +82,16 @@ public class NotificationManager extends ExtendedObjectManager { if (usersToForward != null) { usersToForward.add(userId); } - boolean sentWeb = false; - boolean sentMail = false; - boolean sentSms = Context.getSmppManager() == null; + final Set notificationMethods = new HashSet<>(); for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getServerTime())) { Notification notification = getById(notificationId); if (getById(notificationId).getType().equals(event.getType())) { - if (!sentWeb && notification.getWeb()) { - Context.getConnectionManager().updateEvent(userId, event); - sentWeb = true; - } - if (!sentMail && notification.getMail()) { - NotificationMail.sendMailAsync(userId, event, position); - sentMail = true; - } - if (!sentSms && notification.getSms()) { - NotificationSms.sendSmsAsync(userId, event, position); - sentSms = true; - } - } - if (sentWeb && sentMail && sentSms) { - break; + notificationMethods.addAll(notification.getMethods()); } } + for (String nm : notificationMethods) { + NotificatorManager.getNotificator(nm).sendAsync(userId, event, position); + } } } if (Context.getEventForwarder() != null) { diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index cc80f2ae2..0d0b34cd4 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -15,6 +15,9 @@ */ package org.traccar.model; +import java.util.HashSet; +import java.util.Set; + public class Notification extends ScheduledModel { private boolean always; @@ -66,4 +69,19 @@ public class Notification extends ScheduledModel { public void setSms(boolean sms) { this.sms = sms; } + + public Set getMethods() { + final Set set = new HashSet<>(); + if (web) { + set.add("web"); + } + if (mail) { + set.add("mail"); + } + if (sms) { + set.add("sms"); + } + return set; + } + } diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java new file mode 100644 index 000000000..34d5a80f7 --- /dev/null +++ b/src/org/traccar/notification/NotificationException.java @@ -0,0 +1,25 @@ +/* + * 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; + +public class NotificationException extends Exception { + + public NotificationException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 6c81ecc79..3a2a19337 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -32,10 +32,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -public final class NotificationMail { - - private NotificationMail() { - } +public final class NotificationMail extends Notificator { private static Properties getProperties(PropertiesProvider provider) { Properties properties = new Properties(); @@ -84,7 +81,8 @@ public final class NotificationMail { return properties; } - public static void sendMailSync(long userId, Event event, Position position) throws MessagingException { + @Override + public void sendSync(long userId, Event event, Position position) throws NotificationException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -103,40 +101,32 @@ public final class NotificationMail { 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(); - } - } + String from = properties.getProperty("mail.smtp.from"); + if (from != null) { + message.setFrom(new InternetAddress(from)); + } - 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); - } + 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(); } - }).start(); + } catch (MessagingException e) { + throw new NotificationException(e); + } } } diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java new file mode 100644 index 000000000..afe160561 --- /dev/null +++ b/src/org/traccar/notification/NotificationNull.java @@ -0,0 +1,31 @@ +/* + * 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.model.Event; +import org.traccar.model.Position; + +public final class NotificationNull extends Notificator { + + @Override + public void sendAsync(long userId, Event event, Position position) { + } + + @Override + public void sendSync(long userId, Event event, Position position) { + } +} diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 8c0265af4..34d68e174 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,32 +20,27 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +import org.traccar.sms.SMSException; -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 extends Notificator { -public final class NotificationSms { - - private NotificationSms() { - } - - public static void sendSmsAsync(long userId, Event event, Position position) { + @Override + public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageAsync(user.getPhone(), + Context.getSmsManager().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 { + @Override + public void sendSync(long userId, Event event, Position position) throws SMSException, + InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageSync(user.getPhone(), + Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); } } diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java new file mode 100644 index 000000000..c6a1ae281 --- /dev/null +++ b/src/org/traccar/notification/NotificationWeb.java @@ -0,0 +1,31 @@ +/* + * 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; + +public final class NotificationWeb extends Notificator { + + @Override + public void sendSync(long userId, Event event, Position position) throws NotificationException, + InterruptedException { + Context.getConnectionManager().updateEvent(userId, event); + } + +} diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java new file mode 100644 index 000000000..7192d25c0 --- /dev/null +++ b/src/org/traccar/notification/Notificator.java @@ -0,0 +1,40 @@ +/* + * 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.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 (NotificationException | InterruptedException error) { + Log.warning(error); + } + } + }).start(); + } + + + public abstract void sendSync(long userId, Event event, Position position) + throws NotificationException, InterruptedException; +} diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java new file mode 100644 index 000000000..814c8dc1a --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,51 @@ +/* + * 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.HashMap; +import java.util.Map; + +public class NotificatorManager { + + protected NotificatorManager() { + //not called + } + + private static final Map NOTIFICATORS = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + + static { + NOTIFICATORS.put("sms", new NotificationSms()); + NOTIFICATORS.put("mail", new NotificationMail()); + NOTIFICATORS.put("web", new NotificationWeb()); + } + + public static Notificator getNotificator(String type) { + return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + } + + public static Notificator getSms() { + return getNotificator("sms"); + } + + public static Notificator getMail() { + return getNotificator("mail"); + } + + +} + diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index bf395f90e..97830dd00 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.sms.SMSException; +import org.traccar.sms.SMSManager; import com.cloudhopper.commons.charset.CharsetUtil; import com.cloudhopper.smpp.SmppBindType; @@ -42,7 +44,7 @@ import com.cloudhopper.smpp.type.SmppChannelException; import com.cloudhopper.smpp.type.SmppTimeoutException; import com.cloudhopper.smpp.type.UnrecoverablePduException; -public class SmppClient { +public class SmppClient implements SMSManager { private SmppSessionConfiguration sessionConfig = new SmppSessionConfiguration(); private SmppSession smppSession; @@ -212,46 +214,52 @@ public class SmppClient { } } + @Override public synchronized void sendMessageSync(String destAddress, String message, boolean command) - throws RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, - InterruptedException, IllegalStateException { + throws SMSException, InterruptedException, IllegalStateException { if (getSession() != null && getSession().isBound()) { - SubmitSm submit = new SubmitSm(); - byte[] textBytes; - textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); - submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); - if (requestDlr) { - submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); - } + try { + SubmitSm submit = new SubmitSm(); + byte[] textBytes; + textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); + submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); + if (requestDlr) { + submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); + } - if (textBytes != null && textBytes.length > 255) { - submit.addOptionalParameter(new Tlv(SmppConstants.TAG_MESSAGE_PAYLOAD, textBytes, "message_payload")); - } else { - submit.setShortMessage(textBytes); - } + if (textBytes != null && textBytes.length > 255) { + submit.addOptionalParameter(new Tlv(SmppConstants.TAG_MESSAGE_PAYLOAD, textBytes, + "message_payload")); + } else { + submit.setShortMessage(textBytes); + } - submit.setSourceAddress(command ? new Address(commandSourceTon, commandSourceNpi, commandSourceAddress) - : new Address(sourceTon, sourceNpi, sourceAddress)); - submit.setDestAddress(new Address(destTon, destNpi, destAddress)); - SubmitSmResp submitResponce = getSession().submit(submit, submitTimeout); - if (submitResponce.getCommandStatus() == SmppConstants.STATUS_OK) { - Log.debug("SMS submitted, message id: " + submitResponce.getMessageId()); - } else { - throw new IllegalStateException(submitResponce.getResultMessage()); + submit.setSourceAddress(command ? new Address(commandSourceTon, commandSourceNpi, commandSourceAddress) + : new Address(sourceTon, sourceNpi, sourceAddress)); + submit.setDestAddress(new Address(destTon, destNpi, destAddress)); + SubmitSmResp submitResponce = getSession().submit(submit, submitTimeout); + if (submitResponce.getCommandStatus() == SmppConstants.STATUS_OK) { + Log.debug("SMS submitted, message id: " + submitResponce.getMessageId()); + } else { + throw new IllegalStateException(submitResponce.getResultMessage()); + } + } catch (SmppChannelException | RecoverablePduException + | SmppTimeoutException | UnrecoverablePduException error) { + throw new SMSException(error); } } else { - throw new SmppChannelException("SMPP session is not connected"); + throw new SMSException(new SmppChannelException("SMPP session is not connected")); } } + @Override public void sendMessageAsync(final String destAddress, final String message, final boolean command) { executorService.execute(new Runnable() { @Override public void run() { try { sendMessageSync(destAddress, message, command); - } catch (InterruptedException | RecoverablePduException | UnrecoverablePduException - | SmppTimeoutException | SmppChannelException | IllegalStateException error) { + } catch (SMSException | InterruptedException | IllegalStateException error) { Log.warning(error); } } diff --git a/src/org/traccar/sms/SMSException.java b/src/org/traccar/sms/SMSException.java new file mode 100644 index 000000000..c9c0b3128 --- /dev/null +++ b/src/org/traccar/sms/SMSException.java @@ -0,0 +1,28 @@ +/* + * 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.sms; + + +import org.traccar.notification.NotificationException; + +public class SMSException extends NotificationException { + + public SMSException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/sms/SMSManager.java b/src/org/traccar/sms/SMSManager.java new file mode 100644 index 000000000..b40dae867 --- /dev/null +++ b/src/org/traccar/sms/SMSManager.java @@ -0,0 +1,8 @@ +package org.traccar.sms; + +public interface SMSManager { + + void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, SMSException; + void sendMessageAsync(final String destAddress, final String message, final boolean command); + +} -- cgit v1.2.3