camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmuel...@apache.org
Subject svn commit: r1457477 - in /camel/branches/camel-2.9.x/components/camel-smpp/src: main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java
Date Sun, 17 Mar 2013 16:12:54 GMT
Author: cmueller
Date: Sun Mar 17 16:12:54 2013
New Revision: 1457477

URL: http://svn.apache.org/r1457477
Log:
CAMEL-6121: While creation of submit_sm from Exchange no optional params are being set to
submit_sm

Modified:
    camel/branches/camel-2.9.x/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
    camel/branches/camel-2.9.x/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java

Modified: camel/branches/camel-2.9.x/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java?rev=1457477&r1=1457476&r2=1457477&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppSubmitSmCommand.java
Sun Mar 17 16:12:54 2013
@@ -16,9 +16,12 @@
  */
 package org.apache.camel.component.smpp;
 
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -29,6 +32,9 @@ import org.jsmpp.bean.MessageMode;
 import org.jsmpp.bean.MessageType;
 import org.jsmpp.bean.NumberingPlanIndicator;
 import org.jsmpp.bean.OptionalParameter;
+import org.jsmpp.bean.OptionalParameter.COctetString;
+import org.jsmpp.bean.OptionalParameter.OctetString;
+import org.jsmpp.bean.OptionalParameter.Tag;
 import org.jsmpp.bean.RegisteredDelivery;
 import org.jsmpp.bean.SubmitSm;
 import org.jsmpp.bean.TypeOfNumber;
@@ -51,7 +57,7 @@ public class SmppSubmitSmCommand extends
             if (log.isDebugEnabled()) {
                 log.debug("Sending short message {} for exchange id '{}'...", i, exchange.getExchangeId());
             }
-            
+
             try {
                 messageID = session.submitShortMessage(
                         submitSm.getServiceType(),
@@ -75,7 +81,7 @@ public class SmppSubmitSmCommand extends
             } catch (Exception e) {
                 throw new SmppException(e);
             }
-            
+
             messageIDs.add(messageID);
         }
 
@@ -111,6 +117,7 @@ public class SmppSubmitSmCommand extends
         return submitSms;
     }
 
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     protected SubmitSm createSubmitSmTemplate(Exchange exchange) {
         Message in = exchange.getIn();
         SubmitSm submitSm = new SubmitSm();
@@ -201,11 +208,52 @@ public class SmppSubmitSmCommand extends
         } else {
             submitSm.setReplaceIfPresent(config.getReplaceIfPresentFlag());
         }
-        
+
         submitSm.setEsmClass(new ESMClass().value());
-        
-        submitSm.setOptionalParametes();
+
+        Map<String, String> optinalParamaters = in.getHeader(SmppConstants.OPTIONAL_PARAMETERS,
Map.class);
+        if (optinalParamaters != null) {
+            List<OptionalParameter> optParams = new ArrayList<OptionalParameter>();
+            for (Entry<String, String> entry : optinalParamaters.entrySet()) {
+                OptionalParameter optParam = null;
+
+                try {
+                    Tag tag = Tag.valueOf(entry.getKey());
+                    Class type = determineTypeClass(tag);
+    
+                    if (OctetString.class.equals(type)) {
+                        optParam = new OptionalParameter.OctetString(tag.code(), entry.getValue());
+                    } else if (COctetString.class.equals(type)) {
+                        optParam = new OptionalParameter.COctetString(tag.code(), entry.getValue());
+                    } else if (org.jsmpp.bean.OptionalParameter.Byte.class.equals(type))
{
+                        optParam = new OptionalParameter.Byte(tag.code(), Byte.valueOf(entry.getValue()));
+                    } else if (org.jsmpp.bean.OptionalParameter.Int.class.equals(type)) {
+                        optParam = new OptionalParameter.Int(tag.code(), Integer.valueOf(entry.getValue()));
+                    } else if (org.jsmpp.bean.OptionalParameter.Short.class.equals(type))
{
+                        optParam = new OptionalParameter.Short(tag.code(), Short.valueOf(entry.getValue()));
+                    } else if (org.jsmpp.bean.OptionalParameter.Null.class.equals(type))
{
+                        optParam = new OptionalParameter.Null(tag);
+                    }
+
+                    optParams.add(optParam);
+                } catch (Exception e) {
+                    log.info("Couldn't determine optional parameter for key {} and value
{}. Skip this one.", entry.getKey(), entry.getValue());
+                }
+            }
+
+            submitSm.setOptionalParametes(optParams.toArray(new OptionalParameter[optParams.size()]));
+        } else {
+            submitSm.setOptionalParametes();
+        }
 
         return submitSm;
     }
