001/*
002 *   Copyright 2020 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.sms.messages;
017
018
019import org.apache.http.client.methods.RequestBuilder;
020
021/**
022 * Represents the details of a plain-text message that is to be submitted via the Vonage REST api.
023 *
024 * @author Paul Cook
025 */
026public class TextMessage extends Message {
027    private final String messageBody;
028
029    private boolean unicode;
030
031    /**
032     * Instantiate a new text-message request.<br>
033     * This message will be submitted as a regular 8 bit text message
034     *
035     * @param from        the 'from' address that will be seen on the handset when this message arrives,
036     *                    typically either a valid short-code / long code that can be replied to, or a short text
037     *                    description of the application sending the message (Max 11 chars)
038     * @param to          the phone number of the handset that you wish to send the message to
039     * @param messageBody The text of the message to be sent to the handset
040     */
041    public TextMessage(final String from,
042                       final String to,
043                       final String messageBody) {
044        this(from, to, messageBody, false);
045    }
046
047    /**
048     * Instantiate a new text-message request.<br>
049     * This message will be submitted as a regular 8 bit text message
050     *
051     * @param from        the 'from' address that will be seen on the handset when this message arrives,
052     *                    typically either a valid short-code / long code that can be replied to, or a short text
053     *                    description of the application sending the message (Max 11 chars)
054     * @param to          the phone number of the handset that you wish to send the message to
055     * @param messageBody The text of the message to be sent to the handset
056     */
057    public TextMessage(final String from,
058                       final String to,
059                       final String messageBody,
060                       final boolean unicode) {
061        super(null, from, to);
062        this.messageBody = messageBody;
063        this.unicode = unicode;
064    }
065
066    /**
067     * @return String The text of the message to be sent to the handset
068     */
069    public String getMessageBody() {
070        return this.messageBody;
071    }
072
073    /**
074     * @return boolean This flag is set to true if the message needs to be submitted as a unicode message. This would
075     * be for scenario's where the message contains text that does not fit within the Latin GSM alphabet. Examples
076     * would be messages to be sent in non-western scripts, such as Arabic, Kanji, Chinese, etc.
077     */
078    public boolean isUnicode() {
079        return this.unicode;
080    }
081
082    @Override
083    public MessageType getType() {
084        if (unicode) {
085            return MessageType.UNICODE;
086        } else {
087            return MessageType.TEXT;
088        }
089    }
090
091    @Override
092    public void addParams(RequestBuilder request) {
093        super.addParams(request);
094        request.addParameter("text", messageBody);
095    }
096}