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.voice.ncco;
017
018import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
019import com.fasterxml.jackson.annotation.JsonInclude;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Map;
024
025/**
026 * An NCCO notify action which allows for custom events to be sent to a configured webhook.
027 */
028@JsonInclude(value = JsonInclude.Include.NON_NULL)
029@JsonIgnoreProperties(ignoreUnknown = true)
030public class NotifyAction implements Action {
031    private static final String ACTION = "notify";
032
033    private Map<String, ?> payload;
034    private Collection<String> eventUrl;
035    private EventMethod eventMethod;
036
037    private NotifyAction(Builder builder) {
038        this.payload = builder.payload;
039        this.eventUrl = builder.eventUrl;
040        this.eventMethod = builder.eventMethod;
041    }
042
043    @Override
044    public String getAction() {
045        return ACTION;
046    }
047
048    public Map getPayload() {
049        return payload;
050    }
051
052    public Collection<String> getEventUrl() {
053        return eventUrl;
054    }
055
056    public EventMethod getEventMethod() {
057        return eventMethod;
058    }
059
060    public static Builder builder(Map<String, ?> payload, Collection<String> eventUrl) {
061        return new Builder(payload, eventUrl);
062    }
063
064    public static Builder builder(Map<String, ?> payload, String... eventUrl) {
065        return new Builder(payload, eventUrl);
066    }
067
068    public static class Builder {
069        private Map<String, ?> payload;
070        private Collection<String> eventUrl;
071        private EventMethod eventMethod = null;
072
073        /**
074         * @param payload  A Map of String keys and ? values that will be converted to JSON and sent to your event URL.
075         * @param eventUrl The URL to send events to.
076         */
077        public Builder(Map<String, ?> payload, Collection<String> eventUrl) {
078            this.payload = payload;
079            this.eventUrl = eventUrl;
080        }
081
082        /**
083         * @param payload  A Map of String keys and ? values that will be converted to JSON and sent to your event URL.
084         * @param eventUrl The URL to send events to.
085         */
086        public Builder(Map<String, ?> payload, String... eventUrl) {
087            this(payload, Arrays.asList(eventUrl));
088        }
089
090        /**
091         * @param payload A Map of String keys and ? values that will be converted to JSON and sent to your event URL.
092         *
093         * @return The {@link Builder} to keep building.
094         */
095        public Builder payload(Map<String, ?> payload) {
096            this.payload = payload;
097            return this;
098        }
099
100        /**
101         * @param eventUrl The URL to send events to.
102         *
103         * @return The {@link Builder} to keep building.
104         */
105        public Builder eventUrl(Collection<String> eventUrl) {
106            this.eventUrl = eventUrl;
107            return this;
108        }
109
110        /**
111         * @param eventUrl The URL to send events to.
112         *
113         * @return The {@link Builder} to keep building.
114         */
115        public Builder eventUrl(String... eventUrl) {
116            return eventUrl(Arrays.asList(eventUrl));
117        }
118
119        /**
120         * @param eventMethod The HTTP method to use when sending the payload to your event url.
121         *
122         * @return The {@link Builder} to keep building.
123         */
124        public Builder eventMethod(EventMethod eventMethod) {
125            this.eventMethod = eventMethod;
126            return this;
127        }
128
129        /**
130         * @return A new {@link NotifyAction} object from the stored builder options.
131         */
132        public NotifyAction build() {
133            return new NotifyAction(this);
134        }
135    }
136}