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 { ...@@ -38,6 +38,7 @@ public class GitlabAPI {
private final TokenType tokenType; private final TokenType tokenType;
private AuthMethod authMethod; private AuthMethod authMethod;
private boolean ignoreCertificateErrors = false; private boolean ignoreCertificateErrors = false;
private int requestTimeout = 0;
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) { private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl; this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
...@@ -70,6 +71,15 @@ public class GitlabAPI { ...@@ -70,6 +71,15 @@ public class GitlabAPI {
return this; return this;
} }
public int getRequestTimeout() {
return requestTimeout;
}
public GitlabAPI setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
return this;
}
public GitlabHTTPRequestor retrieve() { public GitlabHTTPRequestor retrieve() {
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod); return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod);
} }
......
...@@ -13,10 +13,7 @@ import java.io.IOException; ...@@ -13,10 +13,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.HttpURLConnection; import java.net.*;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -296,6 +293,11 @@ public class GitlabHTTPRequestor { ...@@ -296,6 +293,11 @@ public class GitlabHTTPRequestor {
connection.setRequestProperty(tokenType.getTokenHeaderName(), String.format(tokenType.getTokenHeaderFormat(), apiToken)); connection.setRequestProperty(tokenType.getTokenHeaderName(), String.format(tokenType.getTokenHeaderFormat(), apiToken));
} }
final int requestTimeout = root.getRequestTimeout();
if (requestTimeout > 0) {
connection.setReadTimeout(requestTimeout);
}
try { try {
connection.setRequestMethod(method); connection.setRequestMethod(method);
} catch (ProtocolException e) { } catch (ProtocolException e) {
...@@ -351,6 +353,9 @@ public class GitlabHTTPRequestor { ...@@ -351,6 +353,9 @@ public class GitlabHTTPRequestor {
if (e instanceof FileNotFoundException) { if (e instanceof FileNotFoundException) {
throw e; // pass through 404 Not Found to allow the caller to handle it intelligently 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()); InputStream es = wrapStream(connection, connection.getErrorStream());
try { 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