Commit 81ade3b6 authored by Apanasevich Dmitrii's avatar Apanasevich Dmitrii

Adds request timeout support. Now big commit diffs can be handled with SocketTimeoutException.

parent d82e68ed
......@@ -38,6 +38,7 @@ public class GitlabAPI {
private final TokenType tokenType;
private AuthMethod authMethod;
private boolean ignoreCertificateErrors = false;
private int requestTimeout = 0;
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
......@@ -70,6 +71,15 @@ public class GitlabAPI {
return this;
}
public int getRequestTimeout() {
return requestTimeout;
}
public GitlabAPI setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
return this;
}
public GitlabHTTPRequestor retrieve() {
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod);
}
......
......@@ -13,10 +13,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -296,6 +293,11 @@ public class GitlabHTTPRequestor {
connection.setRequestProperty(tokenType.getTokenHeaderName(), String.format(tokenType.getTokenHeaderFormat(), apiToken));
}
final int requestTimeout = root.getRequestTimeout();
if (requestTimeout > 0) {
connection.setReadTimeout(requestTimeout);
}
try {
connection.setRequestMethod(method);
} catch (ProtocolException e) {
......@@ -351,6 +353,9 @@ public class GitlabHTTPRequestor {
if (e instanceof FileNotFoundException) {
throw e; // pass through 404 Not Found to allow the caller to handle it intelligently
}
if (e instanceof SocketTimeoutException && root.getRequestTimeout() > 0) {
throw e;
}
InputStream es = wrapStream(connection, connection.getErrorStream());
try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment