Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: set connect and read timeouts in ParsedURLData [BATIK-1366] #100

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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