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 of a plain-text message that is to be submitted via the Nexmo REST api.
029 *
030 * @author Paul Cook
031 */
032public class TextMessage extends Message {
033    private final String messageBody;
034
035    private boolean unicode = false;
036
037    /**
038     * Instantiate a new text-message request.<br>
039     * This message will be submitted as a regular 8 bit text message
040     *
041     * @param from        the 'from' address that will be seen on the handset when this message arrives,
042     *                    typically either a valid short-code / long code that can be replied to, or a short text
043     *                    description of the application sending the message (Max 11 chars)
044     * @param to          the phone number of the handset that you wish to send the message to
045     * @param messageBody The text of the message to be sent to the handset
046     */
047    public TextMessage(final String from,
048                       final String to,
049                       final String messageBody) {
050        this(from, to, messageBody, false);
051    }
052
053    /**
054     * Instantiate a new text-message request.<br>
055     * This message will be submitted as a regular 8 bit text message
056     *
057     * @param from        the 'from' address that will be seen on the handset when this message arrives,
058     *                    typically either a valid short-code / long code that can be replied to, or a short text
059     *                    description of the application sending the message (Max 11 chars)
060     * @param to          the phone number of the handset that you wish to send the message to
061     * @param messageBody The text of the message to be sent to the handset
062     */
063    public TextMessage(final String from,
064                       final String to,
065                       final String messageBody,
066                       final boolean unicode) {
067        super(null, from, to);
068        this.messageBody = messageBody;
069        this.unicode = unicode;
070    }
071
072    /**
073     * @return String The text of the message to be sent to the handset
074     */
075    public String getMessageBody() {
076        return this.messageBody;
077    }
078
079    /**
080     * @return boolean This flag is set to true if the message needs to be submitted as a unicode message. This would
081     * be for scenario's where the message contains text that does not fit within the Latin GSM alphabet. Examples
082     * would be messages to be sent in non-western scripts, such as Arabic, Kanji, Chinese, etc.
083     */
084    public boolean isUnicode() {
085        return this.unicode;
086    }
087
088    @Override
089    public MessageType getType() {
090        if (unicode) {
091            return MessageType.UNICODE;
092        } else {
093            return MessageType.TEXT;
094        }
095    }
096
097    @Override
098    public void addParams(RequestBuilder request) {
099        super.addParams(request);
100        request.addParameter("text", messageBody);
101    }
102}