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