+
+    @SuppressWarnings("unchecked")
+    protected Class<? extends OptionalParameter> determineTypeClass(Tag tag) throws
SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{
+        // we have to use reflection because the type field is private
+        Field f = tag.getClass().getDeclaredField("type");
+        f.setAccessible(true);
+        return (Class<? extends OptionalParameter>) f.get(tag);
+    }
 }
\ No newline at end of file

Modified: camel/branches/camel-2.9.x/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java?rev=1457477&r1=1457476&r2=1457477&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppSubmitSmCommandTest.java
Sun Mar 17 16:12:54 2013
@@ -18,6 +18,8 @@ package org.apache.camel.component.smpp;
 
 import java.util.Arrays;
 import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
 import java.util.TimeZone;
 
 import org.apache.camel.Exchange;
@@ -28,6 +30,10 @@ import org.jsmpp.bean.Alphabet;
 import org.jsmpp.bean.DataCoding;
 import org.jsmpp.bean.ESMClass;
 import org.jsmpp.bean.NumberingPlanIndicator;
+import org.jsmpp.bean.OptionalParameter;
+import org.jsmpp.bean.OptionalParameter.COctetString;
+import org.jsmpp.bean.OptionalParameter.OctetString;
+import org.jsmpp.bean.OptionalParameter.Tag;
 import org.jsmpp.bean.RegisteredDelivery;
 import org.jsmpp.bean.ReplaceIfPresentFlag;
 import org.jsmpp.bean.SMSCDeliveryReceipt;
@@ -48,34 +54,35 @@ import static org.easymock.classextensio
 import static org.easymock.classextension.EasyMock.replay;
 import static org.easymock.classextension.EasyMock.verify;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 
 public class SmppSubmitSmCommandTest {
-    
+
     private static TimeZone defaultTimeZone;
 
     private SMPPSession session;
     private SmppConfiguration config;
     private SmppSubmitSmCommand command;
-    
+
     @BeforeClass
     public static void setUpBeforeClass() {
         defaultTimeZone = TimeZone.getDefault();
-        
+
         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
     }
-    
+
     @AfterClass
     public static void tearDownAfterClass() {
         if (defaultTimeZone != null) {
             TimeZone.setDefault(defaultTimeZone);            
         }
     }
-    
+
     @Before
     public void setUp() {
         session = createMock(SMPPSession.class);
         config = new SmppConfiguration();
-        
+
         command = new SmppSubmitSmCommand(session, config);
     }
 
@@ -91,17 +98,17 @@ public class SmppSubmitSmCommandTest {
                 eq(DataCoding.newInstance((byte) 0)), eq((byte) 0),
                 aryEq("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890".getBytes())))
                 .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
-        
+
         assertEquals(Arrays.asList("1"), exchange.getOut().getHeader(SmppConstants.ID));
         assertEquals(1, exchange.getOut().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
     }
-    
+
     @Test
     @Ignore()
     public void executeLongBody() throws Exception {
@@ -110,7 +117,7 @@ public class SmppSubmitSmCommandTest {
             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};
         byte[] secondSM = new byte[]{52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 48};
-        
+
         Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
         exchange.getIn().setHeader(SmppConstants.COMMAND, "SubmitSm");
         exchange.getIn().setHeader(SmppConstants.ID, "1");
@@ -125,17 +132,17 @@ public class SmppSubmitSmCommandTest {
                 (String) isNull(), (String) isNull(), eq(new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE)),
eq(ReplaceIfPresentFlag.DEFAULT.value()),
                 eq(DataCoding.newInstance((byte) 0)), eq((byte) 0), eq(secondSM)))
                 .andReturn("2");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
-        
+
         assertEquals(Arrays.asList("1", "2"), exchange.getOut().getHeader(SmppConstants.ID));
         assertEquals(2, exchange.getOut().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
     }
