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.conversations;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import com.vonage.client.common.HttpMethod;
021import java.net.URI;
022import java.util.UUID;
023
024/**
025 * Callback properties for a {@link Conversation}.
026 */
027public final class Callback extends JsonableBaseObject {
028        private URI url;
029        private String eventMask;
030        private Params params;
031        private HttpMethod method;
032
033        Callback() {}
034
035        Callback(Builder builder) {
036                url = builder.url;
037                if ((eventMask = builder.eventMask) != null && (eventMask.length() > 200 || eventMask.trim().isEmpty())) {
038                        throw new IllegalArgumentException("Event mask must be between 1 and 200 characters");
039                }
040                if ((method = builder.method) != null && !(method == HttpMethod.POST || method == HttpMethod.GET)) {
041                        throw new IllegalArgumentException("Callback HTTP method must be either POST or GET, not "+method);
042                }
043                params = builder.params;
044        }
045
046        /**
047         * Event URL for the callback.
048         * 
049         * @return The callback URL, or {@code null} if unspecified.
050         */
051        @JsonProperty("url")
052        public URI getUrl() {
053                return url;
054        }
055
056        @JsonProperty("event_mask")
057        public String getEventMask() {
058                return eventMask;
059        }
060
061        /**
062         * Additional parameters.
063         * 
064         * @return The callback parameters, or {@code null} if unspecified.
065         */
066        @JsonProperty("params")
067        public Params getParams() {
068                return params;
069        }
070
071        /**
072         * Method to use for the callback, either {@linkplain HttpMethod#GET} or {@linkplain HttpMethod#POST}.
073         * 
074         * @return The HTTP method as an enum, or {@code null} if unspecified.
075         */
076        @JsonProperty("method")
077        public HttpMethod getMethod() {
078                return method;
079        }
080
081        public static class Params extends JsonableBaseObject {
082                private UUID applicationId;
083                private URI nccoUrl;
084
085                protected Params() {}
086
087                /**
088                 * Vonage Application ID.
089                 *
090                 * @return The application ID, or {@code null} if unspecified.
091                 */
092                @JsonProperty("applicationId")
093                public UUID getApplicationId() {
094                        return applicationId;
095                }
096
097                /**
098                 * Call Control Object URL to use for the callback.
099                 *
100                 * @return The NCCO URL, or {@code null} if unspecified.
101                 */
102                @JsonProperty("ncco_url")
103                public URI getNccoUrl() {
104                        return nccoUrl;
105                }
106        }
107
108        /**
109         * Entry point for constructing an instance of this class.
110         *
111         * @return A new Builder.
112         */
113        public static Builder builder() {
114                return new Builder();
115        }
116
117        /**
118         * Builder for creating Callback settings. All parameters are optional.
119         */
120        public static final class Builder {
121                private URI url;
122                private String eventMask;
123                private HttpMethod method;
124                private Params params;
125
126                private Builder() {}
127
128                private Params initParams() {
129                        if (params == null) {
130                                params = new Params();
131                        }
132                        return params;
133                }
134
135                /**
136                 * Event URL for the callback.
137                 *
138                 * @param url The callback URL as a string.
139                 *
140                 * @return This builder.
141                 */
142                public Builder url(String url) {
143                        this.url = URI.create(url);
144                        return this;
145                }
146
147                /**
148                 * Callback event mask.
149                 *
150                 * @param eventMask The event mask as a string.
151                 *
152                 * @return This builder.
153                 */
154                public Builder eventMask(String eventMask) {
155                        this.eventMask = eventMask;
156                        return this;
157                }
158
159                /**
160                 * HTTP method to use for the callback.
161                 * Must be either {@linkplain HttpMethod#GET} or {@linkplain HttpMethod#POST}.
162                 *
163                 * @param method The HTTP method as an enum, or {@code null} if unspecified.
164                 *
165                 * @return This builder.
166                 */
167                public Builder method(HttpMethod method) {
168                        this.method = method;
169                        return this;
170                }
171
172                /**
173                 * Vonage Application ID.
174                 *
175                 * @param applicationId The application ID as a string.
176                 *
177                 * @return This builder.
178                 */
179                public Builder applicationId(String applicationId) {
180                        initParams().applicationId = UUID.fromString(applicationId);
181                        return this;
182                }
183
184                /**
185                 * Call Control Object URL to use for the callback.
186                 *
187                 * @param nccoUrl The NCCO URL as a string.
188                 *
189                 * @return This builder.
190                 */
191                public Builder nccoUrl(String nccoUrl) {
192                        initParams().nccoUrl = URI.create(nccoUrl);
193                        return this;
194                }
195
196                /**
197                 * Builds the {@linkplain Callback}.
198                 *
199                 * @return An instance of Callback, populated with all fields from this builder.
200                 */
201                public Callback build() {
202                        return new Callback(this);
203                }
204        }
205}