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.numbers;
017
018import com.fasterxml.jackson.annotation.JsonCreator;
019import java.net.URI;
020import java.util.*;
021
022/**
023 * Defines properties for updating an existing owned number.
024 */
025public class UpdateNumberRequest extends BaseNumberRequest {
026    private final UUID applicationId;
027    private CallbackType voiceCallbackType;
028    private URI moHttpUrl, voiceStatusCallback;
029    private String moSmppSysType, voiceCallbackValue,  messagesCallbackValue;
030
031    private UpdateNumberRequest(Builder builder) {
032        super(builder.country, builder.msisdn);
033        applicationId = builder.appId;
034        moHttpUrl = builder.moHttpUrl;
035        voiceStatusCallback = builder.voiceStatusCallback;
036        moSmppSysType = builder.moSmppSysType;
037        voiceCallbackType = builder.voiceCallbackType;
038        voiceCallbackValue = builder.voiceCallbackValue;
039    }
040
041    /**
042     * Deprecated constructor.
043     *
044     * @param msisdn The inbound virtual number to update.
045     * @param country The two character country code in ISO 3166-1 alpha-2 format.
046     * @deprecated Use {@link #builder(String, String)}. This will be removed in the next major release.
047     */
048    @Deprecated
049    public UpdateNumberRequest(String msisdn, String country) {
050        this(builder(msisdn, country));
051    }
052
053    /**
054     * ID of the application that will handle inbound traffic to this number.
055     *
056     * @return The application ID, or {@code null} if unspecified.
057     */
058    public UUID getApplicationId() {
059        return applicationId;
060    }
061
062    /**
063     * A URL-encoded URI to the webhook endpoint that handles inbound messages. Your webhook endpoint must
064     * be active before you make this request. Vonage makes a GET request to the endpoint and checks that
065     * it returns a 200 OK response. Set this parameter's value to an empty string to remove the webhook.
066     *
067     * @return The inbound message webhook URL as a string, or {@code null} if unspecified.
068     */
069    public String getMoHttpUrl() {
070        return moHttpUrl != null ? moHttpUrl.toString() : null;
071    }
072
073    /**
074     * The associated system type for your SMPP client.
075     *
076     * @return The SMPP system type as a string, or {@code null} if unspecified.
077     */
078    public String getMoSmppSysType() {
079        return moSmppSysType;
080    }
081
082    /**
083     * Specifies whether inbound voice calls on your number are forwarded to a SIP or a telephone number.
084     * If set, sip or tel are prioritized over the Voice capability in your Application.
085     *
086     * @return The voice callback type as an enum, or {@code null} if unspecified.
087     */
088    public CallbackType getVoiceCallbackType() {
089        return voiceCallbackType;
090    }
091
092    /**
093     * The SIP URI or telephone number.
094     *
095     * @return The voice callback value as a string, or {@code null} if unspecified.
096     */
097    public String getVoiceCallbackValue() {
098        return voiceCallbackValue;
099    }
100
101    /**
102     * The webhook URI for Vonage to send a request to when a call ends.
103     *
104     * @return The voice status callback URL as a string, or {@code null} if unspecified.
105     */
106    public String getVoiceStatusCallback() {
107        return voiceStatusCallback != null ? voiceStatusCallback.toString() : null;
108    }
109
110    @Deprecated
111    public void setMoHttpUrl(String moHttpUrl) {
112        this.moHttpUrl = URI.create(moHttpUrl);
113    }
114
115    @Deprecated
116    public void setMoSmppSysType(String moSmppSysType) {
117        this.moSmppSysType = moSmppSysType;
118    }
119
120    @Deprecated
121    public void setVoiceCallbackType(CallbackType voiceCallbackType) {
122        this.voiceCallbackType = voiceCallbackType;
123    }
124
125    @Deprecated
126    public void setVoiceCallbackValue(String voiceCallbackValue) {
127        this.voiceCallbackValue = voiceCallbackValue;
128    }
129
130    @Deprecated
131    public void setVoiceStatusCallback(String voiceStatusCallback) {
132        this.voiceStatusCallback = URI.create(voiceStatusCallback);
133    }
134
135    @Deprecated
136    public String getMessagesCallbackValue() {
137        return messagesCallbackValue;
138    }
139
140    @Deprecated
141    public void setMessagesCallbackValue(String messagesCallbackValue) {
142        this.messagesCallbackValue = messagesCallbackValue;
143    }
144
145    @Override
146    public Map<String, String> makeParams() {
147        Map<String, String> params = super.makeParams();
148        if (applicationId != null) {
149            params.put("app_id", applicationId.toString());
150        }
151        if (moHttpUrl != null) {
152            params.put("moHttpUrl", moHttpUrl.toString());
153        }
154        if (moSmppSysType != null) {
155            params.put("moSmppSysType", moSmppSysType);
156        }
157        if (voiceCallbackType != null) {
158            params.put("voiceCallbackType", voiceCallbackType.toString());
159        }
160        if (voiceCallbackValue != null) {
161            params.put("voiceCallbackValue", voiceCallbackValue);
162        }
163        if (voiceStatusCallback != null) {
164            params.put("voiceStatusCallback", voiceStatusCallback.toString());
165        }
166        if (messagesCallbackValue != null) {
167            params.put("messagesCallbackValue", messagesCallbackValue);
168            params.put("messagesCallbackType", CallbackType.APP.paramValue());
169        }
170        return params;
171    }
172
173    /**
174     * Represents the callback type for voice.
175     */
176    public enum CallbackType {
177        SIP,
178
179        TEL,
180
181        @Deprecated
182        VXML,
183
184        APP;
185
186
187        /**
188         * Serialized enum.
189         *
190         * @return The string value.
191         * @deprecated Use {@link #toString()}.
192         */
193        @Deprecated
194        public String paramValue() {
195            return toString();
196        }
197
198        @Override
199        public String toString() {
200            return name().toLowerCase();
201        }
202
203        /**
204         * Creates the enum from its string representation.
205         *
206         * @param type The serialized callback type as a string.
207         *
208         * @return Enum representation of the callback type, or {@code null} if {@code type} is null.
209         * @since 8.10.0
210         */
211        @JsonCreator
212        public static CallbackType fromString(String type) {
213            if (type == null) return null;
214            return valueOf(type.toUpperCase());
215        }
216    }
217
218    /**
219     * Entrypoint for constructing an instance of this class.
220     *
221     * @param country The two character country code in ISO 3166-1 alpha-2 format.
222     * @param msisdn The inbound virtual number to update.
223     *
224     * @return A new Builder.
225     *
226     * @since 8.10.0
227     */
228    public static Builder builder(String msisdn, String country) {
229        return new Builder(msisdn, country);
230    }
231
232    /**
233     * Builder for specifying the properties of the number to update.
234     *
235     * @since 8.10.0
236     */
237    public static class Builder {
238        private final String country, msisdn;
239        private String moSmppSysType, voiceCallbackValue;
240        private CallbackType voiceCallbackType;
241        private UUID appId;
242        private URI moHttpUrl, voiceStatusCallback;
243
244        private Builder(String msisdn, String country) {
245            this.country = country;
246            this.msisdn = msisdn;
247        }
248
249        /**
250         * Sets the associated system type for your SMPP client.
251         *
252         * @param moSmppSysType The system type as a string.
253         * @return This builder.
254         */
255        public Builder moSmppSysType(String moSmppSysType) {
256            this.moSmppSysType = moSmppSysType;
257            return this;
258        }
259
260        /**
261         * Sets the voice callback type and value (either SIP or PSTN).
262         *
263         * @param type Specify whether inbound voice calls on your number are forwarded to a SIP or
264         *             a telephone number. If set, {@code sip} or {@code tel} are prioritized over
265         *             the Voice capability in your Application.
266         *
267         * @param voiceCallbackValue The SIP URI or telephone number.
268         *
269         * @return This builder.
270         */
271        public Builder voiceCallback(CallbackType type, String voiceCallbackValue) {
272            this.voiceCallbackType = Objects.requireNonNull(type, "Voice callback type is required.)");
273            this.voiceCallbackValue = Objects.requireNonNull(voiceCallbackValue, "Voice callback value is required.");
274            return this;
275        }
276
277        /**
278         * Sets the application that will handle inbound traffic to this number.
279         *
280         * @param appId The application ID as a string.
281         * @return This builder.
282         */
283        public Builder applicationId(String appId) {
284            return applicationId(UUID.fromString(appId));
285        }
286
287        /**
288         * Sets the application that will handle inbound traffic to this number.
289         *
290         * @param appId The application ID.
291         * @return This builder.
292         */
293        public Builder applicationId(UUID appId) {
294            this.appId = appId;
295            return this;
296        }
297
298        /**
299         * Sets the URL-encoded URI to the webhook endpoint that handles inbound messages. Your webhook endpoint must
300         * be active before you make this request. Vonage makes a GET request to the endpoint and checks that it
301         * returns a 200 OK response. Set this parameter's value to an empty string to remove the webhook.
302         *
303         * @param moHttpUrl The inbound message webhook URL as a string.
304         * @return This builder.
305         */
306        public Builder moHttpUrl(String moHttpUrl) {
307            return moHttpUrl(URI.create(moHttpUrl));
308        }
309
310        /**
311         * Sets the URL-encoded URI to the webhook endpoint that handles inbound messages. Your webhook endpoint must
312         * be active before you make this request. Vonage makes a GET request to the endpoint and checks that it
313         * returns a 200 OK response. Set this parameter's value to an empty string to remove the webhook.
314         *
315         * @param moHttpUrl The inbound message webhook URL.
316         * @return This builder.
317         */
318        public Builder moHttpUrl(URI moHttpUrl) {
319            this.moHttpUrl = moHttpUrl;
320            return this;
321        }
322
323        /**
324         * Sets the webhook URI for Vonage to send a request to when a call ends.
325         *
326         * @param voiceStatusCallback The voice status webhook URL as a string.
327         * @return This builder.
328         */
329        public Builder voiceStatusCallback(String voiceStatusCallback) {
330            return voiceStatusCallback(URI.create(voiceStatusCallback));
331        }
332
333        /**
334         * Sets the webhook URI for Vonage to send a request to when a call ends.
335         *
336         * @param voiceStatusCallback The voice status webhook URL.
337         * @return This builder.
338         */
339        public Builder voiceStatusCallback(URI voiceStatusCallback) {
340            this.voiceStatusCallback = voiceStatusCallback;
341            return this;
342        }
343
344        /**
345         * Builds the UpdateNumberRequest.
346         *
347         * @return A new UpdateNumberRequest with this builder's properties.
348         */
349        public UpdateNumberRequest build() {
350            return new UpdateNumberRequest(this);
351        }
352    }
353}