Guest User

Untitled

a guest
Jan 11th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. class Sender {
  2.  
  3. fun send(send: Send, message: String, to: String) {
  4. send.send(to, message)
  5. }
  6.  
  7. }
  8.  
  9. interface Send {
  10. fun send(to: String, message: String)
  11. }
  12.  
  13. class SendSMS(private val phoneValidator: PhoneValidator,
  14. private val smpp: SMPPProxy,
  15. private val username: String,
  16. private val password: String) : Send {
  17.  
  18. override fun send(to: String, message: String) {
  19. if (!phoneValidator.isValid(to)) {
  20. throw NoPhoneNumber(invalidPhone = to)
  21. }
  22.  
  23. try {
  24. smpp.openConnection(username, password)
  25. smpp.send(to, message, null, "15")
  26. smpp.closeConnection()
  27. } catch (ex: Exception) {
  28. try {
  29. smpp.closeConnection()
  30. } catch (ex: Exception) {
  31. } finally {
  32. throw SendSMSException(ex.message ?: "unknown message")
  33. }
  34. }
  35. }
  36.  
  37. }
  38.  
  39.  
  40. class SendEmail(private val emailConnection: SendMailConnection) : Send {
  41.  
  42. var subject: String = ""
  43.  
  44. override fun send(to: String, message: String) {
  45. if (subject.isEmpty()) {
  46. throw RequiredSubjectEmail()
  47. }
  48.  
  49. try {
  50. emailConnection.prepareMessage(to, message, subject)
  51. emailConnection.send()
  52. emailConnection.close()
  53. } catch (ex: Exception) {
  54. try {
  55. emailConnection.close()
  56. } catch (ex: Exception) {
  57. } finally {
  58. throw SendEmailException(ex.message ?: "unknown message")
  59. }
  60. }
  61. }
  62.  
  63. }
  64.  
  65. class SMPPProxy(private val ip: String,
  66. private val port: String) {
  67.  
  68. val smpp: SMPP by lazy {
  69. SMPP(ip, port)
  70. }
  71.  
  72. fun openConnection(username: String, password: String) = smpp.openConnection(username, password)
  73.  
  74. fun closeConnection() = smpp.closeConnection()
  75.  
  76. fun send(to: String, message: String, a: String?, s: String) {
  77. smpp.send(to, message, a, s)
  78. }
  79.  
  80. }
  81.  
  82.  
  83. sealed class SendException(message: String = "") : Exception(message)
  84. class SendEmailException(message: String) : SendException(message)
  85. class RequiredSubjectEmail : SendException()
  86. class NoPhoneNumber(invalidPhone: String) : SendException("$invalidPhone is not a phone number")
  87. class SendSMSException(message: String) : SendException(message)
  88.  
  89.  
  90. fun main(vararg arg: String) {
  91.  
  92. try {
  93. val smpp = SMPPProxy("192.168.1.4", "4301")
  94. val emailConnection = SendMailConnection("mail.myserver.com")
  95.  
  96. val sms = SendSMS(PhoneValidator(), smpp, "your­username", "your­password")
  97. val email = SendEmail(emailConnection)
  98. val sender = Sender()
  99.  
  100. sender.send(sms, "Message", "675848584")
  101.  
  102. email.subject = "Subject"
  103. sender.send(email, "Message", "tonilopezmr@gmail.com")
  104. } catch (ex: SendException) {
  105. ex.printStackTrace()
  106. }
  107.  
  108. }
Add Comment
Please, Sign In to add comment