001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.sms.messages;
023
024
025import org.apache.http.client.methods.RequestBuilder;
026
027/**
028 * Represents the details common to any message that is to be submitted to the Nexmo SMS API.
029 */
030public abstract class Message {
031    public enum MessageType {
032        /**
033         * Message is a regular TEXT SMS message
034         */
035        TEXT,
036        /**
037         * Message is a binary SMS message with a custom UDH and binary payload
038         */
039        BINARY,
040        /**
041         * Message is a wap-push message to send a browsable / downloadable url to the handset
042         */
043        WAPPUSH,
044        /**
045         * Message is a unicode message, for sending messages in non-latin script to a supported handset
046         */
047        UNICODE;
048
049        public String toString() {
050            return super.toString().toLowerCase();
051        }
052    }
053
054    private final MessageType type;
055    private final String from;
056    private final String to;
057
058    private String clientReference;
059    private boolean statusReportRequired;
060    private MessageClass messageClass = null;
061    private Long timeToLive = null;
062    private String callbackUrl = null;
063
064    protected Message(final MessageType type,
065                      final String from,
066                      final String to) {
067        this(type, from, to, false);
068    }
069
070    /**
071     * Abstract type for more specific SMS message types.<br>
072     * This constructor exposes the full range of possible parameters and is not for general use
073     * Instead, it is accessed via super() in the constructors of various sub-classes that expose a relevant
074     * sub-set of the available parameters
075     *
076     * @param type the type of SMS message to be sent
077     * @param from the 'from' address that will be seen on the handset when this message arrives, typically either a
078     *             valid short-code / long code that can be replied to, or a short text description of the application
079     *             sending the message (Max 15 chars)
080     * @param to   the phone number of the handset you wish to send the message to
081     */
082    protected Message(final MessageType type,
083                      final String from,
084                      final String to,
085                      final boolean statusReportRequired) {
086        this.type = type;
087        this.from = from;
088        this.to = to;
089        this.statusReportRequired = statusReportRequired;
090    }
091
092    /**
093     * @return int the type of message will influence the makeup of the request we post to the Nexmo server, and also the action taken by the Nexmo server in response to this message
094     */
095    public MessageType getType() {
096        return this.type;
097    }
098
099    /**
100     * @return String the 'from' address that will be seen on the handset when this message arrives,
101     * typically either a valid short-code / long code that can be replied to, or a short text description of the application sending the message (Max 11 chars)
102     */
103    public String getFrom() {
104        return this.from;
105    }
106
107    /**
108     * @return String the phone number of the handset that you wish to send the message to
109     */
110    public String getTo() {
111        return this.to;
112    }
113
114    /**
115     * @return String A user definable value that will be stored in the Nexmo sms records. It will
116     * be available in detailed reporting &amp; analytics in order to help with reconciliation of messages
117     */
118    public String getClientReference() {
119        return this.clientReference;
120    }
121
122    public void setClientReference(String clientReference) {
123        if (clientReference.length() > 40) {
124            throw new IllegalArgumentException("Client reference must be 40 characters or less.");
125        }
126        this.clientReference = clientReference;
127    }
128
129    /**
130     * @return com.nexmo.sms.verify.messages.parameters.MessageClass The message class that is to be applied to this message.
131     */
132    public MessageClass getMessageClass() {
133        return this.messageClass;
134    }
135
136    public void setMessageClass(MessageClass messageClass) {
137        this.messageClass = messageClass;
138    }
139
140    public Long getTimeToLive() {
141        return timeToLive;
142    }
143
144    public void setTimeToLive(Long timeToLive) {
145        this.timeToLive = timeToLive;
146    }
147
148    public String getCallbackUrl() {
149        return callbackUrl;
150    }
151
152    public void setCallbackUrl(String callbackUrl) {
153        this.callbackUrl = callbackUrl;
154    }
155
156    /**
157     * Get the value of the 'status-report-req' parameter.
158     */
159    public boolean getStatusReportRequired() {
160        return this.statusReportRequired;
161    }
162
163    /**
164     * Set the value of the 'status-report-req' parameter.
165     *
166     * If set to 'true', Nexmo will call 'callbackUrl' with status updates about the delivery of this message. If this
167     * value is set to 'true', then 'callbackUrl' should also be set to a URL that is configured to receive these
168     * status updates.
169     *
170     * @param statusReportRequired 'true' if status reports are desired, 'false' otherwise.
171     */
172    public void setStatusReportRequired(boolean statusReportRequired) {
173        this.statusReportRequired = statusReportRequired;
174    }
175
176    public void addParams(RequestBuilder request) {
177        request.addParameter("from", getFrom())
178                .addParameter("to", getTo())
179                .addParameter("type", getType().toString());
180        if (statusReportRequired) {
181            request.addParameter("status-report-req", "1");
182        }
183        if (clientReference != null) {
184            request.addParameter("client-ref", clientReference);
185        }
186        if (timeToLive != null) {
187            request.addParameter("ttl", timeToLive.toString());
188        }
189        if (callbackUrl != null) {
190            request.addParameter("callback", callbackUrl);
191        }
192        if (messageClass != null) {
193            request.addParameter("message-class", Integer.toString(messageClass.getMessageClass()));
194        }
195    }
196
197    /**
198     * An enum of the valid values that may be supplied to as the message-class parameter of a rest submission.
199     *
200     * @author  Paul Cook
201     */
202    public enum MessageClass {
203
204        /**
205         * Message Class 0
206         */
207        CLASS_0(0),
208
209        /**
210         * Message Class 1
211         */
212        CLASS_1(1),
213
214        /**
215         * Message Class 2
216         */
217        CLASS_2(2),
218
219        /**
220         * Message Class 3
221         */
222        CLASS_3(3);
223
224        private final int messageClass;
225
226        MessageClass(int messageClass) {
227            this.messageClass = messageClass;
228        }
229
230        public int getMessageClass() {
231            return this.messageClass;
232        }
233
234    }
235}