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.servlet; 023 024import com.fasterxml.jackson.core.JsonProcessingException; 025import com.fasterxml.jackson.databind.ObjectMapper; 026import com.nexmo.client.NexmoUnexpectedException; 027import com.nexmo.client.voice.ncco.Ncco; 028 029import javax.servlet.http.HttpServletRequest; 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * Encapsulates a set of NCCO objects for driving the Nexmo Voice API. 035 * <p> 036 * This would usually be returned by {@link AbstractAnswerServlet#handleRequest(HttpServletRequest)}, which serializes 037 * it correctly for the Voice API. {@link NccoResponseBuilder} provides a fluent interface for constructing instances 038 * of this class. 039 */ 040public class NccoResponse { 041 // This object has been purposefully designed to be relatively opaque, as 042 // the internal structure of this response may become more complex, and so 043 // we don't want users to become dependent on it being a simple list of 044 // Ncco objects. 045 046 private List<Ncco> nccoList; 047 048 public NccoResponse() { 049 nccoList = new ArrayList<Ncco>(); 050 } 051 052 public void appendNcco(Ncco ncco) { 053 this.nccoList.add(ncco); 054 } 055 056 public String toJson() { 057 try { 058 return new ObjectMapper().writeValueAsString(nccoList); 059 } catch (JsonProcessingException e) { 060 throw new NexmoUnexpectedException("Failed to serialize NccoResponse object.", e); 061 } 062 } 063}