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: 954209 $
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        @Override
064        protected Object createBody() {
065            if (command instanceof MessageRequest) {
066                byte[] shortMessage = ((MessageRequest) command).getShortMessage();
067                try {
068                    return new String(shortMessage, configuration.getEncoding());
069                } catch (UnsupportedEncodingException e) {
070                    return new String(shortMessage);
071                }
072            }
073    
074            return null;
075        }
076    
077        @Override
078        public String toString() {
079            if (command != null) {
080                return "SmppMessage: " + command;
081            } else {
082                return "SmppMessage: " + getBody();
083            }
084        }
085    
086        /**
087         * Returns the underlying jSMPP command
088         * 
089         * @return command
090         */
091        public Command getCommand() {
092            return command;
093        }
094    }