This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new ffed1a2ed5 Improve URLConnection wrapper
ffed1a2ed5 is described below

commit ffed1a2ed50dda4b53de434bade2f132d72fae8b
Author: remm <[email protected]>
AuthorDate: Wed Jul 1 11:06:45 2026 +0200

    Improve URLConnection wrapper
    
    Implement all methods from URLConnection.
    Improve close strategy for JARs.
---
 .../tomcat/util/buf/CloseableURLConnection.java    | 209 ++++++++++++++++++++-
 1 file changed, 204 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/tomcat/util/buf/CloseableURLConnection.java 
b/java/org/apache/tomcat/util/buf/CloseableURLConnection.java
index 62e4b8fa37..e6807fd058 100644
--- a/java/org/apache/tomcat/util/buf/CloseableURLConnection.java
+++ b/java/org/apache/tomcat/util/buf/CloseableURLConnection.java
@@ -22,6 +22,9 @@ import java.net.HttpURLConnection;
 import java.net.JarURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.security.Permission;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.tomcat.util.ExceptionUtils;
 
@@ -81,7 +84,8 @@ public final class CloseableURLConnection extends 
URLConnection implements AutoC
 
 
     /**
-     * Returns the wrapped URLConnection.
+     * Returns the wrapped URLConnection. Some subclasses can have additional
+     * methods, in which case the wrapped URLConnection needs to be accessed.
      *
      * @return the wrapped URLConnection
      */
@@ -131,13 +135,14 @@ public final class CloseableURLConnection extends 
URLConnection implements AutoC
         if (trackedStream != null) {
             try {
                 trackedStream.close();
-            } catch (IOException e) {
-                // Ignore
+            } catch (Exception e) {
+                ExceptionUtils.handleThrowable(e);
             }
         } else if (connection instanceof JarURLConnection) {
             try (@SuppressWarnings("unused")
-            InputStream is = connection.getInputStream()) {
-                // Open and immediately close to release the JarFile
+                java.util.jar.JarFile jarFile = ((JarURLConnection) 
connection).getJarFile()) {
+                // Explicitly close the JarFile to release its native 
resources.
+                // As setUseCaches(false) is set, this should not cause side 
effects on other streams.
             } catch (Exception e) {
                 ExceptionUtils.handleThrowable(e);
             }
@@ -146,6 +151,7 @@ public final class CloseableURLConnection extends 
URLConnection implements AutoC
         if (connection instanceof HttpURLConnection) {
             ((HttpURLConnection) connection).disconnect();
         }
+
     }
 
 
@@ -192,4 +198,197 @@ public final class CloseableURLConnection extends 
URLConnection implements AutoC
         return connection.getHeaderField(name);
     }
 
+
+    @Override
+    public URL getURL() {
+        return connection.getURL();
+    }
+
+
+    @Override
+    public String getContentEncoding() {
+        return connection.getContentEncoding();
+    }
+
+
+    @Override
+    public long getExpiration() {
+        return connection.getExpiration();
+    }
+
+
+    @Override
+    public long getDate() {
+        return connection.getDate();
+    }
+
+
+    @Override
+    public Map<String, List<String>> getHeaderFields() {
+        return connection.getHeaderFields();
+    }
+
+
+    @Override
+    public int getHeaderFieldInt(String name, int defaultValue) {
+        return connection.getHeaderFieldInt(name, defaultValue);
+    }
+
+
+    @Override
+    public long getHeaderFieldLong(String name, long defaultValue) {
+        return connection.getHeaderFieldLong(name, defaultValue);
+    }
+
+
+    @Override
+    public long getHeaderFieldDate(String name, long defaultValue) {
+        return connection.getHeaderFieldDate(name, defaultValue);
+    }
+
+
+    @Override
+    public String getHeaderFieldKey(int n) {
+        return connection.getHeaderFieldKey(n);
+    }
+
+
+    @Override
+    public String getHeaderField(int n) {
+        return connection.getHeaderField(n);
+    }
+
+
+    @Override
+    public Object getContent() throws IOException {
+        return connection.getContent();
+    }
+
+
+    @Override
+    public Object getContent(Class<?>[] classes) throws IOException {
+        return connection.getContent(classes);
+    }
+
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public Permission getPermission() throws IOException {
+        return connection.getPermission();
+    }
+
+
+    @Override
+    public String toString() {
+        return connection.toString();
+    }
+
+
+    @Override
+    public void setDoInput(boolean doinput) {
+        connection.setDoInput(doinput);
+    }
+
+
+    @Override
+    public boolean getDoInput() {
+        return connection.getDoInput();
+    }
+
+
+    @Override
+    public void setDoOutput(boolean dooutput) {
+        connection.setDoOutput(dooutput);
+    }
+
+
+    @Override
+    public boolean getDoOutput() {
+        return connection.getDoOutput();
+    }
+
+
+    @Override
+    public void setAllowUserInteraction(boolean allowuserinteraction) {
+        connection.setAllowUserInteraction(allowuserinteraction);
+    }
+
+
+    @Override
+    public boolean getAllowUserInteraction() {
+        return connection.getAllowUserInteraction();
+    }
+
+
+    @Override
+    public void setUseCaches(boolean usecaches) {
+        connection.setUseCaches(usecaches);
+    }
+
+
+    @Override
+    public boolean getUseCaches() {
+        return connection.getUseCaches();
+    }
+
+
+    @Override
+    public void setIfModifiedSince(long ifmodifiedsince) {
+        connection.setIfModifiedSince(ifmodifiedsince);
+    }
+
+
+    @Override
+    public long getIfModifiedSince() {
+        return connection.getIfModifiedSince();
+    }
+
+
+    @Override
+    public boolean getDefaultUseCaches() {
+        return connection.getDefaultUseCaches();
+    }
+
+
+    @Override
+    public void setDefaultUseCaches(boolean defaultusecaches) {
+        connection.setDefaultUseCaches(defaultusecaches);
+    }
+
+
+    @Override
+    public void setRequestProperty(String key, String value) {
+        connection.setRequestProperty(key, value);
+    }
+
+
+    @Override
+    public void addRequestProperty(String key, String value) {
+        connection.addRequestProperty(key, value);
+    }
+
+
+    @Override
+    public String getRequestProperty(String key) {
+        return connection.getRequestProperty(key);
+    }
+
+
+    @Override
+    public Map<String, List<String>> getRequestProperties() {
+        return connection.getRequestProperties();
+    }
+
+
+    @Override
+    public int hashCode() {
+        return connection.hashCode();
+    }
+
+
+    @Override
+    public boolean equals(Object obj) {
+        return connection.equals(obj);
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to