camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [2/3] camel git commit: SmppNLSTSplitter for sending messages using National Language Shit Table
Date Fri, 02 Dec 2016 09:44:26 GMT
SmppNLSTSplitter for sending messages using National Language Shit Table


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b1b67bda
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b1b67bda
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b1b67bda

Branch: refs/heads/master
Commit: b1b67bdafd58ad105668e19699b8bbd27f1ec99b
Parents: 32143e8
Author: Engin Dumlu <engindumlu@gmail.com>
Authored: Wed Nov 30 20:00:35 2016 +0300
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Fri Dec 2 10:40:51 2016 +0100

----------------------------------------------------------------------
 .../camel/component/smpp/SmppConstants.java     |   2 +
 .../camel/component/smpp/SmppNLSTSplitter.java  | 100 +++++++++++++++++++
 .../camel/component/smpp/SmppSmCommand.java     |  12 ++-
 .../component/smpp/SmppSubmitSmCommand.java     |   7 +-
 .../component/smpp/SmppNLSTSplitterTest.java    |  63 ++++++++++++
 5 files changed, 180 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b1b67bda/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConstants.java
b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConstants.java
index 80c47d6..b9f3d08 100644
--- a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConstants.java
+++ b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConstants.java
@@ -27,6 +27,7 @@ public interface SmppConstants {
     String COMMAND_ID = "CamelSmppCommandId";
     String COMMAND_STATUS = "CamelSmppCommandStatus";
     String DATA_CODING = "CamelSmppDataCoding";
+    String DATA_SPLITTER = "CamelSmppSplitter";
     String DELIVERED = "CamelSmppDelivered";
     String DEST_ADDR = "CamelSmppDestAddr";
     String DEST_ADDR_NPI = "CamelSmppDestAddrNpi";
@@ -34,6 +35,7 @@ public interface SmppConstants {
     String DONE_DATE = "CamelSmppDoneDate";
     String ENCODING = "CamelSmppEncoding";
     String ERROR = "CamelSmppError";
+    String ESM_CLASS = "CamelSmppClass";
     String ESME_ADDR = "CamelSmppEsmeAddr";
     String ESME_ADDR_NPI = "CamelSmppEsmeAddrNpi";
     String ESME_ADDR_TON = "CamelSmppEsmeAddrTon";

http://git-wip-us.apache.org/repos/asf/camel/blob/b1b67bda/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppNLSTSplitter.java
----------------------------------------------------------------------
diff --git a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppNLSTSplitter.java
b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppNLSTSplitter.java
new file mode 100644
index 0000000..4e0bd39
--- /dev/null
+++ b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppNLSTSplitter.java
@@ -0,0 +1,100 @@
+package org.apache.camel.component.smpp;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Created by engin on 30/11/2016.
+ */
+class SmppNLSTSplitter extends SmppSplitter{
+
+    protected static final int UDHIE_NLI_SINGLE_MSG_HEADER_LENGTH = 0x03; // header length
for single message
+    protected static final int UDHIE_NLI_SINGLE_MSG_HEADER_REAL_LENGTH = UDHIE_NLI_SINGLE_MSG_HEADER_LENGTH
+ 1;
+
+    protected static final int UDHIE_NLI_MULTI_MSG_HEADER_LENGTH = 0x08; // header length
for multi message
+    protected static final int UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH = UDHIE_NLI_MULTI_MSG_HEADER_LENGTH
+ 1;
+
+
+    protected static final int UDHIE_SAR_REF_NUM_LENGTH = 1;
+//        protected static final byte UDHIE_IDENTIFIER_SAR = 0x00;
+//        protected static final byte UDHIE_SAR_LENGTH = 0x03;
+//        protected static final int MAX_SEG_COUNT = 255;
+
+    protected static final int UDHIE_NLI_IDENTIFIER = 0x25;
+    protected static final int UDHIE_NLI_HEADER_LENGTH = 0x01;
+
+    public static final int MAX_MSG_CHAR_SIZE = (MAX_MSG_BYTE_LENGTH * 8 / 7) - (UDHIE_NLI_SINGLE_MSG_HEADER_REAL_LENGTH
+ 1); // 155 for NLST
+    public static final int MAX_SEG_BYTE_SIZE = (MAX_MSG_BYTE_LENGTH - UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH)
* 8 / 7;
+
+    private byte languageIdentifier;
+    private final Logger logger = LoggerFactory.getLogger(SmppNLSTSplitter.class);
+
+    SmppNLSTSplitter(int currentLength, byte languageIdentifier) {
+        super(MAX_MSG_CHAR_SIZE, MAX_SEG_BYTE_SIZE, currentLength);
+        this.languageIdentifier = languageIdentifier;
+    }
+
+    public byte[][] split(byte[] message) {
+        if (!isSplitRequired()) {
+            byte[] nli_message = new byte[UDHIE_NLI_SINGLE_MSG_HEADER_REAL_LENGTH + message.length];
+            nli_message[0] = (byte) UDHIE_NLI_SINGLE_MSG_HEADER_LENGTH;
+            nli_message[1] = (byte) UDHIE_NLI_IDENTIFIER;
+            nli_message[2] = (byte) UDHIE_NLI_HEADER_LENGTH;
+            nli_message[3] = this.languageIdentifier;
+            System.arraycopy(message, 0, nli_message, 4, message.length);
+            return new byte[][]{nli_message};
+        }
+
+        int segmentLength = getSegmentLength();
+
+        // determine how many messages
+        int segmentNum = message.length / segmentLength;
+        int messageLength = message.length;
+        if (segmentNum > MAX_SEG_COUNT) {
+            // this is too long, can't fit, so chop
+            segmentNum = MAX_SEG_COUNT;
+            messageLength = segmentNum * segmentLength;
+        }
+        if ((messageLength % segmentLength) > 0) {
+            segmentNum++;
+        }
+
+        byte[][] segments = new byte[segmentNum][];
+
+        int lengthOfData;
+        byte refNum = getReferenceNumber();
+        for (int i = 0; i < segmentNum; i++) {
+            logger.info("segment number = {}", i);
+            if (segmentNum - i == 1) {
+                lengthOfData = messageLength - i * segmentLength;
+            } else {
+                lengthOfData = segmentLength;
+            }
+            logger.info("Length of data = {}", lengthOfData);
+
+            segments[i] = new byte[UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH + lengthOfData];
+            logger.info("segments[{}].length = {}", i, segments[i].length);
+
+            segments[i][0] = UDHIE_NLI_MULTI_MSG_HEADER_LENGTH; // doesn't include itself,
is header length
+            // SAR identifier
+            segments[i][1] = UDHIE_IDENTIFIER_SAR;
+            // SAR length
+            segments[i][2] = UDHIE_SAR_LENGTH;
+            // DATAGRAM REFERENCE NUMBER
+            segments[i][3] = refNum;
+            // total number of segments
+            segments[i][4] = (byte) segmentNum;
+            // segment #
+            segments[i][5] = (byte) (i + 1);
+
+            // language stuff
+            segments[i][6] = (byte) UDHIE_NLI_IDENTIFIER;
+            segments[i][7] = (byte) UDHIE_NLI_HEADER_LENGTH;
+            segments[i][8] = this.languageIdentifier;
+
+            // now copy the data
+            System.arraycopy(message, i * segmentLength, segments[i], UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH,
lengthOfData);
+        }
+        return segments;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/b1b67bda/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSmCommand.java
----------------------------------------------------------------------
diff --git a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSmCommand.java
b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSmCommand.java
index c308e4d..71b2171 100644
--- a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSmCommand.java
+++ b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSmCommand.java
@@ -76,11 +76,19 @@ public abstract class SmppSmCommand extends AbstractSmppCommand {
     }
 
     protected SmppSplitter createSplitter(Message message) {
-        Alphabet alphabet = determineAlphabet(message);
 
+        SmppSplitter splitter;
+        // use the splitter if provided via header
+        if (message.getHeaders().containsKey(SmppConstants.DATA_SPLITTER)){
+            splitter = message.getHeader(SmppConstants.DATA_SPLITTER, SmppSplitter.class);
+            if (null != splitter){
+                return splitter;
+            }
+            logger.warn("Invalid splitter given. Must be instance of SmppSplitter");
+        }
+        Alphabet alphabet = determineAlphabet(message);
         String body = message.getBody(String.class);
 
-        SmppSplitter splitter;
         if (SmppUtils.is8Bit(alphabet)) {
             splitter = new Smpp8BitSplitter(body.length());
         } else if (alphabet == Alphabet.ALPHA_UCS2) {

http://git-wip-us.apache.org/repos/asf/camel/blob/b1b67bda/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
----------------------------------------------------------------------
diff --git a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
index 542266f..7ac8955 100644
--- a/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
+++ b/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
@@ -95,8 +95,11 @@ public class SmppSubmitSmCommand extends SmppSmCommand {
         SubmitSm template = createSubmitSmTemplate(exchange);
         byte[][] segments = splitBody(exchange.getIn());
 
-        // multipart message
-        if (segments.length > 1) {
+        ESMClass esmClass = exchange.getIn().getHeader(SmppConstants.ESM_CLASS, ESMClass.class);
+        if (null != esmClass) {
+            template.setEsmClass(esmClass.value());
+            // multipart message
+        }else if (segments.length > 1) {
             template.setEsmClass(new ESMClass(MessageMode.DEFAULT, MessageType.DEFAULT, GSMSpecificFeature.UDHI).value());
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b1b67bda/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppNLSTSplitterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppNLSTSplitterTest.java
b/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppNLSTSplitterTest.java
new file mode 100644
index 0000000..2dfcabc
--- /dev/null
+++ b/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppNLSTSplitterTest.java
@@ -0,0 +1,63 @@
+package org.apache.camel.component.smpp;
+
+import java.nio.charset.Charset;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by engin on 30/11/2016.
+ */
+public class SmppNLSTSplitterTest {
+
+    @Test
+    public void splitTurkishShortMessageWith155Character() {
+        String message = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
+                + "123456789012345678901234567890123456789012345678901234567890123456789012345";
// 155 single message
+
+        byte turkishLanguageIdentifier = 0x01;
+        SmppSplitter splitter = new SmppNLSTSplitter(message.length(), turkishLanguageIdentifier);
+        SmppSplitter.resetCurrentReferenceNumber();
+        byte[][] result = splitter.split(message.getBytes());
+
+        assertEquals(1, result.length);
+        assertArrayEquals(new byte[]{SmppNLSTSplitter.UDHIE_NLI_SINGLE_MSG_HEADER_LENGTH,
SmppNLSTSplitter.UDHIE_NLI_IDENTIFIER, SmppNLSTSplitter.UDHIE_NLI_HEADER_LENGTH, turkishLanguageIdentifier,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50,
51, 52, 53,
+                54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 48,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
48, 49, 50, 51, 52, 53}, result[0]);
+    }
+
+    @Test
+    public void splitShortMessageWith156Character() {
+        String message = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
+ // first part 149
+                "0123456"; // second part 7
+
+        byte turkishLanguageIdentifier = 0x01;
+        SmppSplitter splitter = new SmppNLSTSplitter(message.length(), turkishLanguageIdentifier);
+        SmppSplitter.resetCurrentReferenceNumber();
+        byte[][] result = splitter.split(message.getBytes());
+
+        assertEquals(2, result.length);
+        assertArrayEquals(new byte[]{
+                SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_LENGTH, SmppNLSTSplitter.UDHIE_IDENTIFIER_SAR,
SmppNLSTSplitter.UDHIE_SAR_LENGTH, 1, 2, 1,
+                SmppNLSTSplitter.UDHIE_NLI_IDENTIFIER, SmppNLSTSplitter.UDHIE_NLI_HEADER_LENGTH,
turkishLanguageIdentifier,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50,
51, 52, 53,
+                54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 48,
+                49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
result[0]);
+
+        assertArrayEquals(new byte[]{
+                SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_LENGTH, SmppNLSTSplitter.UDHIE_IDENTIFIER_SAR,
SmppNLSTSplitter.UDHIE_SAR_LENGTH, 1, 2, 2,
+                SmppNLSTSplitter.UDHIE_NLI_IDENTIFIER, SmppNLSTSplitter.UDHIE_NLI_HEADER_LENGTH,
turkishLanguageIdentifier,
+                48, 49, 50, 51, 52, 53, 54}, result[1]);
+
+        String firstShortMessage = new String(result[0], SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH,
result[0].length - SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH);
+        String secondShortMessage = new String(result[1], SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH,
result[1].length - SmppNLSTSplitter.UDHIE_NLI_MULTI_MSG_HEADER_REAL_LENGTH);
+        assertEquals(message, firstShortMessage + secondShortMessage);
+    }
+}
\ No newline at end of file


Mime
View raw message