001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.smpp;
018    
019    import java.io.UnsupportedEncodingException;
020    import java.util.Arrays;
021    import java.util.Date;
022    import java.util.List;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Message;
026    import org.jsmpp.bean.AlertNotification;
027    import org.jsmpp.bean.Command;
028    import org.jsmpp.bean.DataSm;
029    import org.jsmpp.bean.DeliverSm;
030    import org.jsmpp.bean.DeliveryReceipt;
031    import org.jsmpp.bean.OptionalParameter;
032    import org.jsmpp.bean.OptionalParameter.OctetString;
033    import org.jsmpp.bean.SubmitSm;
034    import org.jsmpp.util.AbsoluteTimeFormatter;
035    import org.jsmpp.util.TimeFormatter;
036    
037    /**
038     * A Strategy used to convert between a Camel {@link Exchange} and
039     * {@link SmppMessage} to and from a SMPP {@link Command}
040     * 
041     * @version $Revision: 1054271 $
042     * @author muellerc
043     */
044    public class SmppBinding {
045    
046        public static final String SEQUENCE_NUMBER = "CamelSmppSequenceNumber";
047        public static final String SUBMITTED = "CamelSmppSubmitted";
048        public static final String SUBMIT_DATE = "CamelSmppSubmitDate";
049        public static final String ERROR = "CamelSmppError";
050        public static final String DONE_DATE = "CamelSmppDoneDate";
051        public static final String DELIVERED = "CamelSmppDelivered";
052        public static final String COMMAND_ID = "CamelSmppCommandId";
053        public static final String COMMAND_STATUS = "CamelSmppCommandStatus";
054        public static final String ID = "CamelSmppId";
055        public static final String REPLACE_IF_PRESENT_FLAG = "CamelSmppReplaceIfPresentFlag";
056        public static final String VALIDITY_PERIOD = "CamelSmppValidityPeriod";
057        public static final String SCHEDULE_DELIVERY_TIME = "CamelSmppScheduleDeliveryTime";
058        public static final String PRIORITY_FLAG = "CamelSmppPriorityFlag";
059        public static final String PROTOCOL_ID = "CamelSmppProtocolId";
060        public static final String REGISTERED_DELIVERY = "CamelSmppRegisteredDelivery";
061        public static final String SERVICE_TYPE = "CamelSmppServiceType";
062        public static final String SOURCE_ADDR_NPI = "CamelSmppSourceAddrNpi";
063        public static final String SOURCE_ADDR_TON = "CamelSmppSourceAddrTon";
064        public static final String SOURCE_ADDR = "CamelSmppSourceAddr";
065        public static final String DEST_ADDR_NPI = "CamelSmppDestAddrNpi";
066        public static final String DEST_ADDR_TON = "CamelSmppDestAddrTon";
067        public static final String DEST_ADDR = "CamelSmppDestAddr";
068        public static final String ESME_ADDR_NPI = "CamelSmppEsmeAddrNpi";
069        public static final String ESME_ADDR_TON = "CamelSmppEsmeAddrTon";
070        public static final String ESME_ADDR = "CamelSmppEsmeAddr";
071        public static final String FINAL_STATUS = "CamelSmppStatus";
072        public static final String DATA_CODING = "CamelSmppDataCoding";
073        public static final String MESSAGE_TYPE = "CamelSmppMessageType";
074    
075        private static TimeFormatter timeFormatter = new AbsoluteTimeFormatter();
076    
077        private SmppConfiguration configuration;
078    
079        public SmppBinding() {
080            this.configuration = new SmppConfiguration();
081        }
082    
083        public SmppBinding(SmppConfiguration configuration) {
084            this.configuration = configuration;
085        }
086    
087        /**
088         * Create the SubmitSm object from the inbound exchange
089         * 
090         * @throws UnsupportedEncodingException if the encoding is not supported
091         */
092        public SubmitSm createSubmitSm(Exchange exchange) throws UnsupportedEncodingException {
093            Message in = exchange.getIn();
094    
095            SubmitSm submitSm = new SubmitSm();
096            submitSm.setShortMessage(exchange.getIn().getBody(String.class).getBytes(configuration.getEncoding()));
097    
098            if (in.getHeaders().containsKey(DEST_ADDR)) {
099                submitSm.setDestAddress((String) in.getHeader(DEST_ADDR));
100            } else {
101                submitSm.setDestAddress(configuration.getDestAddr());
102            }
103    
104            if (in.getHeaders().containsKey(DEST_ADDR_TON)) {
105                submitSm.setDestAddrTon((Byte) in.getHeader(DEST_ADDR_TON));
106            } else {
107                submitSm.setDestAddrTon(configuration.getDestAddrTon());
108            }
109    
110            if (in.getHeaders().containsKey(DEST_ADDR_NPI)) {
111                submitSm.setDestAddrNpi((Byte) in.getHeader(DEST_ADDR_NPI));
112            } else {
113                submitSm.setDestAddrNpi(configuration.getDestAddrNpi());
114            }
115    
116            if (in.getHeaders().containsKey(SOURCE_ADDR)) {
117                submitSm.setSourceAddr((String) in.getHeader(SOURCE_ADDR));
118            } else {
119                submitSm.setSourceAddr(configuration.getSourceAddr());
120            }
121    
122            if (in.getHeaders().containsKey(SOURCE_ADDR_TON)) {
123                submitSm.setSourceAddrTon((Byte) in.getHeader(SOURCE_ADDR_TON));
124            } else {
125                submitSm.setSourceAddrTon(configuration.getSourceAddrTon());
126            }
127    
128            if (in.getHeaders().containsKey(SOURCE_ADDR_NPI)) {
129                submitSm.setSourceAddrNpi((Byte) in.getHeader(SOURCE_ADDR_NPI));
130            } else {
131                submitSm.setSourceAddrNpi(configuration.getSourceAddrNpi());
132            }
133    
134            if (in.getHeaders().containsKey(SERVICE_TYPE)) {
135                submitSm.setServiceType((String) in.getHeader(SERVICE_TYPE));
136            } else {
137                submitSm.setServiceType(configuration.getServiceType());
138            }
139    
140            if (in.getHeaders().containsKey(REGISTERED_DELIVERY)) {
141                submitSm.setRegisteredDelivery((Byte) in.getHeader(REGISTERED_DELIVERY));
142            } else {
143                submitSm.setRegisteredDelivery(configuration.getRegisteredDelivery());
144            }
145    
146            if (in.getHeaders().containsKey(PROTOCOL_ID)) {
147                submitSm.setProtocolId((Byte) in.getHeader(PROTOCOL_ID));
148            } else {
149                submitSm.setProtocolId(configuration.getProtocolId());
150            }
151    
152            if (in.getHeaders().containsKey(PRIORITY_FLAG)) {
153                submitSm.setPriorityFlag((Byte) in.getHeader(PRIORITY_FLAG));
154            } else {
155                submitSm.setPriorityFlag(configuration.getPriorityFlag());
156            }
157    
158            if (in.getHeaders().containsKey(SCHEDULE_DELIVERY_TIME)) {
159                submitSm.setScheduleDeliveryTime(timeFormatter.format((Date) in.getHeader(SCHEDULE_DELIVERY_TIME)));
160            } 
161    
162            if (in.getHeaders().containsKey(VALIDITY_PERIOD)) {
163                submitSm.setValidityPeriod(timeFormatter.format((Date) in.getHeader(VALIDITY_PERIOD)));
164            }
165    
166            if (in.getHeaders().containsKey(REPLACE_IF_PRESENT_FLAG)) {
167                submitSm.setReplaceIfPresent((Byte) in.getHeader(REPLACE_IF_PRESENT_FLAG));
168            } else {
169                submitSm.setReplaceIfPresent(configuration.getReplaceIfPresentFlag());
170            }
171            
172            if (in.getHeaders().containsKey(DATA_CODING)) {
173                System.out.println("1");
174                submitSm.setDataCoding((Byte) in.getHeader(DATA_CODING));
175            } else {
176                System.out.println("2");
177                submitSm.setDataCoding(configuration.getDataCoding());
178            }
179    
180            return submitSm;
181        }
182    
183        /**
184         * Create a new SmppMessage from the inbound alert notification
185         */
186        public SmppMessage createSmppMessage(AlertNotification alertNotification) {
187            SmppMessage smppMessage = new SmppMessage(alertNotification, configuration);
188    
189            smppMessage.setHeader(MESSAGE_TYPE, SmppMessageType.AlertNotification.toString());
190            smppMessage.setHeader(SEQUENCE_NUMBER, alertNotification.getSequenceNumber());
191            smppMessage.setHeader(COMMAND_ID, alertNotification.getCommandId());
192            smppMessage.setHeader(COMMAND_STATUS, alertNotification.getCommandStatus());
193            smppMessage.setHeader(SOURCE_ADDR, alertNotification.getSourceAddr());
194            smppMessage.setHeader(SOURCE_ADDR_NPI, alertNotification.getSourceAddrNpi());
195            smppMessage.setHeader(SOURCE_ADDR_TON, alertNotification.getSourceAddrTon());
196            smppMessage.setHeader(ESME_ADDR, alertNotification.getEsmeAddr());
197            smppMessage.setHeader(ESME_ADDR_NPI, alertNotification.getEsmeAddrNpi());
198            smppMessage.setHeader(ESME_ADDR_TON, alertNotification.getEsmeAddrTon());
199    
200            return smppMessage;
201        }
202    
203        /**
204         * Create a new SmppMessage from the inbound deliver sm or deliver receipt
205         */
206        public SmppMessage createSmppMessage(DeliverSm deliverSm) throws Exception {
207            SmppMessage smppMessage = new SmppMessage(deliverSm, configuration);
208    
209            if (deliverSm.isSmscDeliveryReceipt()) {
210                smppMessage.setHeader(MESSAGE_TYPE, SmppMessageType.DeliveryReceipt.toString());
211                DeliveryReceipt smscDeliveryReceipt = deliverSm.getShortMessageAsDeliveryReceipt();
212                smppMessage.setBody(smscDeliveryReceipt.getText());
213    
214                smppMessage.setHeader(ID, smscDeliveryReceipt.getId());
215                smppMessage.setHeader(DELIVERED, smscDeliveryReceipt.getDelivered());
216                smppMessage.setHeader(DONE_DATE, smscDeliveryReceipt.getDoneDate());
217                if (!"000".equals(smscDeliveryReceipt.getError())) {
218                    smppMessage.setHeader(ERROR, smscDeliveryReceipt.getError());
219                }
220                smppMessage.setHeader(SUBMIT_DATE, smscDeliveryReceipt.getSubmitDate());
221                smppMessage.setHeader(SUBMITTED, smscDeliveryReceipt.getSubmitted());
222                smppMessage.setHeader(FINAL_STATUS, smscDeliveryReceipt.getFinalStatus());
223            } else {
224                smppMessage.setHeader(MESSAGE_TYPE, SmppMessageType.DeliverSm.toString());
225                if (deliverSm.getShortMessage() != null) {
226                    smppMessage.setBody(String.valueOf(new String(deliverSm.getShortMessage(),
227                            configuration.getEncoding())));
228                } else if (deliverSm.getOptionalParametes() != null && deliverSm.getOptionalParametes().length > 0) {
229                    List<OptionalParameter> oplist = Arrays.asList(deliverSm.getOptionalParametes());
230    
231                    for (OptionalParameter optPara : oplist) {
232                        if (OptionalParameter.Tag.MESSAGE_PAYLOAD.code() == optPara.tag && OctetString.class.isInstance(optPara)) {
233                            smppMessage.setBody(((OctetString) optPara).getValueAsString());
234                            break;
235                        }
236                    }
237                }
238    
239                smppMessage.setHeader(SEQUENCE_NUMBER, deliverSm.getSequenceNumber());
240                smppMessage.setHeader(COMMAND_ID, deliverSm.getCommandId());
241                smppMessage.setHeader(SOURCE_ADDR, deliverSm.getSourceAddr());
242                smppMessage.setHeader(DEST_ADDR, deliverSm.getDestAddress());
243                smppMessage.setHeader(SCHEDULE_DELIVERY_TIME, deliverSm.getScheduleDeliveryTime());
244                smppMessage.setHeader(VALIDITY_PERIOD, deliverSm.getValidityPeriod());
245                smppMessage.setHeader(SERVICE_TYPE, deliverSm.getServiceType());
246            }
247    
248            return smppMessage;
249        }
250        
251        public SmppMessage createSmppMessage(DataSm dataSm, String smppMessageId) {
252            SmppMessage smppMessage = new SmppMessage(dataSm, configuration);
253    
254            smppMessage.setHeader(MESSAGE_TYPE, SmppMessageType.DataSm.toString());
255            smppMessage.setHeader(ID, smppMessageId);
256            smppMessage.setHeader(SEQUENCE_NUMBER, dataSm.getSequenceNumber());
257            smppMessage.setHeader(COMMAND_ID, dataSm.getCommandId());
258            smppMessage.setHeader(COMMAND_STATUS, dataSm.getCommandStatus());
259            smppMessage.setHeader(SOURCE_ADDR, dataSm.getSourceAddr());
260            smppMessage.setHeader(SOURCE_ADDR_NPI, dataSm.getSourceAddrNpi());
261            smppMessage.setHeader(SOURCE_ADDR_TON, dataSm.getSourceAddrTon());
262            smppMessage.setHeader(DEST_ADDR, dataSm.getDestAddress());
263            smppMessage.setHeader(DEST_ADDR_NPI, dataSm.getDestAddrNpi());
264            smppMessage.setHeader(DEST_ADDR_TON, dataSm.getDestAddrTon());
265            smppMessage.setHeader(SERVICE_TYPE, dataSm.getServiceType());
266            smppMessage.setHeader(REGISTERED_DELIVERY, dataSm.getRegisteredDelivery());
267            smppMessage.setHeader(DATA_CODING, dataSm.getDataCoding());
268    
269            return smppMessage;
270        }
271        
272        /**
273         * Returns the current date. Externalized for better test support.
274         * 
275         * @return the current date
276         */
277        Date getCurrentDate() {
278            return new Date();
279        }
280    
281        /**
282         * Returns the smpp configuration
283         * 
284         * @return the configuration
285         */
286        public SmppConfiguration getConfiguration() {
287            return configuration;
288        }
289    
290        /**
291         * Set the smpp configuration.
292         * 
293         * @param configuration smppConfiguration
294         */
295        public void setConfiguration(SmppConfiguration configuration) {
296            this.configuration = configuration;
297        }
298    }