Skip to content

Commit ffed1a2

Browse files
committed
Improve URLConnection wrapper
Implement all methods from URLConnection. Improve close strategy for JARs.
1 parent a3e0241 commit ffed1a2

1 file changed

Lines changed: 204 additions & 5 deletions

File tree

java/org/apache/tomcat/util/buf/CloseableURLConnection.java

Lines changed: 204 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.net.JarURLConnection;
2323
import java.net.URL;
2424
import java.net.URLConnection;
25+
import java.security.Permission;
26+
import java.util.List;
27+
import java.util.Map;
2528

2629
import org.apache.tomcat.util.ExceptionUtils;
2730

@@ -81,7 +84,8 @@ public CloseableURLConnection(URLConnection connection) {
8184

8285

8386
/**
84-
* Returns the wrapped URLConnection.
87+
* Returns the wrapped URLConnection. Some subclasses can have additional
88+
* methods, in which case the wrapped URLConnection needs to be accessed.
8589
*
8690
* @return the wrapped URLConnection
8791
*/
@@ -131,13 +135,14 @@ public void close() {
131135
if (trackedStream != null) {
132136
try {
133137
trackedStream.close();
134-
} catch (IOException e) {
135-
// Ignore
138+
} catch (Exception e) {
139+
ExceptionUtils.handleThrowable(e);
136140
}
137141
} else if (connection instanceof JarURLConnection) {
138142
try (@SuppressWarnings("unused")
139-
InputStream is = connection.getInputStream()) {
140-
// Open and immediately close to release the JarFile
143+
java.util.jar.JarFile jarFile = ((JarURLConnection) connection).getJarFile()) {
144+
// Explicitly close the JarFile to release its native resources.
145+
// As setUseCaches(false) is set, this should not cause side effects on other streams.
141146
} catch (Exception e) {
142147
ExceptionUtils.handleThrowable(e);
143148
}
@@ -146,6 +151,7 @@ public void close() {
146151
if (connection instanceof HttpURLConnection) {
147152
((HttpURLConnection) connection).disconnect();
148153
}
154+
149155
}
150156

151157

@@ -192,4 +198,197 @@ public String getHeaderField(String name) {
192198
return connection.getHeaderField(name);
193199
}
194200

201+
202+
@Override
203+
public URL getURL() {
204+
return connection.getURL();
205+
}
206+
207+
208+
@Override
209+
public String getContentEncoding() {
210+
return connection.getContentEncoding();
211+
}
212+
213+
214+
@Override
215+
public long getExpiration() {
216+
return connection.getExpiration();
217+
}
218+
219+
220+
@Override
221+
public long getDate() {
222+
return connection.getDate();
223+
}
224+
225+
226+
@Override
227+
public Map<String, List<String>> getHeaderFields() {
228+
return connection.getHeaderFields();
229+
}
230+
231+
232+
@Override
233+
public int getHeaderFieldInt(String name, int defaultValue) {
234+
return connection.getHeaderFieldInt(name, defaultValue);
235+
}
236+
237+
238+
@Override
239+
public long getHeaderFieldLong(String name, long defaultValue) {
240+
return connection.getHeaderFieldLong(name, defaultValue);
241+
}
242+
243+
244+
@Override
245+
public long getHeaderFieldDate(String name, long defaultValue) {
246+
return connection.getHeaderFieldDate(name, defaultValue);
247+
}
248+
249+
250+
@Override
251+
public String getHeaderFieldKey(int n) {
252+
return connection.getHeaderFieldKey(n);
253+
}
254+
255+
256+
@Override
257+
public String getHeaderField(int n) {
258+
return connection.getHeaderField(n);
259+
}
260+
261+
262+
@Override
263+
public Object getContent() throws IOException {
264+
return connection.getContent();
265+
}
266+
267+
268+
@Override
269+
public Object getContent(Class<?>[] classes) throws IOException {
270+
return connection.getContent(classes);
271+
}
272+
273+
274+
@Override
275+
@SuppressWarnings("deprecation")
276+
public Permission getPermission() throws IOException {
277+
return connection.getPermission();
278+
}
279+
280+
281+
@Override
282+
public String toString() {
283+
return connection.toString();
284+
}
285+
286+
287+
@Override
288+
public void setDoInput(boolean doinput) {
289+
connection.setDoInput(doinput);
290+
}
291+
292+
293+
@Override
294+
public boolean getDoInput() {
295+
return connection.getDoInput();
296+
}
297+
298+
299+
@Override
300+
public void setDoOutput(boolean dooutput) {
301+
connection.setDoOutput(dooutput);
302+
}
303+
304+
305+
@Override
306+
public boolean getDoOutput() {
307+
return connection.getDoOutput();
308+
}
309+
310+
311+
@Override
312+
public void setAllowUserInteraction(boolean allowuserinteraction) {
313+
connection.setAllowUserInteraction(allowuserinteraction);
314+
}
315+
316+
317+
@Override
318+
public boolean getAllowUserInteraction() {
319+
return connection.getAllowUserInteraction();
320+
}
321+
322+
323+
@Override
324+
public void setUseCaches(boolean usecaches) {
325+
connection.setUseCaches(usecaches);
326+
}
327+
328+
329+
@Override
330+
public boolean getUseCaches() {
331+
return connection.getUseCaches();
332+
}
333+
334+
335+
@Override
336+
public void setIfModifiedSince(long ifmodifiedsince) {
337+
connection.setIfModifiedSince(ifmodifiedsince);
338+
}
339+
340+
341+
@Override
342+
public long getIfModifiedSince() {
343+
return connection.getIfModifiedSince();
344+
}
345+
346+
347+
@Override
348+
public boolean getDefaultUseCaches() {
349+
return connection.getDefaultUseCaches();
350+
}
351+
352+
353+
@Override
354+
public void setDefaultUseCaches(boolean defaultusecaches) {
355+
connection.setDefaultUseCaches(defaultusecaches);
356+
}
357+
358+
359+
@Override
360+
public void setRequestProperty(String key, String value) {
361+
connection.setRequestProperty(key, value);
362+
}
363+
364+
365+
@Override
366+
public void addRequestProperty(String key, String value) {
367+
connection.addRequestProperty(key, value);
368+
}
369+
370+
371+
@Override
372+
public String getRequestProperty(String key) {
373+
return connection.getRequestProperty(key);
374+
}
375+
376+
377+
@Override
378+
public Map<String, List<String>> getRequestProperties() {
379+
return connection.getRequestProperties();
380+
}
381+
382+
383+
@Override
384+
public int hashCode() {
385+
return connection.hashCode();
386+
}
387+
388+
389+
@Override
390+
public boolean equals(Object obj) {
391+
return connection.equals(obj);
392+
}
393+
195394
}

0 commit comments

Comments
 (0)