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.ncco; 017 018import com.fasterxml.jackson.annotation.JsonValue; 019import com.fasterxml.jackson.core.JsonProcessingException; 020import com.fasterxml.jackson.databind.ObjectMapper; 021import com.fasterxml.jackson.databind.ObjectWriter; 022import com.vonage.client.VonageUnexpectedException; 023 024import java.util.Arrays; 025import java.util.Collection; 026import java.util.Collections; 027 028/** 029 * Vonage Call Control Object for controlling the flow of a Voice API call. 030 */ 031public class Ncco { 032 @JsonValue 033 private Collection<Action> actions; 034 private ObjectWriter writer; 035 036 public Ncco() { 037 this(new ObjectMapper().writer(), Collections.emptyList()); 038 } 039 040 public Ncco(Collection<Action> actions) { 041 this(new ObjectMapper().writer(), actions); 042 } 043 044 public Ncco(ObjectWriter writer) { 045 this(writer, Collections.emptyList()); 046 } 047 048 public Ncco(ObjectWriter writer, Collection<Action> actions) { 049 this.writer = writer; 050 this.actions = actions; 051 } 052 053 public Ncco(ObjectWriter writer, Action... action) { 054 this(writer, Arrays.asList(action)); 055 } 056 057 public Ncco(Action... action) { 058 this(Arrays.asList(action)); 059 } 060 061 public Collection<Action> getActions() { 062 return this.actions; 063 } 064 065 public String toJson() { 066 try { 067 return this.writer.writeValueAsString(this.actions); 068 } catch (JsonProcessingException e) { 069 throw new VonageUnexpectedException("Unable to convert NCCO Object to JSON."); 070 } 071 } 072}