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