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.common;
017
018import com.fasterxml.jackson.annotation.*;
019import com.vonage.client.JsonableBaseObject;
020import java.util.Arrays;
021import java.util.Map;
022import java.util.function.Function;
023import java.util.stream.Collectors;
024
025/**
026 * Represents the "webhooks" field used in Application capabilities.
027 *
028 * @deprecated Will be moved to the {@code com.vonage.client.application} package.
029 */
030@Deprecated
031public class Webhook extends JsonableBaseObject {
032    private String address;
033    private HttpMethod method;
034    private Integer connectionTimeout, socketTimeout;
035
036    private Webhook(Builder builder) {
037        if ((address = builder.address) == null || address.trim().isEmpty()) {
038            throw new IllegalStateException("Address is required.");
039        }
040        if ((method = builder.method) == null) {
041            throw new IllegalStateException("HTTP method is required.");
042        }
043        if ((connectionTimeout = builder.connectionTimeout) != null) {
044            int min = 300, max = 1000;
045            if (connectionTimeout < min || connectionTimeout > max) {
046                throw new IllegalArgumentException(
047                        "Connection timeout must be between "+min+" and "+max+" milliseconds."
048                );
049            }
050        }
051        if ((socketTimeout = builder.socketTimeout) != null) {
052            int min = 1000, max = 10000;
053            if (socketTimeout < min || socketTimeout > max) {
054                throw new IllegalArgumentException(
055                        "Socket timeout must be between "+min+" and "+max+" milliseconds."
056                );
057            }
058        }
059    }
060
061    protected Webhook() {
062    }
063
064    public Webhook(String address, HttpMethod method) {
065        this.address = address;
066        this.method = method;
067    }
068
069    /**
070     * The webhook's URL.
071     *
072     * @return The URL as a string.
073     */
074    @JsonProperty("address")
075    public String getAddress() {
076        return address;
077    }
078
079    /**
080     * The HTTP request method for this webhook.
081     *
082     * @return The HTTP method as an enum.
083     */
084    @JsonProperty("http_method")
085    public HttpMethod getMethod() {
086        return method;
087    }
088
089    /**
090     * If Vonage can't connect to the webhook URL for this specified amount of time, then Vonage makes one
091     * additional attempt to connect to the webhook endpoint. This is an integer value specified in milliseconds.
092     * The minimum is 300, maximum 1000 and default is 1000.
093     *
094     * @return The connection timeout in milliseconds as an integer, or {@code null}
095     * if unspecified (the default) / not applicable.
096     */
097    @JsonProperty("connection_timeout")
098    public Integer getConnectionTimeout() {
099        return connectionTimeout;
100    }
101
102    /**
103     * If a response from the webhook URL can't be read for this specified amount of time, then Vonage makes one
104     * additional attempt to read the webhook endpoint. This is an integer value specified in milliseconds.
105     * The minimum is 1000, maximum 5000 and default is 5000.
106     *
107     * @return The socket timeout in milliseconds as an integer, or {@code null}
108     * if unspecified (the default) / not applicable.
109     */
110    @JsonProperty("socket_timeout")
111    public Integer getSocketTimeout() {
112        return socketTimeout;
113    }
114
115    /**
116     * Entrypoint for constructing an instance of this class.
117     *
118     * @return A new Builder.
119     * @since 7.7.0
120     */
121    public static Builder builder() {
122        return new Builder();
123    }
124
125    /**
126     * Builder for configuring Webhook object.
127     *
128     * @since 7.7.0
129     */
130    public static class Builder {
131        private String address;
132        private HttpMethod method;
133        private Integer connectionTimeout, socketTimeout;
134
135        Builder() {}
136
137        /**
138         * (REQUIRED) The webhook's URL.
139         *
140         * @param address The address as a string.
141         *
142         * @return This builder.
143         */
144        public Builder address(String address) {
145            this.address = address;
146            return this;
147        }
148
149        /**
150         * (REQUIRED) The HTTP request method for this webhook.
151         *
152         * @param method The HTTP method as an enum.
153         *
154         * @return This builder.
155         */
156        public Builder method(HttpMethod method) {
157            this.method = method;
158            return this;
159        }
160
161        /**
162         * (OPTIONAL) If Vonage can't connect to the webhook URL for this specified amount of time, then Vonage
163         * makes one additional attempt to connect to the webhook endpoint. This is an integer value specified
164         * in milliseconds. The minimum is 300, maximum 1000 and default is 1000.
165         *
166         * @param connectionTimeout The connection timeout in milliseconds.
167         *
168         * @return This builder.
169         */
170        public Builder connectionTimeout(int connectionTimeout) {
171            this.connectionTimeout = connectionTimeout;
172            return this;
173        }
174
175        /**
176         * (OPTIONAL)  If a response from the webhook URL can't be read for this specified amount of time, then
177         * Vonage makes one additional attempt to read the webhook endpoint. This is an integer value specified
178         * in milliseconds. The minimum is 1000, maximum 5000 and default is 5000.
179         *
180         * @param socketTimeout The socket timeout in milliseconds.
181         *
182         * @return This builder.
183         */
184        public Builder socketTimeout(int socketTimeout) {
185            this.socketTimeout = socketTimeout;
186            return this;
187        }
188
189        /**
190         * Builds the Webhook object.
191         *
192         * @return A new Webhook instance with this builder's properties.
193         */
194        public Webhook build() {
195            return new Webhook(this);
196        }
197    }
198
199    /**
200     * Represents the webhook URL type.
201     */
202    public enum Type {
203        ANSWER("answer_url"),
204        FALLBACK_ANSWER("fallback_answer_url"),
205        EVENT("event_url"),
206        INBOUND("inbound_url"),
207        STATUS("status_url"),
208        UNKNOWN("unknown");
209
210        private final String name;
211
212        private static final Map<String, Type> TYPE_INDEX =
213            Arrays.stream(Type.values()).collect(Collectors.toMap(
214                    Type::getName, Function.identity()
215            ));
216
217        Type(String name) {
218            this.name = name;
219        }
220
221        @JsonValue
222        public String getName() {
223            return name;
224        }
225
226        @JsonCreator
227        public static Type fromName(String name) {
228            return TYPE_INDEX.getOrDefault(name.toLowerCase(), UNKNOWN);
229        }
230    }
231}