Skip to content

Commit

Permalink
fix(apache): Close connections to prevent leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPicador committed Sep 20, 2017
1 parent 8a10af5 commit b8ceb0b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ protected AlgoliaHttpResponse request(@Nonnull AlgoliaHttpRequest request) throw

private AlgoliaHttpResponse from(final HTTPResponse httpResponse) {
return new AlgoliaHttpResponse() {
@Override
public void close() throws IOException {
}

@Override
public int getStatusCode() {
return httpResponse.getResponseCode();
}

@Override
public Reader getBody() throws IOException {
if(hasGzip(httpResponse.getHeaders())) {
if (hasGzip(httpResponse.getHeaders())) {
return new InputStreamReader(
new GZIPInputStream(new ByteArrayInputStream(httpResponse.getContent())),
UTF8
Expand All @@ -77,7 +81,7 @@ public Reader getBody() throws IOException {

private boolean hasGzip(List<HTTPHeader> headers) {
for (HTTPHeader header : headers) {
if(header.getName().equalsIgnoreCase("Content-Encoding") && header.getValue().toLowerCase().contains("gzip")) {
if (header.getName().equalsIgnoreCase("Content-Encoding") && header.getValue().toLowerCase().contains("gzip")) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public <T> T requestWithRetry(@Nonnull AlgoliaRequest<T> request) throws Algolia

switch (code) {
case 400:
throw new AlgoliaHttpException(code, message.length() > 0 ? message : "Bad build request");
throw new AlgoliaHttpException(code, message.length() > 0 ? message : "Bad build request");
case 403:
throw new AlgoliaHttpException(code, message.length() > 0 ? message : "Invalid Application-ID or API-Key");
case 404:
Expand All @@ -132,6 +132,12 @@ public <T> T requestWithRetry(@Nonnull AlgoliaRequest<T> request) throws Algolia
return Utils.parseAs(getObjectMapper(), response.getBody(), request.getJavaType(getObjectMapper().getTypeFactory()));
} catch (IOException e) {
throw new AlgoliaException("Error while deserialization the response", e);
} finally {
try {
response.close();
} catch (IOException e) {
throw new AlgoliaException("Can not close underlying response", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.algolia.search.http;

import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;

public interface AlgoliaHttpResponse {
public interface AlgoliaHttpResponse extends Closeable {

int getStatusCode();

Expand Down
3 changes: 2 additions & 1 deletion algoliasearch-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ private AlgoliaHttpResponse makeMockRequest() throws IOException {

private AlgoliaHttpResponse response(int status) {
return new AlgoliaHttpResponse() {
@Override
public void close() throws IOException {
}

@Override
public int getStatusCode() {
return status;
Expand Down Expand Up @@ -319,6 +323,10 @@ private class MockedAlgoliaHttpClient extends AlgoliaHttpClient {
@Override
public AlgoliaHttpResponse request(@Nonnull AlgoliaHttpRequest request) throws IOException {
return new AlgoliaHttpResponse() {
@Override
public void close() throws IOException {
}

@Override
public int getStatusCode() {
return 0;
Expand Down
3 changes: 2 additions & 1 deletion algoliasearch/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -75,9 +77,14 @@ protected AlgoliaHttpResponse request(@Nonnull AlgoliaHttpRequest request) throw
} else {
builder = builder.setEntity(new StringEntity("", ContentType.APPLICATION_JSON));
}
final HttpResponse response = internal.execute(builder.build());
final CloseableHttpResponse response = internal.execute(builder.build());

return new AlgoliaHttpResponse() {
@Override
public void close() throws IOException {
response.close();
}

@Override
public int getStatusCode() {
return response.getStatusLine().getStatusCode();
Expand Down

0 comments on commit b8ceb0b

Please sign in to comment.