Skip to content

Commit 3a76a9c

Browse files
committed
Update Okhttp to 4.12 and use logging interceptor
1 parent 7d31283 commit 3a76a9c

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

lib/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
<artifactId>sonar-scanner-java-library</artifactId>
1111
<name>SonarScanner Java Library</name>
1212

13+
<properties>
14+
<slf4j.version>2.0.13</slf4j.version>
15+
</properties>
16+
17+
1318
<dependencies>
1419
<dependency>
1520
<groupId>org.slf4j</groupId>
1621
<artifactId>slf4j-api</artifactId>
17-
<version>2.0.13</version>
22+
<version>${slf4j.version}</version>
1823
</dependency>
1924
<dependency>
2025
<groupId>com.squareup.okhttp3</groupId>
@@ -24,6 +29,10 @@
2429
<groupId>com.squareup.okhttp3</groupId>
2530
<artifactId>okhttp-urlconnection</artifactId>
2631
</dependency>
32+
<dependency>
33+
<groupId>com.squareup.okhttp3</groupId>
34+
<artifactId>logging-interceptor</artifactId>
35+
</dependency>
2736
<dependency>
2837
<groupId>com.google.code.gson</groupId>
2938
<artifactId>gson</artifactId>

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/OkHttpClientFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
import okhttp3.Credentials;
3636
import okhttp3.JavaNetCookieJar;
3737
import okhttp3.OkHttpClient;
38+
import okhttp3.logging.HttpLoggingInterceptor;
39+
import org.jetbrains.annotations.NotNull;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
3842
import org.sonarsource.scanner.lib.ScannerProperties;
3943
import org.sonarsource.scanner.lib.internal.http.ssl.CertificateStore;
4044
import org.sonarsource.scanner.lib.internal.http.ssl.SslConfig;
@@ -59,6 +63,8 @@
5963

6064
public class OkHttpClientFactory {
6165

66+
private static final Logger LOG = LoggerFactory.getLogger(OkHttpClientFactory.class);
67+
6268
static final CookieManager COOKIE_MANAGER;
6369
private static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
6470
// use the same cookie jar for all instances
@@ -127,6 +133,10 @@ static OkHttpClient create(Map<String, String> bootstrapProperties, Path sonarUs
127133
});
128134
}
129135

136+
var logging = new HttpLoggingInterceptor(LOG::debug);
137+
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
138+
okHttpClientBuilder.addInterceptor(logging);
139+
130140
return okHttpClientBuilder.build();
131141
}
132142

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/ServerConnection.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ private String callApi(String url) throws IOException {
140140
if (httpClient == null) {
141141
throw new IllegalStateException("ServerConnection must be initialized");
142142
}
143-
LOG.debug("Call API: {}}", url);
144143
try (ResponseBody responseBody = callUrl(url, true, null)) {
145144
return responseBody.string();
146145
}
@@ -152,31 +151,30 @@ private String callApi(String url) throws IOException {
152151
* @param url the URL to call
153152
* @param authentication if true, the request will be authenticated with the token
154153
* @param acceptHeader the value of the Accept header
155-
* @throws IOException if connectivity error/timeout (network)
156154
* @throws IllegalStateException if HTTP code is different than 2xx
157155
*/
158-
private ResponseBody callUrl(String url, boolean authentication, @Nullable String acceptHeader) throws IOException {
156+
private ResponseBody callUrl(String url, boolean authentication, @Nullable String acceptHeader) {
157+
var requestBuilder = new Request.Builder()
158+
.get()
159+
.url(url)
160+
.addHeader("User-Agent", userAgent);
161+
if (authentication && credentials != null) {
162+
requestBuilder.header("Authorization", credentials);
163+
}
164+
if (acceptHeader != null) {
165+
requestBuilder.header("Accept", acceptHeader);
166+
}
167+
Request request = requestBuilder.build();
168+
Response response;
159169
try {
160-
var requestBuilder = new Request.Builder()
161-
.get()
162-
.url(url)
163-
.addHeader("User-Agent", userAgent);
164-
if (authentication && credentials != null) {
165-
requestBuilder.header("Authorization", credentials);
166-
}
167-
if (acceptHeader != null) {
168-
requestBuilder.header("Accept", acceptHeader);
169-
}
170-
Request request = requestBuilder.build();
171-
Response response = httpClient.newCall(request).execute();
172-
if (!response.isSuccessful()) {
173-
response.close();
174-
throw new IllegalStateException(format("Error status returned by url [%s]: %s", response.request().url(), response.code()));
175-
}
176-
return response.body();
170+
response = httpClient.newCall(request).execute();
177171
} catch (Exception e) {
178-
LOG.error("Call to URL [{}] failed", url);
179-
throw e;
172+
throw new IllegalStateException(format("Call to URL [%s] failed", url), e);
173+
}
174+
if (!response.isSuccessful()) {
175+
response.close();
176+
throw new IllegalStateException(format("Error status returned by url [%s]: %s", response.request().url(), response.code()));
180177
}
178+
return response.body();
181179
}
182180
}

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
<!-- used for deployment to SonarSource Artifactory -->
5555
<gitRepositoryName>sonar-scanner-java-library</gitRepositoryName>
56-
<okhttp.version>4.10.0</okhttp.version>
56+
<okhttp.version>4.12.0</okhttp.version>
5757
<mockito.version>3.12.4</mockito.version>
5858
</properties>
5959

@@ -84,6 +84,11 @@
8484
<artifactId>okhttp-urlconnection</artifactId>
8585
<version>${okhttp.version}</version>
8686
</dependency>
87+
<dependency>
88+
<groupId>com.squareup.okhttp3</groupId>
89+
<artifactId>logging-interceptor</artifactId>
90+
<version>${okhttp.version}</version>
91+
</dependency>
8792
<dependency>
8893
<groupId>com.google.code.gson</groupId>
8994
<artifactId>gson</artifactId>

0 commit comments

Comments
 (0)