From 5ccf682bc2bcdb30c03dc881bbc95710d22089c9 Mon Sep 17 00:00:00 2001 From: Carlos Amengual Date: Fri, 10 May 2024 22:10:52 +0200 Subject: [PATCH] util: set connect and read timeouts in ParsedURLData [BATIK-1366] Default connect timeout is 10 seconds, read timeout is 4 minutes to allow for large reads. --- .../sf/carte/echosvg/util/ParsedURLData.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/echosvg-util/src/main/java/io/sf/carte/echosvg/util/ParsedURLData.java b/echosvg-util/src/main/java/io/sf/carte/echosvg/util/ParsedURLData.java index 2958a384c..9680938bc 100644 --- a/echosvg-util/src/main/java/io/sf/carte/echosvg/util/ParsedURLData.java +++ b/echosvg-util/src/main/java/io/sf/carte/echosvg/util/ParsedURLData.java @@ -108,6 +108,9 @@ public static InputStream checkGZIP(InputStream is) throws IOException { return is; } + private static int connectTimeout = 10000; + private static int readTimeout = 240000; + /** * Since the Data instance is 'hidden' in the ParsedURL instance we make all our * methods public. This makes it easy for the various Protocol Handlers to @@ -192,6 +195,53 @@ protected URL buildURL() throws MalformedURLException { return new URL(toString()); } + /** + * Gets the connect timeout. + * + * @return the connect timeout. + */ + public static int getConnectTimeout() { + return connectTimeout; + } + + /** + * Sets the connect timeout. + *

+ * Default is 10 seconds. + *

+ * + * @param connectTimeout the connect timeout. + * @throws IllegalArgumentException if the argument is negative. + */ + public static void setConnectTimeout(int connectTimeout) { + if (connectTimeout < 0) { + throw new IllegalArgumentException("Cannot set negative timeout."); + } + ParsedURLData.connectTimeout = connectTimeout; + } + + /** + * Gets the read timeout. + * + * @return the read timeout. + */ + public static int getReadTimeout() { + return readTimeout; + } + + /** + * Sets the read timeout. + * + * @param readTimeout the read timeout. + * @throws IllegalArgumentException if the argument is negative. + */ + public static void setReadTimeout(int readTimeout) { + if (readTimeout < 0) { + throw new IllegalArgumentException("Cannot set negative timeout."); + } + ParsedURLData.readTimeout = readTimeout; + } + /** * Implement Object.hashCode. */ @@ -546,6 +596,9 @@ protected InputStream openStreamInternal(String userAgent, Iterator mime urlC.setRequestProperty(HTTP_ACCEPT_ENCODING_HEADER, encodingHeader); } + urlC.setConnectTimeout(connectTimeout); + urlC.setReadTimeout(readTimeout); + contentType = urlC.getContentType(); contentEncoding = urlC.getContentEncoding(); postConnectionURL = urlC.getURL();