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.endpoints; 023 024 025import com.nexmo.client.HttpWrapper; 026import com.nexmo.client.NexmoClientException; 027import com.nexmo.client.NexmoUnexpectedException; 028import com.nexmo.client.auth.JWTAuthMethod; 029import com.nexmo.client.voice.CallInfoPage; 030import com.nexmo.client.voice.CallsFilter; 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.apache.http.HttpResponse; 034import org.apache.http.NameValuePair; 035import org.apache.http.client.methods.RequestBuilder; 036import org.apache.http.client.utils.URIBuilder; 037import org.apache.http.impl.client.BasicResponseHandler; 038import org.apache.http.util.EntityUtils; 039 040import java.io.IOException; 041import java.io.UnsupportedEncodingException; 042import java.net.URISyntaxException; 043import java.util.List; 044 045public class ListCallsMethod extends AbstractMethod<CallsFilter, CallInfoPage> { 046 private static final Log LOG = LogFactory.getLog(CreateCallMethod.class); 047 048 private static final String DEFAULT_URI = "https://api.nexmo.com/v1/calls"; 049 private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{JWTAuthMethod.class}; 050 private String uri = DEFAULT_URI; 051 052 public ListCallsMethod(HttpWrapper httpWrapper) { 053 super(httpWrapper); 054 } 055 056 @Override 057 protected Class[] getAcceptableAuthMethods() { 058 return ALLOWED_AUTH_METHODS; 059 } 060 061 @Override 062 public RequestBuilder makeRequest(CallsFilter filter) throws NexmoClientException, UnsupportedEncodingException { 063 URIBuilder uriBuilder; 064 try { 065 uriBuilder = new URIBuilder(this.uri); 066 } catch (URISyntaxException e) { 067 throw new NexmoUnexpectedException("Could not parse URI: " + this.uri); 068 } 069 if (filter != null) { 070 List<NameValuePair> params = filter.toUrlParams(); 071 for (NameValuePair param : params) { 072 uriBuilder.setParameter(param.getName(), param.getValue()); 073 } 074 } 075 return RequestBuilder.get().setUri(uriBuilder.toString()); 076 } 077 078 @Override 079 public CallInfoPage parseResponse(HttpResponse response) throws IOException { 080 String json = new BasicResponseHandler().handleResponse(response); 081 return CallInfoPage.fromJson(json); 082 } 083 084 public void setUri(String uri) { 085 this.uri = uri; 086 } 087 088 public String getUri() { 089 return this.uri; 090 } 091}