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    
021    import org.apache.camel.impl.DefaultMessage;
022    import org.jsmpp.bean.AlertNotification;
023    import org.jsmpp.bean.Command;
024    import org.jsmpp.bean.DataSm;
025    import org.jsmpp.bean.DeliverSm;
026    import org.jsmpp.bean.MessageRequest;
027    
028    /**
029     * Represents a {@link org.apache.camel.Message} for working with SMPP
030     * 
031     * @author muellerc
032     * @version $Revision: 1054271 $
033     */
034    public class SmppMessage extends DefaultMessage {
035    
036        private Command command;
037        private SmppConfiguration configuration;
038        
039        public SmppMessage(SmppConfiguration configuration) {
040            this.configuration = configuration;
041        }
042    
043        public SmppMessage(AlertNotification command, SmppConfiguration configuration) {
044            this.command = command;
045            this.configuration = configuration;
046        }
047        
048        public SmppMessage(DeliverSm command, SmppConfiguration configuration) {
049            this.command = command;
050            this.configuration = configuration;
051        }
052    
053        public SmppMessage(DataSm dataSm, SmppConfiguration configuration) {
054            this.command = dataSm;
055            this.configuration = configuration;
056        }
057    
058        @Override
059        public SmppMessage newInstance() {
060            return new SmppMessage(this.configuration);
061        }
062        
063        public boolean isAlertNotification() {
064            return command instanceof AlertNotification;
065        }
066        
067        public boolean isDataSm() {
068            return command instanceof DataSm;
069        }
070        
071        public boolean isDeliverSm() {
072            return command instanceof DeliverSm && !((DeliverSm) command).isSmscDeliveryReceipt();
073        }
074        
075        public boolean isDeliveryReceipt() {
076            return command instanceof DeliverSm && ((DeliverSm) command).isSmscDeliveryReceipt();
077        }
078    
079        @Override
080        protected Object createBody() {
081            if (command instanceof MessageRequest) {
082                byte[] shortMessage = ((MessageRequest) command).getShortMessage();
083                try {
084                    return new String(shortMessage, configuration.getEncoding());
085                } catch (UnsupportedEncodingException e) {
086                    return new String(shortMessage);
087                }
088            }
089    
090            return null;
091        }
092    
093        @Override
094        public String toString() {
095            if (command != null) {
096                return "SmppMessage: " + command;
097            } else {
098                return "SmppMessage: " + getBody();
099            }
100        }
101    
102        /**
103         * Returns the underlying jSMPP command
104         * 
105         * @return command
106         */
107        public Command getCommand() {
108            return command;
109        }
110    }