Skip to content

Commit

Permalink
Make MeshResponse Closeable
Browse files Browse the repository at this point in the history
  • Loading branch information
npomaroli committed Sep 21, 2022
1 parent 2a53148 commit 5ea7c17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public T getBody() {
public List<String> getCookies() {
throw new RuntimeException("There are no cookies in local requests");
}

@Override
public void close() {
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.gentics.mesh.rest.client;

import java.io.Closeable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface MeshResponse<T> {
/**
* Common interface for Gentics Mesh REST Client responses.
*
* @param <T>
* Response type
* @implNote It is important to close the response by calling {@link #close()} when the response is no longer needed. Failing to do so might lead to a connection leak.
*/
public interface MeshResponse<T> extends Closeable {
/**
* Retrieve the response headers
* @return A map of all response headers
Expand Down Expand Up @@ -62,4 +70,7 @@ default List<String> getCookies() {
* @return The body as the specified type
*/
T getBody();

@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ public String getBodyAsString() {
public T getBody() {
return body.get();
}

@Override
public void close() {
Optional.ofNullable(response).map(Response::body).ifPresent(ResponseBody::close);
}
});
}

Expand Down

0 comments on commit 5ea7c17

Please sign in to comment.