Skip to content

Commit

Permalink
fix: Close HttpResponse in AbstractMethod (#549)
Browse files Browse the repository at this point in the history
* fix: Close HttpResponse in AbstractMethod

* refactor: Add RedactResponseException

* chore: Bump version

* docs: Complete Javadocs for VonageClient
  • Loading branch information
SMadani authored Nov 14, 2024
1 parent e0e00b0 commit 42dd627
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

# [8.14.0] - 2024-11-??
- Close HTTP responses to prevent resource leaks
- Added `RedactResponseException` and deprecated `VonageBadRequestException`

# [8.13.0] - 2024-10-28
- Added support for Verify custom templates

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.vonage</groupId>
<artifactId>server-sdk</artifactId>
<version>8.13.1</version>
<version>8.14.0</version>

<name>Vonage Java Server SDK</name>
<description>Java client for Vonage APIs</description>
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/vonage/client/AbstractMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import com.vonage.client.auth.*;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -77,20 +76,20 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
.setHeader("User-Agent", httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();

HttpResponse response = httpWrapper.getHttpClient().execute(httpRequest);
try {
return postProcessParsedResponse(parseResponse(response));
try (CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
try {
return postProcessParsedResponse(parseResponse(response));
}
catch (IOException iox) {
throw new VonageResponseParseException(iox);
}
}
catch (IOException io) {
throw new VonageResponseParseException("Unable to parse response.", io);
catch (IOException iox) {
throw new VonageMethodFailedException("Something went wrong while executing the HTTP request.", iox);
}
}
catch (UnsupportedEncodingException uee) {
throw new VonageUnexpectedException("UTF-8 encoding is not supported by this JVM.", uee);
}
catch (IOException io) {
throw new VonageMethodFailedException("Something went wrong while executing the HTTP request: " +
io.getMessage() + ".", io);
catch (UnsupportedEncodingException uex) {
throw new VonageUnexpectedException("UTF-8 encoding is not supported by this JVM.", uex);
}
}

Expand All @@ -105,7 +104,7 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
*
* @throws VonageClientException If no appropriate {@link AuthMethod} is available
*/
protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException {
final RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException {
AuthMethod am = getAuthMethod();
if (am instanceof HeaderAuthMethod) {
request.setHeader("Authorization", ((HeaderAuthMethod) am).getHeaderValue());
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/vonage/client/HttpWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import java.nio.charset.StandardCharsets;
Expand All @@ -34,12 +35,12 @@
public class HttpWrapper {
private static final String
CLIENT_NAME = "vonage-java-sdk",
CLIENT_VERSION = "8.13.1",
CLIENT_VERSION = "8.14.0",
JAVA_VERSION = System.getProperty("java.version"),
USER_AGENT = String.format("%s/%s java/%s", CLIENT_NAME, CLIENT_VERSION, JAVA_VERSION);

private AuthCollection authCollection;
private HttpClient httpClient;
private CloseableHttpClient httpClient;
private HttpConfig httpConfig;

public HttpWrapper(HttpConfig httpConfig, AuthCollection authCollection) {
Expand All @@ -64,7 +65,7 @@ public HttpWrapper(HttpConfig httpConfig, AuthMethod... authMethods) {
*
* @return The Apache HTTP client instance.
*/
public HttpClient getHttpClient() {
public CloseableHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = createHttpClient();
}
Expand Down Expand Up @@ -103,7 +104,7 @@ public String getApiKey() {

@Deprecated
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
this.httpClient = (CloseableHttpClient) httpClient;
}

@Deprecated
Expand All @@ -125,7 +126,7 @@ public void setAuthCollection(AuthCollection authCollection) {
this.authCollection = authCollection;
}

protected HttpClient createHttpClient() {
protected CloseableHttpClient createHttpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(200);
connectionManager.setMaxTotal(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vonage.client;

@Deprecated
public class VonageBadRequestException extends VonageClientException {
public VonageBadRequestException() {
super();
Expand Down
Loading

0 comments on commit 42dd627

Please sign in to comment.