Skip to content

Instantly share code, notes, and snippets.

@foreverdeepak
Last active November 29, 2019 09:23
Show Gist options
  • Save foreverdeepak/9130661 to your computer and use it in GitHub Desktop.
Save foreverdeepak/9130661 to your computer and use it in GitHub Desktop.
package com.esoftdive.core.smpp.internal;
import java.util.Random;
import com.cloudhopper.commons.charset.CharsetUtil;
import com.cloudhopper.smpp.SmppConstants;
import com.cloudhopper.smpp.SmppSession;
import com.cloudhopper.smpp.pdu.SubmitSm;
import com.cloudhopper.smpp.type.Address;
public class CloudhopperClient {
public void sendLongMessage(String msisdn, String message, String senderAddr) throws Exception {
byte sourceTon = (byte)0x03;
if(senderAddr != null && senderAddr.length() > 0) {
sourceTon = (byte)0x05;
}
byte[] textBytes = CharsetUtil.encode(message, CharsetUtil.CHARSET_ISO_8859_15);
int maximumMultipartMessageSegmentSize = 134;
byte[] byteSingleMessage = textBytes;
byte[][] byteMessagesArray = splitUnicodeMessage(byteSingleMessage, maximumMultipartMessageSegmentSize);
// submit all messages
for (int i = 0; i < byteMessagesArray.length; i++) {
SubmitSm submit0 = new SubmitSm();
submit0.setEsmClass(SmppConstants.ESM_CLASS_UDHI_MASK);
submit0.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED);
submit0.setSourceAddress(new Address(sourceTon, (byte)0x00, senderAddr));
submit0.setDestAddress(new Address((byte)0x03, (byte)0x00, msisdn));
submit0.setShortMessage(byteMessagesArray[i]);
getSession().submit(submit0, 10000);
}
}
private static byte[][] splitUnicodeMessage(byte[] aMessage, Integer maximumMultipartMessageSegmentSize) {
final byte UDHIE_HEADER_LENGTH = 0x05;
final byte UDHIE_IDENTIFIER_SAR = 0x00;
final byte UDHIE_SAR_LENGTH = 0x03;
// determine how many messages have to be sent
int numberOfSegments = aMessage.length / maximumMultipartMessageSegmentSize;
int messageLength = aMessage.length;
if (numberOfSegments > 255) {
numberOfSegments = 255;
messageLength = numberOfSegments * maximumMultipartMessageSegmentSize;
}
if ((messageLength % maximumMultipartMessageSegmentSize) > 0) {
numberOfSegments++;
}
// prepare array for all of the msg segments
byte[][] segments = new byte[numberOfSegments][];
int lengthOfData;
// generate new reference number
byte[] referenceNumber = new byte[1];
new Random().nextBytes(referenceNumber);
// split the message adding required headers
for (int i = 0; i < numberOfSegments; i++) {
if (numberOfSegments - i == 1) {
lengthOfData = messageLength - i * maximumMultipartMessageSegmentSize;
} else {
lengthOfData = maximumMultipartMessageSegmentSize;
}
// new array to store the header
segments[i] = new byte[6 + lengthOfData];
// UDH header
// doesn't include itself, its header length
segments[i][0] = UDHIE_HEADER_LENGTH;
// SAR identifier
segments[i][1] = UDHIE_IDENTIFIER_SAR;
// SAR length
segments[i][2] = UDHIE_SAR_LENGTH;
// reference number (same for all messages)
segments[i][3] = referenceNumber[0];
// total number of segments
segments[i][4] = (byte) numberOfSegments;
// segment number
segments[i][5] = (byte) (i + 1);
// copy the data into the array
System.arraycopy(aMessage, (i * maximumMultipartMessageSegmentSize), segments[i], 6, lengthOfData);
}
return segments;
}
private SmppSession getSession() {
//take the session
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment