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; 023 024import com.nexmo.client.auth.AuthMethod; 025import com.nexmo.client.logging.LoggingUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.apache.http.HttpEntity; 029import org.apache.http.HttpEntityEnclosingRequest; 030import org.apache.http.HttpResponse; 031import org.apache.http.client.HttpClient; 032import org.apache.http.client.entity.UrlEncodedFormEntity; 033import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 034import org.apache.http.client.methods.HttpUriRequest; 035import org.apache.http.client.methods.RequestBuilder; 036import org.apache.http.util.EntityUtils; 037 038import java.io.IOException; 039import java.io.UnsupportedEncodingException; 040import java.nio.charset.Charset; 041import java.util.Collections; 042import java.util.HashSet; 043import java.util.Set; 044 045/** 046 * Abstract class to assist in implementing a call against a REST endpoint. 047 * <p> 048 * Concrete implementations must implement {@link #makeRequest(Object)} to construct a {@link RequestBuilder} from the 049 * provided parameterized request object, and {@link #parseResponse(HttpResponse)} to construct the parameterized {@link 050 * HttpResponse} object. 051 * <p> 052 * The REST call is executed by calling {@link #execute(Object)}. 053 * 054 * @param <RequestT> The type of the method-specific request object that will be used to construct an HTTP request 055 * @param <ResultT> The type of method-specific response object which will be constructed from the returned HTTP 056 * response 057 */ 058public abstract class AbstractMethod<RequestT, ResultT> implements Method<RequestT, ResultT> { 059 private static final Log LOG = LogFactory.getLog(AbstractMethod.class); 060 061 protected final HttpWrapper httpWrapper; 062 private Set<Class> acceptable; 063 064 public AbstractMethod(HttpWrapper httpWrapper) { 065 this.httpWrapper = httpWrapper; 066 } 067 068 /** 069 * Execute the REST call represented by this method object. 070 * 071 * @param request A RequestT representing input to the REST call to be made 072 * 073 * @return A ResultT representing the response from the executed REST call 074 * 075 * @throws NexmoClientException if there is a problem parsing the HTTP response 076 */ 077 public ResultT execute(RequestT request) throws NexmoResponseParseException, NexmoClientException { 078 try { 079 RequestBuilder requestBuilder = applyAuth(makeRequest(request)); 080 HttpUriRequest httpRequest = requestBuilder.build(); 081 082 // If we have a URL Encoded form entity, we may need to regenerate it as UTF-8 083 // due to a bug (or two!) in RequestBuilder: 084 // 085 // This fix can be removed when HttpClient is upgraded to 4.5, although 4.5 also 086 // has a bug where RequestBuilder.put(uri) and RequestBuilder.post(uri) use the 087 // wrong encoding, whereas RequestBuilder.put().setUri(uri) uses UTF-8. 088 // - MS 2017-04-12 089 if (httpRequest instanceof HttpEntityEnclosingRequest) { 090 HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpRequest; 091 HttpEntity entity = entityRequest.getEntity(); 092 if (entity instanceof UrlEncodedFormEntity) { 093 entityRequest.setEntity(new UrlEncodedFormEntity(requestBuilder.getParameters(), 094 Charset.forName("UTF-8") 095 )); 096 } 097 } 098 LOG.debug("Request: " + httpRequest); 099 if (LOG.isDebugEnabled() && httpRequest instanceof HttpEntityEnclosingRequestBase) { 100 HttpEntityEnclosingRequestBase enclosingRequest = (HttpEntityEnclosingRequestBase) httpRequest; 101 LOG.debug(EntityUtils.toString(enclosingRequest.getEntity())); 102 } 103 HttpResponse response = this.httpWrapper.getHttpClient().execute(httpRequest); 104 105 LOG.debug("Response: " + LoggingUtils.logResponse(response)); 106 107 try{ 108 return parseResponse(response); 109 } 110 catch (IOException io){ 111 throw new NexmoResponseParseException("Unable to parse response.", io); 112 } 113 } catch (UnsupportedEncodingException uee) { 114 throw new NexmoUnexpectedException("UTF-8 encoding is not supported by this JVM.", uee); 115 } catch (IOException io) { 116 throw new NexmoMethodFailedException("Something went wrong while executing the HTTP request: " + 117 io.getMessage() + ".", io); 118 } 119 } 120 121 /** 122 * Apply an appropriate authentication method (specified by {@link #getAcceptableAuthMethods()} to the provided 123 * {@link RequestBuilder}, and return the result. 124 * 125 * @param request A RequestBuilder which has not yet had authentication information applied 126 * 127 * @return A RequestBuilder with appropriate authentication information applied (may or not be the same instance as 128 * <pre>request</pre>) 129 * 130 * @throws NexmoClientException If no appropriate {@link AuthMethod} is available 131 */ 132 protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { 133 return getAuthMethod(getAcceptableAuthMethods()).apply(request); 134 } 135 136 /** 137 * Utility method for obtaining an appropriate {@link AuthMethod} for this call. 138 * 139 * @param acceptableAuthMethods an array of classes, representing authentication methods that are acceptable for 140 * this endpoint 141 * 142 * @return An AuthMethod created from one of the provided acceptableAuthMethods. 143 * 144 * @throws NexmoClientException If no AuthMethod is available from the provided array of acceptableAuthMethods. 145 */ 146 protected AuthMethod getAuthMethod(Class[] acceptableAuthMethods) throws NexmoClientException { 147 if (acceptable == null) { 148 this.acceptable = new HashSet<>(); 149 Collections.addAll(acceptable, acceptableAuthMethods); 150 } 151 152 return this.httpWrapper.getAuthCollection().getAcceptableAuthMethod(acceptable); 153 } 154 155 public void setHttpClient(HttpClient client) { 156 this.httpWrapper.setHttpClient(client); 157 } 158 159 protected abstract Class[] getAcceptableAuthMethods(); 160 161 /** 162 * Construct and return a RequestBuilder instance from the provided request. 163 * 164 * @param request A RequestT representing input to the REST call to be made 165 * 166 * @return A ResultT representing the response from the executed REST call 167 * 168 * @throws UnsupportedEncodingException if UTF-8 encoding is not supported by the JVM 169 */ 170 public abstract RequestBuilder makeRequest(RequestT request) throws UnsupportedEncodingException; 171 172 /** 173 * Construct a ResultT representing the contents of the HTTP response returned from the Nexmo Voice API. 174 * 175 * @param response An HttpResponse returned from the Nexmo Voice API 176 * 177 * @return A ResultT type representing the result of the REST call 178 * 179 * @throws IOException if a problem occurs parsing the response 180 */ 181 public abstract ResultT parseResponse(HttpResponse response) throws IOException; 182}