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