Skip to content

Commit

Permalink
Merge pull request #350 from snieguu/master
Browse files Browse the repository at this point in the history
Version 2.67.2 properly release system resource for ApacheHttpClient connection
  • Loading branch information
lastverb authored Nov 16, 2018
2 parents 595e47f + 167f4b8 commit b1e0f7a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testdroid</groupId>
<artifactId>testdroid-api</artifactId>
<version>2.67.1</version>
<version>2.67.2</version>
<packaging>jar</packaging>
<name>Testdroid API v2</name>
<url>https://github.com/bitbar/testdroid-api</url>
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/testdroid/api/AbstractAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public abstract class AbstractAPIClient implements APIClient {

protected HttpTransport httpTransport;

protected ObjectMapper objectMapper = new ObjectMapper()
protected final ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

protected String apiURL;

private static List<Integer> POSSIBLE_DELETE_STATUSES = Arrays.asList(SC_OK, SC_ACCEPTED, SC_NO_CONTENT);
private static final List<Integer> POSSIBLE_DELETE_STATUSES = Arrays.asList(SC_OK, SC_ACCEPTED, SC_NO_CONTENT);

protected HttpRequestFactory getRequestFactory() throws APIException {
return httpTransport.createRequestFactory();
Expand Down Expand Up @@ -90,7 +90,7 @@ protected <T extends APIEntity> T getOnce(String uri, Context<?> context, TypeRe
// Build request
HttpRequestFactory factory = getRequestFactory();
HttpRequest request;
HttpResponse response;
HttpResponse response = null;
try {
// Call request and parse result
request = factory.buildGetRequest(new GenericUrl(buildUrl(apiURL + uri, context)));
Expand All @@ -116,6 +116,8 @@ protected <T extends APIEntity> T getOnce(String uri, Context<?> context, TypeRe
} catch (IOException ex) {
throw new APIException(String
.format("Failed to execute API call: %s. Reason: %s", uri, ex.getMessage()), ex);
} finally {
disconnectQuitely(response);
}
}

Expand Down Expand Up @@ -154,7 +156,7 @@ protected <T extends APIEntity> T postOnce(String uri, Object body, String conte
}
HttpRequestFactory factory = getRequestFactory();
HttpRequest request;
HttpResponse response;
HttpResponse response = null;
String resourceUrl = apiURL + uri;
try {
HttpContent content;
Expand Down Expand Up @@ -222,6 +224,8 @@ protected <T extends APIEntity> T postOnce(String uri, Object body, String conte
} catch (IOException ex) {
throw new APIException(String
.format("Failed to execute API call: %s. Reason: %s", uri, ex.getMessage()), ex);
} finally {
disconnectQuitely(response);
}
}

Expand All @@ -239,7 +243,7 @@ public void delete(String uri) throws APIException {
protected void deleteOnce(String uri) throws APIException {
HttpRequestFactory factory = getRequestFactory();
HttpRequest request;
HttpResponse response;
HttpResponse response = null;
try {
request = factory.buildDeleteRequest(new GenericUrl(apiURL + uri));
request.setHeaders(getHttpHeaders());
Expand All @@ -260,6 +264,8 @@ protected void deleteOnce(String uri) throws APIException {
} catch (IOException ex) {
throw new APIException(String
.format("Failed to execute API call: %s. Reason: %s", uri, ex.getMessage()), ex);
} finally {
disconnectQuitely(response);
}
}

Expand Down Expand Up @@ -318,7 +324,7 @@ protected <T extends APIEntity> String buildUrl(String url, Context<T> context)
}
}

protected Map fixMapParameters(Map<String, Object> map) {
protected void fixMapParameters(Map<String, Object> map) {
String key;
Object value;
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand All @@ -331,7 +337,6 @@ protected Map fixMapParameters(Map<String, Object> map) {
map.put(key, value.toString());
}
}
return map;
}

protected APIException getAPIException(HttpResponseException ex) {
Expand All @@ -343,4 +348,14 @@ protected APIException getAPIException(HttpResponseException ex) {
return new APIException(ex.getStatusCode(), ex.getMessage());
}
}

protected void disconnectQuitely(HttpResponse httpResponse) {
if (httpResponse != null) {
try {
httpResponse.disconnect();
} catch (IOException exc) {
//ignore
}
}
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/testdroid/api/DefaultAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private String getAccessToken() throws APIException {
}

protected String acquireAccessToken() throws APIException {
HttpResponse response = null;
try {
if (username == null && password == null) {
return "";
Expand All @@ -151,7 +152,7 @@ protected String acquireAccessToken() throws APIException {
request.setReadTimeout(HTTP_READ_TIMEOUT); // one minute
request.setHeaders(new HttpHeaders().setAccept("application/json"));

HttpResponse response = request.execute();
response = request.execute();
if (response.getStatusCode() != 200) {
throw new APIException(response.getStatusCode(), "Failed to acquire access token");
}
Expand All @@ -166,10 +167,13 @@ protected String acquireAccessToken() throws APIException {
.format("Failed to acquire access token. Reason: %s", ex.getStatusMessage()), ex);
} catch (IOException ex) {
throw new APIException(String.format("Failed to acquire access token. Reason: %s", ex.getMessage()), ex);
} finally {
disconnectQuitely(response);
}
}

protected String refreshAccessToken() throws APIException {
HttpResponse response = null;
try {
if (refreshToken == null) {
return null;
Expand All @@ -186,7 +190,7 @@ protected String refreshAccessToken() throws APIException {
request.setConnectTimeout(HTTP_CONNECT_TIMEOUT); // one minute
request.setReadTimeout(HTTP_READ_TIMEOUT); // one minute
request.setHeaders(getHttpHeaders());
HttpResponse response = request.execute();
response = request.execute();

if (response.getStatusCode() != 200) {
throw new APIException(response.getStatusCode(), "Failed to refresh access token");
Expand All @@ -199,6 +203,8 @@ protected String refreshAccessToken() throws APIException {
return json.get("access_token");
} catch (IOException ex) {
throw new APIException(String.format("Failed to refresh access token. Reason: %s", ex.getMessage()), ex);
} finally {
disconnectQuitely(response);
}
}

Expand Down

0 comments on commit b1e0f7a

Please sign in to comment.