Commit 2214d2ba authored by Tim Olshansky's avatar Tim Olshansky

Merge pull request #104 from apanasevich/master

Adds request timeout support.
parents e6159742 81ade3b6
...@@ -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;
...@@ -299,6 +296,11 @@ public class GitlabHTTPRequestor { ...@@ -299,6 +296,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) {
...@@ -354,6 +356,9 @@ public class GitlabHTTPRequestor { ...@@ -354,6 +356,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