Browse Source

refactoring

pull/14446/head
Artem Barysh 1 year ago
parent
commit
67e4644a8c
  1. 53
      application/src/main/java/org/thingsboard/server/service/sms/smpp/SmppDataCoding.java
  2. 38
      application/src/main/java/org/thingsboard/server/service/sms/smpp/SmppSmsSender.java

53
application/src/main/java/org/thingsboard/server/service/sms/smpp/SmppDataCoding.java

@ -1,53 +0,0 @@
/**
* Copyright © 2016-2025 The Thingsboard Authors
*
* 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.thingsboard.server.service.sms.smpp;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.smpp.Data;
@Getter
@RequiredArgsConstructor
public enum SmppDataCoding {
SMSC_DEFAULT_ALPHABET((byte) 0, Data.ENC_GSM7BIT),
IA5((byte) 1, Data.ENC_ASCII),
OCTET_UNSPECIFIED_2((byte) 2, Data.ENC_GSM7BIT),
LATIN1((byte) 3, Data.ENC_ISO8859_1),
OCTET_UNSPECIFIED_4((byte) 4, Data.ENC_GSM7BIT),
JIS((byte) 5, Data.ENC_GSM7BIT),
CYRILLIC((byte) 6, Data.ENC_GSM7BIT),
LATIN_HEBREW((byte) 7, Data.ENC_GSM7BIT),
UCS2((byte) 8, Data.ENC_UTF16_BE),
PICTOGRAM((byte) 9, Data.ENC_GSM7BIT),
MUSIC_CODES((byte) 10, Data.ENC_GSM7BIT),
EXTENDED_KANJI((byte) 13, Data.ENC_GSM7BIT),
KOREAN((byte) 14, Data.ENC_GSM7BIT);
private final byte code;
private final String encodingName;
public static SmppDataCoding fromCode(byte code) {
for (SmppDataCoding smppDataCoding : values()) {
if (smppDataCoding.code == code) {
return smppDataCoding;
}
}
return null;
}
}

38
application/src/main/java/org/thingsboard/server/service/sms/smpp/SmppSmsSender.java

@ -38,6 +38,9 @@ import org.thingsboard.server.common.data.sms.config.SmppSmsProviderConfiguratio
import org.thingsboard.server.service.sms.AbstractSmsSender;
import java.io.IOException;
import org.smpp.util.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
@Slf4j
@ -86,12 +89,24 @@ public class SmppSmsSender extends AbstractSmsSender {
}
request.setDestAddr(new Address(config.getDestinationTon(), config.getDestinationNpi(), prepareNumber(numberTo)));
byte dataCoding = config.getCodingScheme();
SmppDataCoding smppDataCoding = Optional.ofNullable(SmppDataCoding.fromCode(dataCoding)).orElse(SmppDataCoding.SMSC_DEFAULT_ALPHABET);
byte dataCoding = Optional.ofNullable(config.getCodingScheme()).orElse((byte) 0);
request.setDataCoding(dataCoding);
Charset charset = resolveCharset(dataCoding);
if (charset != null) {
ByteBuffer buf = new ByteBuffer();
buf.appendBytes(message.getBytes(charset));
request.setShortMessageData(buf);
} else {
switch (dataCoding) {
case 2, 4, 9, 13 -> {
ByteBuffer buf = new ByteBuffer();
buf.appendBytes(message.getBytes(StandardCharsets.ISO_8859_1));
request.setShortMessageData(buf);
}
default -> request.setShortMessage(message);
}
}
request.setShortMessage(message, smppDataCoding.getEncodingName());
request.setDataCoding(smppDataCoding.getCode());
request.setDataCoding(Optional.ofNullable(config.getCodingScheme()).orElse((byte) 0));
request.setReplaceIfPresentFlag((byte) 0);
request.setEsmClass((byte) 0);
request.setProtocolId((byte) 0);
@ -109,6 +124,19 @@ public class SmppSmsSender extends AbstractSmsSender {
return countMessageSegments(message);
}
private Charset resolveCharset(byte dataCoding) {
return switch (dataCoding) {
case 1 -> Charset.forName("ISO-8859-9");
case 3 -> StandardCharsets.ISO_8859_1;
case 6 -> Charset.forName("ISO-8859-5");
case 7 -> Charset.forName("ISO-8859-8");
case 8 -> StandardCharsets.UTF_16BE;
case 10 -> Charset.forName("ISO-2022-JP");
case 14 -> Charset.forName("EUC-KR");
default -> null;
};
}
private synchronized void checkSmppSession() {
if (smppSession == null || !smppSession.isOpened()) {
smppSession = initSmppSession();

Loading…
Cancel
Save