UI: Refactoring code
This commit is contained in:
parent
c81854e614
commit
07ed2581be
@ -24,8 +24,7 @@ import java.util.regex.Pattern;
|
||||
@Slf4j
|
||||
public abstract class AbstractSmsSender implements SmsSender {
|
||||
|
||||
private static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
|
||||
private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
|
||||
protected static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
|
||||
|
||||
private static final int MAX_SMS_MESSAGE_LENGTH = 1600;
|
||||
private static final int MAX_SMS_SEGMENT_LENGTH = 70;
|
||||
@ -38,14 +37,6 @@ public abstract class AbstractSmsSender implements SmsSender {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
protected String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
|
||||
phoneNumber = phoneNumber.trim();
|
||||
if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
|
||||
throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
|
||||
}
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
protected String prepareMessage(String message) {
|
||||
message = message.replaceAll("^\"|\"$", "").replaceAll("\\\\n", "\n");
|
||||
if (message.length() > MAX_SMS_MESSAGE_LENGTH) {
|
||||
|
||||
@ -19,16 +19,29 @@ import com.twilio.http.TwilioRestClient;
|
||||
import com.twilio.rest.api.v2010.account.Message;
|
||||
import com.twilio.type.PhoneNumber;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.thingsboard.rule.engine.api.sms.exception.SmsParseException;
|
||||
import org.thingsboard.server.common.data.sms.config.TwilioSmsProviderConfiguration;
|
||||
import org.thingsboard.rule.engine.api.sms.exception.SmsException;
|
||||
import org.thingsboard.rule.engine.api.sms.exception.SmsSendException;
|
||||
import org.thingsboard.server.service.sms.AbstractSmsSender;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TwilioSmsSender extends AbstractSmsSender {
|
||||
|
||||
private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
|
||||
|
||||
private TwilioRestClient twilioRestClient;
|
||||
private String numberFrom;
|
||||
|
||||
private String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
|
||||
phoneNumber = phoneNumber.trim();
|
||||
if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
|
||||
throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
|
||||
}
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public TwilioSmsSender(TwilioSmsProviderConfiguration config) {
|
||||
if (StringUtils.isEmpty(config.getAccountSid()) || StringUtils.isEmpty(config.getAccountToken()) || StringUtils.isEmpty(config.getNumberFrom())) {
|
||||
throw new IllegalArgumentException("Invalid twilio sms provider configuration: accountSid, accountToken and numberFrom should be specified!");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user