diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-06-27 17:31:44 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-07-11 12:09:16 +0200 |
commit | e087cc91969df3e16be5be1dc421ffb0f2be830e (patch) | |
tree | 952019e129ef9ab4b0b0809d4e4291963bf9ad94 /sonar-ws | |
parent | b653291c29b67f522ff167ae51cf0ab0ddd7bf30 (diff) | |
download | sonarqube-e087cc91969df3e16be5be1dc421ffb0f2be830e.tar.gz sonarqube-e087cc91969df3e16be5be1dc421ffb0f2be830e.zip |
SONAR-7741 upgrade OK HTTP to 3.3.1
Diffstat (limited to 'sonar-ws')
4 files changed, 49 insertions, 50 deletions
diff --git a/sonar-ws/pom.xml b/sonar-ws/pom.xml index 553c52f8314..bdb55eb3106 100644 --- a/sonar-ws/pom.xml +++ b/sonar-ws/pom.xml @@ -27,7 +27,7 @@ <artifactId>guava</artifactId> </dependency> <dependency> - <groupId>com.squareup.okhttp</groupId> + <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> <dependency> @@ -54,7 +54,7 @@ <scope>test</scope> </dependency> <dependency> - <groupId>com.squareup.okhttp</groupId> + <groupId>com.squareup.okhttp3</groupId> <artifactId>mockwebserver</artifactId> <scope>test</scope> </dependency> 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 faa1d4e19e3..0e4af282deb 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 @@ -20,17 +20,6 @@ package org.sonarqube.ws.client; import com.google.common.annotations.VisibleForTesting; -import com.squareup.okhttp.Call; -import com.squareup.okhttp.ConnectionSpec; -import com.squareup.okhttp.Credentials; -import com.squareup.okhttp.Headers; -import com.squareup.okhttp.HttpUrl; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.MultipartBuilder; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.RequestBody; -import com.squareup.okhttp.Response; import java.io.IOException; import java.net.Proxy; import java.util.Map; @@ -38,6 +27,17 @@ import java.util.concurrent.TimeUnit; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.net.ssl.SSLSocketFactory; +import okhttp3.Call; +import okhttp3.ConnectionSpec; +import okhttp3.Credentials; +import okhttp3.Headers; +import okhttp3.HttpUrl; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; @@ -63,7 +63,7 @@ public class HttpConnector implements WsConnector { private final String userAgent; private final String credentials; private final String proxyCredentials; - private final OkHttpClient okHttpClient = new OkHttpClient(); + private final OkHttpClient okHttpClient; private HttpConnector(Builder builder, JavaVersion javaVersion) { this.baseUrl = HttpUrl.parse(builder.url.endsWith("/") ? builder.url : format("%s/", builder.url)); @@ -78,27 +78,33 @@ public class HttpConnector implements WsConnector { // the Basic credentials consider an empty password. this.credentials = Credentials.basic(builder.login, nullToEmpty(builder.password)); } - - if (builder.proxy != null) { - this.okHttpClient.setProxy(builder.proxy); - } // proxy credentials can be used on system-wide proxies, so even if builder.proxy is null if (isNullOrEmpty(builder.proxyLogin)) { this.proxyCredentials = null; } else { this.proxyCredentials = Credentials.basic(builder.proxyLogin, nullToEmpty(builder.proxyPassword)); } + this.okHttpClient = buildClient(builder, javaVersion); + } + + private static OkHttpClient buildClient(Builder builder, JavaVersion javaVersion) { + OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); + if (builder.proxy != null) { + okHttpClientBuilder.proxy(builder.proxy); + } - this.okHttpClient.setConnectTimeout(builder.connectTimeoutMs, TimeUnit.MILLISECONDS); - this.okHttpClient.setReadTimeout(builder.readTimeoutMs, TimeUnit.MILLISECONDS); + okHttpClientBuilder.connectTimeout(builder.connectTimeoutMs, TimeUnit.MILLISECONDS); + okHttpClientBuilder.readTimeout(builder.readTimeoutMs, TimeUnit.MILLISECONDS); ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .allEnabledTlsVersions() .allEnabledCipherSuites() .supportsTlsExtensions(true) .build(); - this.okHttpClient.setConnectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT)); - this.okHttpClient.setSslSocketFactory(createSslSocketFactory(javaVersion)); + okHttpClientBuilder.connectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT)); + okHttpClientBuilder.sslSocketFactory(createSslSocketFactory(javaVersion)); + + return okHttpClientBuilder.build(); } private static SSLSocketFactory createSslSocketFactory(JavaVersion javaVersion) { @@ -162,14 +168,15 @@ public class HttpConnector implements WsConnector { if (parts.isEmpty()) { okRequestBuilder.post(RequestBody.create(null, "")); } else { - MultipartBuilder body = new MultipartBuilder().type(MultipartBuilder.FORM); + MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); + bodyBuilder.setType(MultipartBody.FORM); for (Map.Entry<String, PostRequest.Part> param : parts.entrySet()) { PostRequest.Part part = param.getValue(); - body.addPart( + bodyBuilder.addPart( Headers.of("Content-Disposition", format("form-data; name=\"%s\"", param.getKey())), RequestBody.create(MediaType.parse(part.getMediaType()), part.getFile())); } - okRequestBuilder.post(body.build()); + okRequestBuilder.post(bodyBuilder.build()); } return doCall(okRequestBuilder.build()); @@ -209,7 +216,7 @@ public class HttpConnector implements WsConnector { Response okResponse = call.execute(); return new OkHttpResponse(okResponse); } catch (IOException e) { - throw new IllegalStateException("Fail to request " + okRequest.urlString(), e); + throw new IllegalStateException("Fail to request " + okRequest.url(), e); } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java index 30217f16681..c2a6babc714 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java @@ -19,7 +19,7 @@ */ package org.sonarqube.ws.client; -import com.squareup.okhttp.Response; +import okhttp3.Response; import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -39,7 +39,7 @@ class OkHttpResponse extends BaseResponse { @Override public String requestUrl() { - return okResponse.request().urlString(); + return okResponse.request().url().toString(); } @Override @@ -52,11 +52,7 @@ class OkHttpResponse extends BaseResponse { */ @Override public InputStream contentStream() { - try { - return okResponse.body().byteStream(); - } catch (IOException e) { - throw fail(e); - } + return okResponse.body().byteStream(); } /** @@ -66,11 +62,7 @@ class OkHttpResponse extends BaseResponse { */ @Override public Reader contentReader() { - try { - return okResponse.body().charStream(); - } catch (IOException e) { - throw fail(e); - } + return okResponse.body().charStream(); } @Override 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 3df29b05672..96925c0d709 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 @@ -19,10 +19,10 @@ */ package org.sonarqube.ws.client; -import com.squareup.okhttp.ConnectionSpec; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; -import com.squareup.okhttp.mockwebserver.RecordedRequest; +import okhttp3.ConnectionSpec; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; import java.io.File; import java.util.List; import javax.net.ssl.SSLSocketFactory; @@ -36,7 +36,7 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonarqube.ws.MediaTypes; -import static com.squareup.okhttp.Credentials.basic; +import static okhttp3.Credentials.basic; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -72,8 +72,8 @@ public class HttpConnectorTest { WsResponse response = underTest.call(request); // verify default timeouts on client - assertThat(underTest.okHttpClient().getConnectTimeout()).isEqualTo(HttpConnector.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS); - assertThat(underTest.okHttpClient().getReadTimeout()).isEqualTo(HttpConnector.DEFAULT_READ_TIMEOUT_MILLISECONDS); + assertThat(underTest.okHttpClient().connectTimeoutMillis()).isEqualTo(HttpConnector.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS); + assertThat(underTest.okHttpClient().readTimeoutMillis()).isEqualTo(HttpConnector.DEFAULT_READ_TIMEOUT_MILLISECONDS); // verify response assertThat(response.hasContent()).isTrue(); @@ -162,8 +162,8 @@ public class HttpConnectorTest { .connectTimeoutMilliseconds(74) .build(); - assertThat(underTest.okHttpClient().getReadTimeout()).isEqualTo(42); - assertThat(underTest.okHttpClient().getConnectTimeout()).isEqualTo(74); + assertThat(underTest.okHttpClient().readTimeoutMillis()).isEqualTo(42); + assertThat(underTest.okHttpClient().connectTimeoutMillis()).isEqualTo(74); } @Test @@ -287,7 +287,7 @@ public class HttpConnectorTest { assertTlsAndClearTextSpecifications(underTest); // enable TLS 1.0, 1.1 and 1.2 - assertThat(underTest.okHttpClient().getSslSocketFactory()).isNotNull().isInstanceOf(Tls12Java7SocketFactory.class); + assertThat(underTest.okHttpClient().sslSocketFactory()).isNotNull().isInstanceOf(Tls12Java7SocketFactory.class); } @Test @@ -296,11 +296,11 @@ public class HttpConnectorTest { underTest = HttpConnector.newBuilder().url(serverUrl).build(javaVersion); assertTlsAndClearTextSpecifications(underTest); - assertThat(underTest.okHttpClient().getSslSocketFactory()).isInstanceOf(SSLSocketFactory.getDefault().getClass()); + assertThat(underTest.okHttpClient().sslSocketFactory()).isInstanceOf(SSLSocketFactory.getDefault().getClass()); } private void assertTlsAndClearTextSpecifications(HttpConnector underTest) { - List<ConnectionSpec> connectionSpecs = underTest.okHttpClient().getConnectionSpecs(); + List<ConnectionSpec> connectionSpecs = underTest.okHttpClient().connectionSpecs(); assertThat(connectionSpecs).hasSize(2); // TLS. tlsVersions()==null means all TLS versions |