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