aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java12
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java16
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java4
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java7
4 files changed, 38 insertions, 1 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
index 2af46da667d..50a35824b06 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
@@ -57,6 +57,7 @@ public class HttpConnector implements WsConnector {
public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30_000;
public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60_000;
+ public static final int DEFAULT_RESPONSE_TIMEOUT_MILLISECONDS = 0;
private static final String JSON = "application/json; charset=utf-8";
/**
@@ -85,6 +86,7 @@ public class HttpConnector implements WsConnector {
okHttpClientBuilder.setProxyLogin(builder.proxyLogin);
okHttpClientBuilder.setProxyPassword(builder.proxyPassword);
okHttpClientBuilder.setConnectTimeoutMs(builder.connectTimeoutMs);
+ okHttpClientBuilder.setResponseTimeoutMs(builder.responseTimeoutMs);
okHttpClientBuilder.setReadTimeoutMs(builder.readTimeoutMs);
okHttpClientBuilder.setSSLSocketFactory(builder.sslSocketFactory);
okHttpClientBuilder.setTrustManager(builder.sslTrustManager);
@@ -264,6 +266,7 @@ public class HttpConnector implements WsConnector {
private String systemPassCode;
private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
private int readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
+ private int responseTimeoutMs = DEFAULT_RESPONSE_TIMEOUT_MILLISECONDS;
private SSLSocketFactory sslSocketFactory = null;
private X509TrustManager sslTrustManager = null;
private boolean acceptGzip = false;
@@ -354,6 +357,15 @@ public class HttpConnector implements WsConnector {
return this;
}
+ /**
+ * Sets the response timeout to a specified timeout, in milliseconds.
+ * A timeout of zero is interpreted as an infinite timeout. Default value is {@link #DEFAULT_RESPONSE_TIMEOUT_MILLISECONDS}
+ */
+ public Builder responseTimeoutMilliseconds(int i) {
+ this.responseTimeoutMs = i;
+ return this;
+ }
+
public Builder proxy(@Nullable Proxy proxy) {
this.proxy = proxy;
return this;
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java
index 92be8643433..afb04328c27 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java
@@ -70,6 +70,7 @@ public class OkHttpClientBuilder {
private Boolean followRedirects;
private long connectTimeoutMs = -1;
private long readTimeoutMs = -1;
+ private long responseTimeoutMs = -1;
private SSLSocketFactory sslSocketFactory = null;
private X509TrustManager sslTrustManager = null;
private boolean acceptGzip = false;
@@ -170,6 +171,18 @@ public class OkHttpClientBuilder {
}
/**
+ * Sets the default response timeout for new connections. A value of 0 means no timeout.
+ * Default is to have no timeout.
+ */
+ public OkHttpClientBuilder setResponseTimeoutMs(long l) {
+ if (l < 0) {
+ throw new IllegalArgumentException("Response timeout must be positive. Got " + l);
+ }
+ this.responseTimeoutMs = l;
+ return this;
+ }
+
+ /**
* Set if redirects should be followed or not.
* Default is defined by OkHttp (true, follow redirects).
*/
@@ -187,6 +200,9 @@ public class OkHttpClientBuilder {
if (readTimeoutMs >= 0) {
builder.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS);
}
+ if (responseTimeoutMs >= 0) {
+ builder.callTimeout(responseTimeoutMs, TimeUnit.MILLISECONDS);
+ }
builder.addNetworkInterceptor(this::addHeaders);
if(!acceptGzip) {
builder.addNetworkInterceptor(new GzipRejectorInterceptor());
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
index bbc9c0e127d..981d0eb145d 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
@@ -107,7 +107,7 @@ public class HttpConnectorTest {
server.enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Encoding", "gzip")
.setBody(gzip("potentially a body with 100 GB of data normally encoded in gzip")));
- //by default we dont accept gzip
+ // by default we dont accept gzip
underTest = HttpConnector.newBuilder().url(serverUrl).build();
GetRequest request = new GetRequest("rest/api/1.0/repos");
@@ -319,10 +319,12 @@ public class HttpConnectorTest {
.url(serverUrl)
.readTimeoutMilliseconds(42)
.connectTimeoutMilliseconds(74)
+ .responseTimeoutMilliseconds(53)
.build();
assertThat(underTest.okHttpClient().readTimeoutMillis()).isEqualTo(42);
assertThat(underTest.okHttpClient().connectTimeoutMillis()).isEqualTo(74);
+ assertThat(underTest.okHttpClient().callTimeoutMillis()).isEqualTo(53);
}
@Test
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java
index 7881f554f92..4daae827e66 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java
@@ -83,4 +83,11 @@ public class OkHttpClientBuilderTest {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Read timeout must be positive. Got -10");
}
+
+ @Test
+ public void build_throws_IAE_if_response_timeout_is_negative() {
+ assertThatThrownBy(() -> underTest.setResponseTimeoutMs(-10))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Response timeout must be positive. Got -10");
+ }
}