diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2024-05-06 16:29:44 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-05-07 20:02:48 +0000 |
commit | 69dd0c1bee34979b463330b3b6a1c85c4115d47d (patch) | |
tree | d3d4767bf8907e2787496627c5bb6dc8e7abff5d /sonar-ws/src | |
parent | 8eb62b3645655f7f0bfea3246a7e317d2f21826c (diff) | |
download | sonarqube-69dd0c1bee34979b463330b3b6a1c85c4115d47d.tar.gz sonarqube-69dd0c1bee34979b463330b3b6a1c85c4115d47d.zip |
SONAR-22194 Use Okhttp interceptor to log requests
Diffstat (limited to 'sonar-ws/src')
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java | 1 | ||||
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java | 15 |
2 files changed, 15 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 50a35824b06..e30512b5e4c 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 @@ -91,6 +91,7 @@ public class HttpConnector implements WsConnector { okHttpClientBuilder.setSSLSocketFactory(builder.sslSocketFactory); okHttpClientBuilder.setTrustManager(builder.sslTrustManager); okHttpClientBuilder.acceptGzip(builder.acceptGzip); + this.okHttpClient = okHttpClientBuilder.build(); this.noRedirectOkHttpClient = newClientWithoutRedirect(this.okHttpClient); } 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 afb04328c27..57fabf1a600 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 @@ -46,6 +46,9 @@ import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import okhttp3.logging.HttpLoggingInterceptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Arrays.asList; @@ -58,6 +61,8 @@ import static org.sonarqube.ws.WsUtils.nullToEmpty; */ public class OkHttpClientBuilder { + private static final Logger LOG = LoggerFactory.getLogger(OkHttpClientBuilder.class); + private static final String NONE = "NONE"; private static final String P11KEYSTORE = "PKCS11"; private static final String PROXY_AUTHORIZATION = "Proxy-Authorization"; @@ -204,7 +209,7 @@ public class OkHttpClientBuilder { builder.callTimeout(responseTimeoutMs, TimeUnit.MILLISECONDS); } builder.addNetworkInterceptor(this::addHeaders); - if(!acceptGzip) { + if (!acceptGzip) { builder.addNetworkInterceptor(new GzipRejectorInterceptor()); } if (proxyLogin != null) { @@ -236,9 +241,17 @@ public class OkHttpClientBuilder { SSLSocketFactory sslFactory = sslSocketFactory != null ? sslSocketFactory : systemDefaultSslSocketFactory(trustManager); builder.sslSocketFactory(sslFactory, trustManager); + builder.addInterceptor(buildLoggingInterceptor()); + return builder.build(); } + private static HttpLoggingInterceptor buildLoggingInterceptor() { + var logging = new HttpLoggingInterceptor(LOG::debug); + logging.setLevel(HttpLoggingInterceptor.Level.BASIC); + return logging; + } + private Response addHeaders(Interceptor.Chain chain) throws IOException { Request.Builder newRequest = chain.request().newBuilder(); if (userAgent != null) { |