Commit 07ed2581bea769dbbad1848448041d97f566264b

Authored by Vladyslav_Prykhodko
1 parent c81854e6

UI: Refactoring code

... ... @@ -24,8 +24,7 @@ import java.util.regex.Pattern;
24 24 @Slf4j
25 25 public abstract class AbstractSmsSender implements SmsSender {
26 26
27   - private static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
28   - private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
  27 + protected static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
29 28
30 29 private static final int MAX_SMS_MESSAGE_LENGTH = 1600;
31 30 private static final int MAX_SMS_SEGMENT_LENGTH = 70;
... ... @@ -38,14 +37,6 @@ public abstract class AbstractSmsSender implements SmsSender {
38 37 return phoneNumber;
39 38 }
40 39
41   - protected String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
42   - phoneNumber = phoneNumber.trim();
43   - if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
44   - throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
45   - }
46   - return phoneNumber;
47   - }
48   -
49 40 protected String prepareMessage(String message) {
50 41 message = message.replaceAll("^\"|\"$", "").replaceAll("\\\\n", "\n");
51 42 if (message.length() > MAX_SMS_MESSAGE_LENGTH) {
... ...
... ... @@ -19,16 +19,29 @@ import com.twilio.http.TwilioRestClient;
19 19 import com.twilio.rest.api.v2010.account.Message;
20 20 import com.twilio.type.PhoneNumber;
21 21 import org.apache.commons.lang3.StringUtils;
  22 +import org.thingsboard.rule.engine.api.sms.exception.SmsParseException;
22 23 import org.thingsboard.server.common.data.sms.config.TwilioSmsProviderConfiguration;
23 24 import org.thingsboard.rule.engine.api.sms.exception.SmsException;
24 25 import org.thingsboard.rule.engine.api.sms.exception.SmsSendException;
25 26 import org.thingsboard.server.service.sms.AbstractSmsSender;
26 27
  28 +import java.util.regex.Pattern;
  29 +
27 30 public class TwilioSmsSender extends AbstractSmsSender {
28 31
  32 + private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
  33 +
29 34 private TwilioRestClient twilioRestClient;
30 35 private String numberFrom;
31 36
  37 + private String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
  38 + phoneNumber = phoneNumber.trim();
  39 + if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
  40 + throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
  41 + }
  42 + return phoneNumber;
  43 + }
  44 +
32 45 public TwilioSmsSender(TwilioSmsProviderConfiguration config) {
33 46 if (StringUtils.isEmpty(config.getAccountSid()) || StringUtils.isEmpty(config.getAccountToken()) || StringUtils.isEmpty(config.getNumberFrom())) {
34 47 throw new IllegalArgumentException("Invalid twilio sms provider configuration: accountSid, accountToken and numberFrom should be specified!");
... ...