Skip to content

Commit ba5fa31

Browse files
committed
Bug 1850284: Close response after use
1 parent 74d549a commit ba5fa31

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/main/java/io/fabric8/elasticsearch/plugin/OpenshiftAPIService.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,22 @@ public String userName(final String token) {
8282
} catch (IOException e) {
8383
LOGGER.error("Error retrieving username from token", e);
8484
throw new ElasticsearchException(e);
85-
}
85+
} finally {
86+
if (response != null ) {
87+
response.close();
88+
}
89+
}
8690
}
8791

8892
public Set<Project> projectNames(final String token){
93+
Response response = null;
8994
try (DefaultOpenShiftClient client = factory.buildClient(token)) {
9095
Request request = new Request.Builder()
9196
.url(client.getMasterUrl() + "apis/project.openshift.io/v1/projects")
9297
.header("Authorization", "Bearer " + token)
9398
.header(ACCEPT, APPLICATION_JSON)
9499
.build();
95-
Response response = client.getHttpClient().newCall(request).execute();
100+
response = client.getHttpClient().newCall(request).execute();
96101
if(response.code() != RestStatus.OK.getStatus()) {
97102
throw new ElasticsearchSecurityException("Unable to retrieve users's project list", RestStatus.fromCode(response.code()));
98103
}
@@ -108,6 +113,10 @@ public Set<Project> projectNames(final String token){
108113
} catch (IOException e) {
109114
LOGGER.error("Error retrieving project list", e);
110115
throw new ElasticsearchException(e);
116+
} finally {
117+
if (response != null ) {
118+
response.close();
119+
}
111120
}
112121
}
113122

@@ -128,6 +137,7 @@ public Set<Project> projectNames(final String token){
128137
*/
129138
public boolean localSubjectAccessReview(final String token,
130139
final String project, final String verb, final String resource, final String resourceAPIGroup, final String [] scopes) {
140+
Response response = null;
131141
try (DefaultOpenShiftClient client = factory.buildClient(token)) {
132142
XContentBuilder payload = XContentFactory.jsonBuilder()
133143
.startObject()
@@ -152,7 +162,7 @@ public boolean localSubjectAccessReview(final String token,
152162
.post(RequestBody.create(MediaType.parse(APPLICATION_JSON), payload.string()))
153163
.build();
154164
log(request);
155-
Response response = client.getHttpClient().newCall(request).execute();
165+
response = client.getHttpClient().newCall(request).execute();
156166
final String body = IOUtils.toString(response.body().byteStream());
157167
log(response, body);
158168
if(response.code() != RestStatus.CREATED.getStatus()) {
@@ -161,6 +171,10 @@ public boolean localSubjectAccessReview(final String token,
161171
return JsonPath.read(body, "$.allowed");
162172
} catch (IOException e) {
163173
LOGGER.error("Error determining user's role", e);
174+
} finally {
175+
if (response != null ) {
176+
response.close();
177+
}
164178
}
165179
return false;
166180
}

0 commit comments

Comments
 (0)