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 com.nexmo.client.sms.HexUtil;
026import org.apache.http.client.methods.RequestBuilder;
027
028/**
029 * A binary message to be submitted via the Nexmo SMS API.
030 */
031public class BinaryMessage extends Message {
032    private final byte[] messageBody;
033    private final byte[] udh;
034    private int protocolId = 0;
035
036    /**
037     * Instantiate a new binary sms message request.
038     *
039     * @param from              the 'from' address that will be seen on the handset when this message arrives,
040     *                          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)
041     * @param to                the phone number of the handset that you wish to send the message to
042     * @param messageBody The raw binary message data to be sent to a handset.
043     *                          This api, and the Nexmo sms service will send this data 'as-is' (in conjunction with the binary UDH) and will not make any corrections.
044     *                          so you should ensure that it is a correctly constructed message
045     * @param udh  Most binary content will require a UserDataHeader portion of the message containing commands to enable the handset to interpret the binary data
046     *                          (for example, a binary ringtone, a wap-push, OverTheAir configuration, etc).
047     *                          Additionally, if you are sending a long text message as multiple concatenated messages and are performing this operation manually rather than
048     *                          using the automated long sms handling in the Nexmo sms service, then you will need to construct and include here an appropriate
049     *                          UserDataHeader field that describes the segmentation/re-assembly fields required to successfully concatenate multiple short messages.
050     */
051    public BinaryMessage(final String from,
052                         final String to,
053                         final byte[] messageBody,
054                         final byte[] udh
055    ) {
056        super(MessageType.BINARY, from, to);
057        this.messageBody = messageBody;
058        this.udh = udh;
059    }
060
061    /**
062     * @return byte[] The raw binary message data to be sent to a handset.
063     * This api, and the Nexmo sms service will send this data 'as-is' (in conjunction with the binary UDH) and will not make any corrections.
064     * so you should ensure that it is a correctly constructed message
065     */
066    public byte[] getMessageBody() {
067        return this.messageBody == null ? null : this.messageBody.clone();
068    }
069
070    /**
071     * @return byte[] Most binary content will require a UserDataHeader portion of the message containing commands to enable the handset to interpret the binary data
072     * (for example, a binary ringtone, a wap-push, OverTheAir configuration, etc).
073     * Additionally, if you are sending a long text message as multiple concatenated messages and are performing this operation manually rather than
074     * using the automated long sms handling in the Nexmo sms service, then you will need to construct and include here an appropriate
075     * UserDataHeader field that describes the segmentation/re-assembly fields required to successfully concatenate multiple short messages.
076     */
077    public byte[] getUdh() {
078        return this.udh == null ? null : this.udh.clone();
079    }
080
081    /**
082     * @return Integer The value of the GSM Protocol ID field to be submitted with this message. Ordinarily this should be left as the default value of 0
083     */
084    public int getProtocolId() {
085        return this.protocolId;
086    }
087
088    public void setProtocolId(int protocolId) {
089        this.protocolId = protocolId;
090    }
091
092    @Override
093    public void addParams(RequestBuilder request) {
094        super.addParams(request);
095        request.addParameter("udh", HexUtil.bytesToHex(getUdh()));
096        request.addParameter("body", HexUtil.bytesToHex(getMessageBody()));
097        request.addParameter("protocol-id", Integer.toString(protocolId));
098    }
099}