001/*
002 *   Copyright 2020 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.capabilities;
017
018import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
019import com.fasterxml.jackson.annotation.JsonInclude;
020import com.vonage.client.common.HttpMethod;
021import com.vonage.client.common.Webhook;
022
023import java.util.LinkedHashMap;
024import java.util.Map;
025
026@JsonIgnoreProperties(ignoreUnknown = true)
027@JsonInclude(JsonInclude.Include.NON_ABSENT)
028public class Rtc extends Capability {
029    private Rtc() {
030
031    }
032
033    private Rtc(Builder builder) {
034        this.webhooks = builder.webhooks;
035    }
036
037    @Override
038    public Type getType() {
039        return Type.RTC;
040    }
041
042    /**
043     * @return A new Builder to start building.
044     */
045    public static Builder builder() {
046        return new Builder();
047    }
048
049    public static class Builder {
050        private Map<Webhook.Type, Webhook> webhooks;
051
052        /**
053         * Add a webhook for the Vonage API to use. See https://developer.nexmo.com/concepts/guides/webhooks. Each
054         * Capability can only have a single webhook of each type. Any futher adding of webhooks will override an
055         * already existing one of that type.
056         *
057         * @param type    The {@link Webhook.Type} of webhook to add.
058         * @param webhook The webhook containing the URL and {@link HttpMethod}.
059         *
060         * @return The {@link Builder} to keep building.
061         */
062        public Builder addWebhook(Webhook.Type type, Webhook webhook) {
063            if (this.webhooks == null) {
064                this.webhooks = new LinkedHashMap<>();
065            }
066
067            this.webhooks.put(type, webhook);
068            return this;
069        }
070
071        /**
072         * Remove a webhook.
073         *
074         * @param type The {@link Webhook.Type} to remove.
075         *
076         * @return The {@link Builder} to keep building.
077         */
078        public Builder removeWebhook(Webhook.Type type) {
079            if (this.webhooks == null) {
080                this.webhooks = new LinkedHashMap<>();
081            }
082
083            this.webhooks.remove(type);
084
085            if (this.webhooks.isEmpty()) {
086                this.webhooks = null;
087            }
088
089            return this;
090        }
091
092        /**
093         * @return A new Rtc capability containing the configured properties.
094         */
095        public Rtc build() {
096            return new Rtc(this);
097        }
098    }
099}