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