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.application.capabilities; 017 018import com.fasterxml.jackson.annotation.JsonIgnore; 019import com.fasterxml.jackson.annotation.JsonInclude; 020import com.fasterxml.jackson.annotation.JsonProperty; 021import com.vonage.client.JsonableBaseObject; 022import com.vonage.client.common.HttpMethod; 023import com.vonage.client.common.Webhook; 024import java.util.LinkedHashMap; 025import java.util.Map; 026 027/** 028 * Represents a capability of a Vonage Application 029 */ 030@JsonInclude(JsonInclude.Include.NON_ABSENT) 031public abstract class Capability extends JsonableBaseObject { 032 protected Map<Webhook.Type, Webhook> webhooks; 033 034 protected Capability() { 035 } 036 037 protected Capability(Builder<?, ?> builder) { 038 webhooks = builder.webhooks; 039 } 040 041 /** 042 * The capability's type. 043 * 044 * @return This capability's type as an enum. 045 */ 046 @JsonIgnore 047 abstract public Type getType(); 048 049 /** 050 * Webhooks grouped by type. 051 * 052 * @return The webhooks as a Map, or {@code null} if there are none. 053 */ 054 @JsonProperty("webhooks") 055 public Map<Webhook.Type, Webhook> getWebhooks() { 056 return webhooks; 057 } 058 059 /** 060 * Represents the API this capability relates to. 061 */ 062 public enum Type { 063 064 /** 065 * Voice API 066 */ 067 VOICE, 068 069 /** 070 * RTC 071 */ 072 RTC, 073 074 /** 075 * Messages API 076 */ 077 MESSAGES, 078 079 /** 080 * VBC 081 */ 082 VBC, 083 084 /** 085 * Verify API 086 * 087 * @since 8.6.0 088 */ 089 VERIFY, 090 091 /** 092 * Network APIs 093 * 094 * @since 8.12.0 095 */ 096 NETWORK 097 } 098 099 @SuppressWarnings("unchecked") 100 protected static abstract class Builder<C extends Capability, B extends Builder<C, B>> { 101 Map<Webhook.Type, Webhook> webhooks; 102 103 /** 104 * Add a webhook for the Vonage API to use. Each Capability can only have a single webhook of each type. 105 * See <a href="https://developer.vonage.com/concepts/guides/webhooks"> the webhooks guide</a> for details. 106 * 107 * @param type The {@link Webhook.Type} of webhook to add. 108 * @param webhook The webhook containing the URL and {@link HttpMethod}. 109 * 110 * @return This builder. 111 */ 112 B addWebhook(Webhook.Type type, Webhook webhook) { 113 if (webhooks == null) { 114 webhooks = new LinkedHashMap<>(); 115 } 116 webhooks.put(type, webhook); 117 return (B) this; 118 } 119 120 /** 121 * Remove a webhook. 122 * 123 * @param type The {@link Webhook.Type} to remove. 124 * 125 * @return This builder. 126 */ 127 B removeWebhook(Webhook.Type type) { 128 if (webhooks != null) { 129 webhooks.remove(type); 130 if (webhooks.isEmpty()) { 131 webhooks = null; 132 } 133 } 134 return (B) this; 135 } 136 137 /** 138 * Constructs the capability with this builder's properties. 139 * 140 * @return A new instance of the capability. 141 */ 142 public abstract C build(); 143 } 144}