001/*
002 * Copyright (c) 2011-2019 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.application.capabilities;
023
024import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
025import com.fasterxml.jackson.annotation.JsonInclude;
026import com.nexmo.client.common.Webhook;
027
028import java.util.LinkedHashMap;
029import java.util.Map;
030
031@JsonIgnoreProperties(ignoreUnknown = true)
032@JsonInclude(JsonInclude.Include.NON_ABSENT)
033public class Messages extends Capability {
034    private Messages() {
035
036    }
037
038    private Messages(Builder builder) {
039        this.webhooks = builder.webhooks;
040    }
041
042    @Override
043    public Type getType() {
044        return Type.MESSAGES;
045    }
046
047    /**
048     * @return A new Builder to start building.
049     */
050    public static Builder builder() {
051        return new Builder();
052    }
053
054    public static class Builder {
055        private Map<Webhook.Type, Webhook> webhooks;
056
057        /**
058         * Add a webhook for the Nexmo API to use. See https://developer.nexmo.com/concepts/guides/webhooks. Each
059         * Capability can only have a single webhook of each type. Any futher adding of webhooks will override an
060         * already existing one of that type.
061         *
062         * @param type    The {@link Webhook.Type} of webhook to add.
063         * @param webhook The webhook containing the URL and {@link com.nexmo.client.common.HttpMethod}.
064         *
065         * @return The {@link Builder} to keep building.
066         */
067        public Builder addWebhook(Webhook.Type type, Webhook webhook) {
068            if (this.webhooks == null) {
069                this.webhooks = new LinkedHashMap<>();
070            }
071
072            this.webhooks.put(type, webhook);
073            return this;
074        }
075
076        /**
077         * Remove a webhook.
078         *
079         * @param type The {@link Webhook.Type} to remove.
080         *
081         * @return The {@link Builder} to keep building.
082         */
083        public Builder removeWebhook(Webhook.Type type) {
084            if (this.webhooks == null) {
085                this.webhooks = new LinkedHashMap<>();
086            }
087
088            this.webhooks.remove(type);
089
090            if (this.webhooks.isEmpty()) {
091                this.webhooks = null;
092            }
093
094            return this;
095        }
096
097        /**
098         * @return A new Messages capability containing the configured properties.
099         */
100        public Messages build() {
101            return new Messages(this);
102        }
103    }
104}