Skip to content

Commit

Permalink
util: set connect and read timeouts in ParsedURLData [BATIK-1366]
Browse files Browse the repository at this point in the history
Default connect timeout is 10 seconds, read timeout is 4 minutes to allow for large reads.
  • Loading branch information
carlosame committed May 17, 2024
1 parent 029954a commit 5ad9d18
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
* <p>
* Default is 10 seconds.
* </p>
*
* @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.
*/
Expand Down Expand Up @@ -546,6 +596,9 @@ protected InputStream openStreamInternal(String userAgent, Iterator<String> mime
urlC.setRequestProperty(HTTP_ACCEPT_ENCODING_HEADER, encodingHeader);
}

urlC.setConnectTimeout(connectTimeout);
urlC.setReadTimeout(readTimeout);

contentType = urlC.getContentType();
contentEncoding = urlC.getContentEncoding();
postConnectionURL = urlC.getURL();
Expand Down

0 comments on commit 5ad9d18

Please sign in to comment.