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.application;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.Jsonable;
020import com.vonage.client.JsonableBaseObject;
021import com.vonage.client.application.capabilities.*;
022
023/**
024 * Represents a Vonage Application (both request and response).
025 */
026public class Application extends JsonableBaseObject {
027    private String id, name;
028    private Keys keys;
029    private Capabilities capabilities;
030    private Privacy privacy;
031
032    private Application() {
033    }
034
035    private Application(Builder builder) {
036        id = builder.id;
037        name = builder.name;
038        keys = builder.keys;
039        capabilities = builder.capabilities;
040        privacy = builder.privacy;
041    }
042
043    /**
044     * Unique application ID.
045     *
046     * @return The application ID as a string, or {@code null} if unknown.
047     */
048    @JsonProperty("id")
049    public String getId() {
050        return id;
051    }
052
053    /**
054     * Friendly identifier for your application. This is not unique.
055     *
056     * @return The application name.
057     */
058    @JsonProperty("name")
059    public String getName() {
060        return name;
061    }
062
063    /**
064     * Cryptographic keys associated with this application.
065     *
066     * @return The keys for this application, or {@code null} if unknown / not applicable for this object.
067     */
068    @JsonProperty("keys")
069    public Keys getKeys() {
070        return keys;
071    }
072
073    /**
074     * Your application can use multiple products. This contains the configuration for each product.
075     *
076     * @return The capabilities of this application, or {@code null} if unknown / not applicable for this object.
077     */
078    @JsonProperty("capabilities")
079    public Capabilities getCapabilities() {
080        return capabilities;
081    }
082
083    /**
084     * Application privacy configuration.
085     *
086     * @return The privacy preferences, or {@code null} if unknown / not applicable for this object.
087     *
088     * @since 7.7.0
089     */
090    @JsonProperty("privacy")
091    public Privacy getPrivacy() {
092        return privacy;
093    }
094
095    public static Application fromJson(String json) {
096        return Jsonable.fromJson(json);
097    }
098
099    /**
100     * Entry point for creating an instance of this class.
101     * 
102     * @return A new Builder.
103     */
104    public static Builder builder() {
105        return new Builder();
106    }
107
108    /**
109     * Copy an application to a new builder to modify into a new application object.
110     *
111     * @param application An existing application to modify.
112     *
113     * @return A new Builder to start building.
114     */
115    public static Builder builder(Application application) {
116        return new Builder(application);
117    }
118
119    public static class Builder {
120        private Privacy privacy;
121        private String id, name;
122        private Keys keys;
123        private Capabilities capabilities;
124
125        public Builder() {}
126
127        public Builder(Application application) {
128            id = application.id;
129            name = application.name;
130            keys = application.keys;
131            capabilities = application.capabilities;
132        }
133
134        /**
135         * Whether Vonage may store and use your content and data for the improvement of
136         * Vonage's AI based services and technologies. Default is {@code true}.
137         *
138         * @param improveAi {@code true} if you consent to data being used for AI improvement,
139         * or {@code false} to opt out.
140         *
141         * @return This builder.
142         *
143         * @since 7.7.0
144         */
145        public Builder improveAi(boolean improveAi) {
146            if (privacy == null) {
147                privacy = new Privacy();
148            }
149            privacy.improveAi = improveAi;
150            return this;
151        }
152
153        /**
154         * Set the friendly identifier for your application. This is not unique.
155         *
156         * @param name The name of the application.
157         *
158         * @return This builder.
159         */
160        public Builder name(String name) {
161            this.name = name;
162            return this;
163        }
164
165        /**
166         * Sets the application's public key.
167         *
168         * @param publicKey The public key for use with the application as a string.
169         *
170         * @return This builder.
171         */
172        public Builder publicKey(String publicKey) {
173            keys = new Keys();
174            keys.publicKey = publicKey;
175            return this;
176        }
177
178        /**
179         * Add a capability for the application. Each capability can only be used one time. Adding a capability of a
180         * duplicate type will overwrite the previous capability of that type.
181         *
182         * @param capability The capability to add to it.
183         *
184         * @return This builder.
185         */
186        public Builder addCapability(Capability capability) {
187            if (capabilities == null) {
188                capabilities = new Capabilities();
189            }
190            capabilities.setCapability(capability);
191            return this;
192        }
193
194        /**
195         * Removes the specified capabilities from the application.
196         *
197         * @param types The type of capabilities to remove as an array / varargs.
198         *
199         * @return This builder.
200         * @since 8.14.0
201         */
202        public Builder removeCapabilities(Capability.Type... types) {
203            for (Capability.Type type : types) {
204                removeCapability(type);
205            }
206            return this;
207        }
208
209        /**
210         * Remove a capability from the application.
211         *
212         * @param type The type of capability to remove.
213         *
214         * @return This builder.
215         */
216        public Builder removeCapability(Capability.Type type) {
217            if (capabilities != null) {
218                capabilities.setCapability(type, null);
219                if (
220                    capabilities.voice == null &&
221                    capabilities.rtc == null &&
222                    capabilities.messages == null &&
223                    capabilities.vbc == null &&
224                    capabilities.verify == null &&
225                    capabilities.networkApis == null
226                ) {
227                    capabilities = null;
228                }
229            }
230            return this;
231        }
232
233        /**
234         * Builds the Application object.
235         *
236         * @return A new Application containing the configured properties.
237         */
238        public Application build() {
239            return new Application(this);
240        }
241    }
242
243    /**
244     * Application privacy configuration settings.
245     *
246     * @since 7.7.0
247     */
248    public static class Privacy extends JsonableBaseObject {
249        private Boolean improveAi;
250
251        /**
252         * Whether Vonage may store and use your content and data for the improvement of
253         * Vonage's AI based services and technologies.
254         *
255         * @return {@code true} if Vonage may use the data for improving its AI services,
256         * or {@code null} if unspecified.
257         */
258        @JsonProperty("improve_ai")
259        public Boolean getImproveAi() {
260            return improveAi;
261        }
262    }
263
264    /**
265     * Represents the cryptographic keys of an Application.
266     */
267    public static class Keys extends JsonableBaseObject {
268        private String publicKey, privateKey;
269
270        /**
271         * The application's public key.
272         *
273         * @return The public key as a string, or {@code null} if absent.
274         */
275        @JsonProperty("public_key")
276        public String getPublicKey() {
277            return publicKey;
278        }
279
280        /**
281         * The application's private key.
282         *
283         * @return The private key as a string, or {@code null} if absent (the default).
284         */
285        @JsonProperty("private_key")
286        public String getPrivateKey() {
287            return privateKey;
288        }
289    }
290
291    public static class Capabilities extends JsonableBaseObject {
292        private Voice voice;
293        private Messages messages;
294        private Rtc rtc;
295        private Vbc vbc;
296        private Verify verify;
297        private NetworkApis networkApis;
298
299        /**
300         * Voice capability.
301         *
302         * @return The Voice capability, or {@code null} if absent.
303         */
304        @JsonProperty("voice")
305        public Voice getVoice() {
306            return voice;
307        }
308
309        /**
310         * Messages capability.
311         *
312         * @return The Messages capability, or {@code null} if absent.
313         */
314        @JsonProperty("messages")
315        public Messages getMessages() {
316            return messages;
317        }
318
319        /**
320         * RTC capability.
321         *
322         * @return The RTC capability, or {@code null} if absent.
323         */
324        @JsonProperty("rtc")
325        public Rtc getRtc() {
326            return rtc;
327        }
328
329        /**
330         * VBC capability.
331         *
332         * @return The VBC capability, or {@code null} if absent.
333         */
334        @JsonProperty("vbc")
335        public Vbc getVbc() {
336            return vbc;
337        }
338
339        /**
340         * Verify capability.
341         *
342         * @return The Verify capability, or {@code null} if absent.
343         * @since 8.6.0
344         */
345        @JsonProperty("verify")
346        public Verify getVerify() {
347            return verify;
348        }
349
350        /**
351         * Network APIs capability.
352         *
353         * @return The Network APIs capability, or {@code null} if absent.
354         * @since 8.12.0
355         */
356        @JsonProperty("network_apis")
357        public NetworkApis getNetworkApis() {
358            return networkApis;
359        }
360
361        private void setCapability(Capability.Type type, Capability capability) {
362            switch (type) {
363                case VOICE:
364                    voice = (Voice) capability;
365                    break;
366                case MESSAGES:
367                    messages = (Messages) capability;
368                    break;
369                case RTC:
370                    rtc = (Rtc) capability;
371                    break;
372                case VBC:
373                    vbc = (Vbc) capability;
374                    break;
375                case VERIFY:
376                    verify = (Verify) capability;
377                    break;
378                case NETWORK:
379                    networkApis = (NetworkApis) capability;
380                    break;
381            }
382        }
383
384        private void setCapability(Capability capability) {
385            setCapability(capability.getType(), capability);
386        }
387    }
388}