Skip to content

Commit ac76476

Browse files
committed
Address resource leak issue identified by Fortify
1 parent 7f4d34b commit ac76476

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

wls-exporter-core/src/main/java/com/oracle/wls/exporter/WebClient8Impl.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.io.OutputStream;
89
import java.net.HttpURLConnection;
910
import java.net.URI;
1011
import java.net.URISyntaxException;
@@ -22,8 +23,8 @@
2223
public class WebClient8Impl extends WebClientCommon {
2324

2425
static class Header {
25-
private String name;
26-
private String value;
26+
private final String name;
27+
private final String value;
2728

2829
Header(String name, String value) {
2930
this.name = name;
@@ -116,18 +117,19 @@ public Java8WebRequestWithBody(String method, String url, String body) {
116117
public void completeRequest(HttpURLConnection connection) throws IOException {
117118
super.completeRequest(connection);
118119
connection.setDoOutput(true);
119-
connection.getOutputStream().write(body.getBytes());
120+
121+
try (final OutputStream outputStream = connection.getOutputStream()) {
122+
outputStream.write(body.getBytes());
123+
}
120124
}
121125
}
122126

123127
static class Java8WebResponse implements WebResponse {
124-
private final WebRequest request;
125128
private final HttpURLConnection connection;
126129
private final int responseCode;
127130
private final Map<String, List<String>> headerFields;
128131

129-
Java8WebResponse(WebRequest request, HttpURLConnection connection) throws IOException {
130-
this.request = request;
132+
Java8WebResponse(HttpURLConnection connection) throws IOException {
131133
this.connection = connection;
132134
responseCode = connection.getResponseCode();
133135
headerFields = connection.getHeaderFields();
@@ -153,7 +155,7 @@ public Stream<String> getHeadersAsStream(String headerName) {
153155
}
154156

155157
@Override
156-
public void close() throws IOException {
158+
public void close() {
157159

158160
}
159161
}
@@ -164,7 +166,7 @@ public WebResponse send(WebRequest request) throws IOException {
164166
HttpURLConnection connection = openConnection(request.getURI().toURL());
165167
((Java8WebRequest) request).completeRequest(connection);
166168

167-
return new Java8WebResponse(request, connection);
169+
return new Java8WebResponse(connection);
168170
}
169171

170172
/**
@@ -186,7 +188,7 @@ private HttpURLConnection openConnection(URL url ) throws IOException {
186188
}
187189

188190
@Override
189-
public void close() throws IOException {
191+
public void close() {
190192

191193
}
192194
}

wls-exporter-core/src/main/java/com/oracle/wls/exporter/WebClientCommon.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ private String sendRequest(WebRequest request) throws IOException {
142142

143143
private String getReply(WebResponse response) throws IOException {
144144
processStatusCode(response);
145-
return toString(response.getContents());
145+
try (final InputStream contents = response.getContents()) {
146+
return toString(contents);
147+
}
146148
}
147149

148150
private void processStatusCode(WebResponse response) {

0 commit comments

Comments
 (0)