-    
+
     @Test
     public void execute() throws Exception {
         Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
@@ -159,17 +166,63 @@ public class SmppSubmitSmCommandTest {
                 eq(new ESMClass()), eq((byte) 1), eq((byte) 2), eq("-300101001831100-"),
eq("-300101003702200-"), eq(new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS)),
                 eq(ReplaceIfPresentFlag.REPLACE.value()), eq(DataCoding.newInstance((byte)
0)), eq((byte) 0), aryEq("short message body".getBytes())))
                 .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
-        
+
         assertEquals(Arrays.asList("1"), exchange.getOut().getHeader(SmppConstants.ID));
         assertEquals(1, exchange.getOut().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
     }
-    
+
+    @Test
+    public void executeWithOptionalParameter() throws Exception {
+        Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
+        exchange.getIn().setHeader(SmppConstants.COMMAND, "SubmitSm");
+        exchange.getIn().setHeader(SmppConstants.ID, "1");
+        exchange.getIn().setHeader(SmppConstants.SOURCE_ADDR_TON, TypeOfNumber.NATIONAL.value());
+        exchange.getIn().setHeader(SmppConstants.SOURCE_ADDR_NPI, NumberingPlanIndicator.NATIONAL.value());
+        exchange.getIn().setHeader(SmppConstants.SOURCE_ADDR, "1818");
+        exchange.getIn().setHeader(SmppConstants.DEST_ADDR_TON, TypeOfNumber.INTERNATIONAL.value());
+        exchange.getIn().setHeader(SmppConstants.DEST_ADDR_NPI, NumberingPlanIndicator.INTERNET.value());
+        exchange.getIn().setHeader(SmppConstants.DEST_ADDR, "1919");
+        exchange.getIn().setHeader(SmppConstants.SCHEDULE_DELIVERY_TIME, new Date(1111111));
+        exchange.getIn().setHeader(SmppConstants.VALIDITY_PERIOD, new Date(2222222));
+        exchange.getIn().setHeader(SmppConstants.PROTOCOL_ID, (byte) 1);
+        exchange.getIn().setHeader(SmppConstants.PRIORITY_FLAG, (byte) 2);
+        exchange.getIn().setHeader(SmppConstants.REGISTERED_DELIVERY, new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS).value());
+        exchange.getIn().setHeader(SmppConstants.REPLACE_IF_PRESENT_FLAG, ReplaceIfPresentFlag.REPLACE.value());
+        exchange.getIn().setBody("short message body");
+        Map<String, String> optionalParameters = new LinkedHashMap<String, String>();
+        optionalParameters.put("SOURCE_SUBADDRESS", "1292");
+        optionalParameters.put("ADDITIONAL_STATUS_INFO_TEXT", "urgent");
+        optionalParameters.put("DEST_ADDR_SUBUNIT", "4");
+        optionalParameters.put("DEST_TELEMATICS_ID", "2");
+        optionalParameters.put("QOS_TIME_TO_LIVE", "3600000");
+        optionalParameters.put("ALERT_ON_MESSAGE_DELIVERY", null);
+        exchange.getIn().setHeader(SmppConstants.OPTIONAL_PARAMETERS, optionalParameters);
+        expect(session.submitShortMessage(eq("CMT"), eq(TypeOfNumber.NATIONAL), eq(NumberingPlanIndicator.NATIONAL),
eq("1818"),
+                eq(TypeOfNumber.INTERNATIONAL), eq(NumberingPlanIndicator.INTERNET), eq("1919"),
+                eq(new ESMClass()), eq((byte) 1), eq((byte) 2), eq("-300101001831100-"),
eq("-300101003702200-"), eq(new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS)),
+                eq(ReplaceIfPresentFlag.REPLACE.value()), eq(DataCoding.newInstance((byte)
0)), eq((byte) 0),
+                aryEq("short message body".getBytes()), eq(new OptionalParameter.OctetString(Tag.SOURCE_SUBADDRESS,
"1292")),
+                eq(new OptionalParameter.COctetString(Tag.ADDITIONAL_STATUS_INFO_TEXT.code(),
"urgent")), eq(new OptionalParameter.Byte(Tag.DEST_ADDR_SUBUNIT, (byte) 4)),
+                eq(new OptionalParameter.Short(Tag.DEST_TELEMATICS_ID.code(), (short) 2)),
eq(new OptionalParameter.Int(Tag.QOS_TIME_TO_LIVE, 3600000)),
+                eq(new OptionalParameter.Null(Tag.ALERT_ON_MESSAGE_DELIVERY))))
+                .andReturn("1");
+
+        replay(session);
+
+        command.execute(exchange);
+
+        verify(session);
+
+        assertEquals(Arrays.asList("1"), exchange.getOut().getHeader(SmppConstants.ID));
+        assertEquals(1, exchange.getOut().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
+    }
+
     @Test
     public void executeWithValidityPeriodAsString() throws Exception {
         Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
@@ -193,13 +246,13 @@ public class SmppSubmitSmCommandTest {
                 eq(new ESMClass()), eq((byte) 1), eq((byte) 2), eq("-300101001831100-"),
eq("000003000000000R"), eq(new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS)),
                 eq(ReplaceIfPresentFlag.REPLACE.value()), eq(DataCoding.newInstance((byte)
0)), eq((byte) 0), aryEq("short message body".getBytes())))
                 .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
