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
018import com.fasterxml.jackson.core.JsonProcessingException;
019import com.fasterxml.jackson.databind.ObjectMapper;
020import com.vonage.client.VonageUnexpectedException;
021import com.vonage.client.voice.ncco.Ncco;
022
023
024public class CallModifier {
025    private final String uuid;
026    private final ModifyCallPayload modifyCallPayload;
027
028    public CallModifier(String uuid, ModifyCallPayload modifyCallPayload) {
029        this.uuid = uuid;
030        this.modifyCallPayload = modifyCallPayload;
031    }
032
033    public CallModifier(String uuid, ModifyCallAction action) {
034        this.uuid = uuid;
035        this.modifyCallPayload = new ModifyCallPayload(action);
036    }
037
038    public static CallModifier transferCall(String uuid, String nccoUrl) {
039        return new CallModifier(uuid, new TransferCallPayload(nccoUrl));
040    }
041
042    public static CallModifier transferCall(String uuid, Ncco ncco) {
043        return new CallModifier(uuid, new TransferCallPayload(ncco));
044    }
045
046    public String getUuid() {
047        return uuid;
048    }
049
050    public ModifyCallAction getAction() {
051        return modifyCallPayload.getAction();
052    }
053
054    public String toJson() {
055        try {
056            ObjectMapper mapper = new ObjectMapper();
057            return mapper.writeValueAsString(this.modifyCallPayload);
058        } catch (JsonProcessingException jpe) {
059            throw new VonageUnexpectedException("Failed to produce json from CallModifier object.", jpe);
060        }
061    }
062}