From e087cc91969df3e16be5be1dc421ffb0f2be830e Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 27 Jun 2016 17:31:44 +0200 Subject: [PATCH] SONAR-7741 upgrade OK HTTP to 3.3.1 --- it/it-tests/pom.xml | 4 +- .../test/java/it/analysis/ReportDumpTest.java | 8 +-- .../test/java/it/http/HttpHeadersTest.java | 8 +-- .../it/user/OAuth2IdentityProviderTest.java | 4 +- pom.xml | 6 +- server/sonar-process/pom.xml | 2 +- .../systeminfo/SystemInfoHttpServerTest.java | 6 +- server/sonar-server/pom.xml | 2 +- .../ProcessSystemInfoClientTest.java | 4 +- .../sonar/scanner/report/ReportPublisher.java | 2 +- .../bootstrap/BatchWsClientProviderTest.java | 8 +-- sonar-ws/pom.xml | 4 +- .../sonarqube/ws/client/HttpConnector.java | 55 +++++++++++-------- .../sonarqube/ws/client/OkHttpResponse.java | 16 ++---- .../ws/client/HttpConnectorTest.java | 24 ++++---- 15 files changed, 76 insertions(+), 77 deletions(-) diff --git a/it/it-tests/pom.xml b/it/it-tests/pom.xml index 4c5deaff037..fb7418301cf 100644 --- a/it/it-tests/pom.xml +++ b/it/it-tests/pom.xml @@ -93,9 +93,9 @@ 1.8.3 - com.squareup.okhttp + com.squareup.okhttp3 mockwebserver - 2.6.0 + 3.3.1 diff --git a/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java b/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java index 1000dc18c88..2232dfba897 100644 --- a/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java +++ b/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java @@ -21,10 +21,10 @@ package it.analysis; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; -import com.squareup.okhttp.HttpUrl; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.Response; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import it.Category3Suite; import org.apache.commons.io.FileUtils; import org.junit.ClassRule; diff --git a/it/it-tests/src/test/java/it/http/HttpHeadersTest.java b/it/it-tests/src/test/java/it/http/HttpHeadersTest.java index c03ab8536cf..d578717da30 100644 --- a/it/it-tests/src/test/java/it/http/HttpHeadersTest.java +++ b/it/it-tests/src/test/java/it/http/HttpHeadersTest.java @@ -22,10 +22,10 @@ package it.http; import com.google.common.base.Throwables; import com.sonar.orchestrator.Orchestrator; -import com.squareup.okhttp.CacheControl; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.Response; +import okhttp3.CacheControl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import it.Category4Suite; import java.io.IOException; import org.junit.ClassRule; diff --git a/it/it-tests/src/test/java/it/user/OAuth2IdentityProviderTest.java b/it/it-tests/src/test/java/it/user/OAuth2IdentityProviderTest.java index 7ca114f194c..c7645ef00b0 100644 --- a/it/it-tests/src/test/java/it/user/OAuth2IdentityProviderTest.java +++ b/it/it-tests/src/test/java/it/user/OAuth2IdentityProviderTest.java @@ -20,8 +20,8 @@ package it.user; import com.sonar.orchestrator.Orchestrator; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; import it.Category4Suite; import java.net.HttpURLConnection; import org.junit.After; diff --git a/pom.xml b/pom.xml index 5d85db456fd..ee93ce5e762 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ 8.0.32 2.3.3 3.11 - 2.6.0 + 3.3.1 2.6.6 3.0.0-beta-2 @@ -953,12 +953,12 @@ ${tomcat.version} - com.squareup.okhttp + com.squareup.okhttp3 okhttp ${okhttp.version} - com.squareup.okhttp + com.squareup.okhttp3 mockwebserver ${okhttp.version} diff --git a/server/sonar-process/pom.xml b/server/sonar-process/pom.xml index 441c429fd85..3453e172047 100644 --- a/server/sonar-process/pom.xml +++ b/server/sonar-process/pom.xml @@ -72,7 +72,7 @@ test - com.squareup.okhttp + com.squareup.okhttp3 okhttp test diff --git a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoHttpServerTest.java b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoHttpServerTest.java index 31f7f48f08f..04df2a6d850 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoHttpServerTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoHttpServerTest.java @@ -19,9 +19,9 @@ */ package org.sonar.process.systeminfo; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.Response; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import java.io.IOException; import java.net.ConnectException; import java.util.Arrays; diff --git a/server/sonar-server/pom.xml b/server/sonar-server/pom.xml index 579e4f8d88b..b753ff7dd24 100644 --- a/server/sonar-server/pom.xml +++ b/server/sonar-server/pom.xml @@ -243,7 +243,7 @@ test - com.squareup.okhttp + com.squareup.okhttp3 mockwebserver test diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java index ff3016a7dfc..8020168d493 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java @@ -20,8 +20,8 @@ package org.sonar.server.platform.monitoring; import com.google.common.base.Optional; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; import java.io.File; import okio.Buffer; import org.junit.Before; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java index f5f22cd911e..e184fd36292 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java @@ -22,7 +22,7 @@ package org.sonar.scanner.report; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Throwables; import com.google.common.io.Files; -import com.squareup.okhttp.HttpUrl; +import okhttp3.HttpUrl; import java.io.File; import java.io.IOException; import java.io.InputStream; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/BatchWsClientProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/BatchWsClientProviderTest.java index 89ae9049260..8fb927c6205 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/BatchWsClientProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/BatchWsClientProviderTest.java @@ -45,9 +45,9 @@ public class BatchWsClientProviderTest { assertThat(client.baseUrl()).isEqualTo("http://localhost:9000/"); HttpConnector httpConnector = (HttpConnector) client.wsConnector(); assertThat(httpConnector.baseUrl()).isEqualTo("http://localhost:9000/"); - assertThat(httpConnector.okHttpClient().getProxy()).isNull(); - assertThat(httpConnector.okHttpClient().getConnectTimeout()).isEqualTo(5_000); - assertThat(httpConnector.okHttpClient().getReadTimeout()).isEqualTo(60_000); + assertThat(httpConnector.okHttpClient().proxy()).isNull(); + assertThat(httpConnector.okHttpClient().connectTimeoutMillis()).isEqualTo(5_000); + assertThat(httpConnector.okHttpClient().readTimeoutMillis()).isEqualTo(60_000); assertThat(httpConnector.userAgent()).isEqualTo("Maven Plugin/2.3"); } @@ -65,7 +65,7 @@ public class BatchWsClientProviderTest { assertThat(client).isNotNull(); HttpConnector httpConnector = (HttpConnector) client.wsConnector(); assertThat(httpConnector.baseUrl()).isEqualTo("https://here/sonarqube/"); - assertThat(httpConnector.okHttpClient().getProxy()).isNull(); + assertThat(httpConnector.okHttpClient().proxy()).isNull(); assertThat(httpConnector.userAgent()).isEqualTo("Maven Plugin/2.3"); } 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 @@ guava - com.squareup.okhttp + com.squareup.okhttp3 okhttp @@ -54,7 +54,7 @@ test - com.squareup.okhttp + com.squareup.okhttp3 mockwebserver test 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 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 connectionSpecs = underTest.okHttpClient().getConnectionSpecs(); + List connectionSpecs = underTest.okHttpClient().connectionSpecs(); assertThat(connectionSpecs).hasSize(2); // TLS. tlsVersions()==null means all TLS versions -- 2.39.5