-        
+
         assertEquals(Arrays.asList("1"), exchange.getOut().getHeader(SmppConstants.ID));
         assertEquals(1, exchange.getOut().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
     }
@@ -209,8 +262,7 @@ public class SmppSubmitSmCommandTest {
         final byte incorrectDataCoding = 0x00;
         byte[] body = {'A', 'B', 'C'};
 
-        Exchange exchange = new DefaultExchange(new DefaultCamelContext(),
-                                                ExchangePattern.InOut);
+        Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
         exchange.getIn().setHeader(SmppConstants.COMMAND, "SubmitSm");
         exchange.getIn().setHeader(SmppConstants.ALPHABET, Alphabet.ALPHA_8_BIT.value());
         exchange.getIn().setBody(body);
@@ -218,14 +270,14 @@ public class SmppSubmitSmCommandTest {
                 eq("1717"), eq(new ESMClass()), eq((byte) 0), eq((byte) 1), (String) isNull(),
(String) isNull(), eq(new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE)),
                 eq(ReplaceIfPresentFlag.DEFAULT.value()), not(eq(DataCoding.newInstance(incorrectDataCoding))),
eq((byte) 0), aryEq(body)))
                 .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
-    
+
     @Test
     public void bodyWithSmscDefaultDataCodingNarrowedToCharset() throws Exception {
         final int dataCoding = 0x00; /* SMSC-default */
@@ -254,11 +306,11 @@ public class SmppSubmitSmCommandTest {
                                           eq((byte) 0),
                                           aryEq(bodyNarrowed)))
             .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
 
@@ -290,11 +342,11 @@ public class SmppSubmitSmCommandTest {
                                           eq((byte) 0),
                                           aryEq(bodyNarrowed)))
             .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
 
@@ -327,9 +379,9 @@ public class SmppSubmitSmCommandTest {
             .andReturn("1");
 
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
 
@@ -360,11 +412,11 @@ public class SmppSubmitSmCommandTest {
                                           eq((byte) 0),
                                           aryEq(body)))
             .andReturn("1");
-        
+
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
 
@@ -398,9 +450,9 @@ public class SmppSubmitSmCommandTest {
             .andReturn("1");
 
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
 
@@ -435,9 +487,19 @@ public class SmppSubmitSmCommandTest {
             .andReturn("1");
 
         replay(session);
-        
+
         command.execute(exchange);
-        
+
         verify(session);
     }
+
+    @Test
+    public void determineTypeClass() throws Exception {
+        assertSame(OctetString.class, command.determineTypeClass(Tag.SOURCE_SUBADDRESS));
+        assertSame(COctetString.class, command.determineTypeClass(Tag.ADDITIONAL_STATUS_INFO_TEXT));
+        assertSame(org.jsmpp.bean.OptionalParameter.Byte.class, command.determineTypeClass(Tag.DEST_ADDR_SUBUNIT));
+        assertSame(org.jsmpp.bean.OptionalParameter.Short.class, command.determineTypeClass(Tag.DEST_TELEMATICS_ID));
+        assertSame(org.jsmpp.bean.OptionalParameter.Int.class, command.determineTypeClass(Tag.QOS_TIME_TO_LIVE));
+        assertSame(org.jsmpp.bean.OptionalParameter.Null.class, command.determineTypeClass(Tag.ALERT_ON_MESSAGE_DELIVERY));
+    }
 }
\ No newline at end of file



Mime
View raw message