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;
017
018
019import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
020import com.fasterxml.jackson.annotation.JsonInclude;
021import com.fasterxml.jackson.annotation.JsonProperty;
022import com.fasterxml.jackson.core.JsonProcessingException;
023import com.fasterxml.jackson.databind.ObjectMapper;
024import com.vonage.client.VonageUnexpectedException;
025import com.vonage.client.voice.ncco.Ncco;
026
027import java.io.IOException;
028
029/**
030 * Call encapsulates the information required to create a call using {@link VoiceClient#createCall(Call)}
031 */
032@JsonInclude(value = JsonInclude.Include.NON_NULL)
033@JsonIgnoreProperties({"_links"})
034public class Call {
035    private Endpoint[] to;
036    private Endpoint from;
037    private String answerUrl;
038
039    private String answerMethod = "GET";
040    private String eventUrl = null;
041    private String eventMethod = null;
042    private MachineDetection machineDetection = null;
043    private Integer lengthTimer = null;
044    private Integer ringingTimer = null;
045    private Ncco ncco;
046
047    public Call() {
048    }
049
050    public Call(String to, String from, String answerUrl) {
051        this(new PhoneEndpoint(to), new PhoneEndpoint(from), answerUrl);
052    }
053
054    public Call(Endpoint to, Endpoint from, String answerUrl) {
055        this(new Endpoint[]{to}, from, answerUrl);
056    }
057
058    public Call(Endpoint[] to, Endpoint from, String answerUrl) {
059        this.to = to;
060        this.from = from;
061        this.answerUrl = answerUrl;
062    }
063
064    public Call(String to, String from, Ncco ncco) {
065        this(new PhoneEndpoint(to), new PhoneEndpoint(from), ncco);
066    }
067
068    public Call(Endpoint to, Endpoint from, Ncco ncco) {
069        this(new Endpoint[]{to}, from, ncco);
070    }
071
072    public Call(Endpoint[] to, Endpoint from, Ncco ncco) {
073        this.to = to;
074        this.from = from;
075        this.ncco = ncco;
076    }
077
078    public Endpoint[] getTo() {
079        return to;
080    }
081
082    public void setTo(Endpoint[] to) {
083        this.to = to;
084    }
085
086    public Endpoint getFrom() {
087        return from;
088    }
089
090    public void setFrom(Endpoint from) {
091        this.from = from;
092    }
093
094    @JsonProperty("answer_url")
095    public String[] getAnswerUrl() {
096        return (answerUrl != null) ? new String[]{answerUrl} : null;
097    }
098
099    public void setAnswerUrl(String answerUrl) {
100        this.answerUrl = answerUrl;
101    }
102
103    @JsonProperty("answer_method")
104    public String getAnswerMethod() {
105        // Hide the answer method if the answer url isn't defined
106        return (answerUrl != null) ? answerMethod : null;
107    }
108
109    public void setAnswerMethod(String answerMethod) {
110        this.answerMethod = answerMethod;
111    }
112
113    @JsonProperty("event_url")
114    public String[] getEventUrl() {
115        if (eventUrl == null) {
116            return null;
117        }
118        return new String[]{eventUrl};
119    }
120
121    public void setEventUrl(String eventUrl) {
122        this.eventUrl = eventUrl;
123    }
124
125    @JsonProperty("event_method")
126    public String getEventMethod() {
127        return eventMethod;
128    }
129
130    public void setEventMethod(String eventMethod) {
131        this.eventMethod = eventMethod;
132    }
133
134    @JsonProperty("machine_detection")
135    public MachineDetection getMachineDetection() {
136        return machineDetection;
137    }
138
139    public void setMachineDetection(MachineDetection machineDetection) {
140        this.machineDetection = machineDetection;
141    }
142
143    @JsonProperty("length_timer")
144    public Integer getLengthTimer() {
145        return lengthTimer;
146    }
147
148    public void setLengthTimer(Integer lengthTimer) {
149        this.lengthTimer = lengthTimer;
150    }
151
152    @JsonProperty("ringing_timer")
153    public Integer getRingingTimer() {
154        return ringingTimer;
155    }
156
157    public void setRingingTimer(Integer ringingTimer) {
158        this.ringingTimer = ringingTimer;
159    }
160
161    @JsonProperty("ncco")
162    public Ncco getNcco() {
163        return ncco;
164    }
165
166    public String toJson() {
167        try {
168            ObjectMapper mapper = new ObjectMapper();
169            return mapper.writeValueAsString(this);
170        } catch (JsonProcessingException jpe) {
171            throw new VonageUnexpectedException("Failed to produce json from Call object.", jpe);
172        }
173    }
174
175    public static Call fromJson(String json) {
176        try {
177            ObjectMapper mapper = new ObjectMapper();
178            return mapper.readValue(json, Call.class);
179        } catch (IOException jpe) {
180            throw new VonageUnexpectedException("Failed to produce json from Call object.", jpe);
181        }
182    }
183}