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.verify2;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.common.E164;
020
021/**
022 * Defines workflow properties for sending a verification code to a user via SMS.
023 */
024public final class SmsWorkflow extends AbstractNumberWorkflow {
025        final String appHash, contentId, entityId;
026
027        SmsWorkflow(Builder builder) {
028                super(builder);
029                if ((this.appHash = builder.appHash) != null && appHash.length() != 11) {
030                        throw new IllegalArgumentException("Android app hash must be 11 characters.");
031                }
032                if ((this.contentId = builder.contentId) != null && (contentId.isEmpty() || contentId.length() > 20)) {
033                        throw new IllegalArgumentException("content_id must be between 1 and 20 characters long.");
034                }
035                if ((this.entityId = builder.entityId) != null && (entityId.isEmpty() || entityId.length() > 20)) {
036                        throw new IllegalArgumentException("entity_id must be between 1 and 20 characters long.");
037                }
038        }
039
040        /**
041         * Constructs a new SMS verification workflow.
042         *
043         * @param to The number to send the message to, in E.164 format.
044         */
045        public SmsWorkflow(String to) {
046                this(builder(to));
047        }
048
049        /**
050         * Constructs a new SMS verification workflow.
051         *
052         * @param to The number to send the message to, in E.164 format.
053         * @param appHash Android Application Hash Key for automatic code detection on a user's device.
054         *
055         * @deprecated Use {@linkplain #builder(String)}.
056         */
057        @Deprecated
058        public SmsWorkflow(String to, String appHash) {
059                this(builder(to).appHash(appHash));
060        }
061
062        /**
063         * Constructs a new SMS verification workflow.
064         *
065         * @param to The number to send the message to, in E.164 format.
066         * @param from The number or sender ID to send the SMS from.
067         * @param appHash Android Application Hash Key for automatic code detection on a user's device.
068         *
069         * @since 8.1.0
070         * @deprecated Use {@linkplain #builder(String)} instead.
071         */
072        @Deprecated
073        public SmsWorkflow(String to, String from, String appHash) {
074                this(builder(to).from(from).appHash(appHash));
075        }
076
077        @Override
078        protected String validateFrom(String from) {
079                if ((from = super.validateFrom(from)) == null) {
080                        return null;
081                }
082                else if (from.length() > 11) {
083                        return new E164(from).toString();
084                }
085                else {
086                        return from;
087                }
088        }
089
090        /**
091         * Android Application Hash Key for automatic code detection on a user's device.
092         *
093         * @return The Android application hash key (11 characters in length), or {@code null} if not set.
094         */
095        @JsonProperty("app_hash")
096        public String getAppHash() {
097                return appHash;
098        }
099
100        /**
101         * The number or sender ID the message will be sent from.
102         *
103         * @return The sender phone number or sender ID, or {@code null} if unspecified.
104         *
105         * @since 8.1.0
106         */
107        @JsonProperty("from")
108        public String getFrom() {
109                return from;
110        }
111
112        /**
113         * Optional value corresponding to a TemplateID for SMS delivery using Indian Carriers.
114         *
115         * @return The content ID, or {@code null} if unspecified.
116         *
117         * @since 8.2.0
118         */
119        @JsonProperty("content_id")
120        public String getContentId() {
121                return contentId;
122        }
123
124        /**
125         * Optional PEID required for SMS delivery using Indian Carriers.
126         *
127         * @return The entity ID, or {@code null} if unspecified.
128         *
129         * @since 8.2.0
130         */
131        @JsonProperty("entity_id")
132        public String getEntityId() {
133                return entityId;
134        }
135
136        /**
137         * Entrypoint for constructing an instance of this class.
138         *
139         * @param to (REQUIRED) The destination phone number in E.164 format.
140         *
141         * @return A new Builder.
142         *
143         * @since 8.2.0
144         */
145        public static Builder builder(String to) {
146                return new Builder(to);
147        }
148
149        /**
150         * Builder class for an SMS workflow.
151         *
152         * @since 8.2.0
153         */
154        public static final class Builder extends AbstractNumberWorkflow.Builder<SmsWorkflow, Builder> {
155                private String from, appHash, contentId, entityId;
156
157                private Builder(String to) {
158                        super(Channel.SMS, to);
159                }
160
161                /**
162                 * (OPTIONAL) The number or ID to send the SMS from.
163                 *
164                 * @param from The sender number in E.164 format, or an alphanumeric ID (50 characters max).
165                 *
166                 * @return This builder.
167                 */
168                @Override
169                public Builder from(String from) {
170                        return super.from(from);
171                }
172
173                /**
174                 * (OPTIONAL) Android Application Hash Key for automatic code detection on a user's device.
175                 *
176                 * @param appHash The 11 character Android app hash.
177                 *
178                 * @return This builder.
179                 */
180                public Builder appHash(String appHash) {
181                        this.appHash = appHash;
182                        return this;
183                }
184
185                /**
186                 * (OPTIONAL) The TemplateID for SMS delivery using Indian Carriers.
187                 *
188                 * @param contentId The template ID (maximum 20 characters).
189                 *
190                 * @return This builder.
191                 */
192                public Builder contentId(String contentId) {
193                        this.contentId = contentId;
194                        return this;
195                }
196
197                /**
198                 * (OPTIONAL) PEID field required for SMS delivery using Indian Carriers.
199                 *
200                 * @param entityId The PEID (maximum 20 characters).
201                 *
202                 * @return This builder.
203                 */
204                public Builder entityId(String entityId) {
205                        this.entityId = entityId;
206                        return this;
207                }
208
209                @Override
210                public SmsWorkflow build() {
211                        return new SmsWorkflow(this);
212                }
213        }
214}