-
Notifications
You must be signed in to change notification settings - Fork 19
DEVX-658: updating okhttp to v5 #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jenschude
merged 15 commits into
main
from
DEVX-658-Update-dependency-com.squareup.okhttp-to-v5
Dec 1, 2025
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0f49b3d
DEVX-658: updating okhttp to v5
ShipilA 3addaca
spotless: Fix code style
ct-sdks[bot] e1a826d
spotless: add commit to blame ignore revs file
ct-sdks[bot] 56ed101
Update commercetools/commercetools-okhttp-client5/src/main/java/com/c…
ShipilA 2dacf2d
Update commercetools/commercetools-okhttp-client5/src/jmh/java/com/co…
ShipilA fce60be
DEVX-658: updating okhttp to v5
ShipilA 311e632
DEVX-658: updating okhttp to v5
ShipilA e922752
DEVX-658: updating okhttp to v5
ShipilA 1276360
DEVX-658: updating okhttp to v5
ShipilA c3b382e
small fixes
jenschude b0ddf41
DEVX-658: updating license for okhttp v5
ShipilA 0295d86
Merge remote-tracking branch 'origin/DEVX-658-Update-dependency-com.s…
ShipilA 63e4276
update license checker for okhttp5
jenschude b795712
TASK: Updating license information
ct-sdks[bot] cf82884
Merge branch 'main' into DEVX-658-Update-dependency-com.squareup.okht…
jenschude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| apply plugin: "me.champeau.jmh" | ||
|
|
||
| jmh { | ||
| iterations = 5 | ||
| benchmarkMode = ['thrpt'] | ||
| threads = 25 | ||
| fork = 3 | ||
| timeOnIteration = '1s' | ||
| profilers = ['gc'] | ||
| } | ||
|
|
||
| dependencies { | ||
| api project(":rmf:rmf-java-base") | ||
| api "com.squareup.okhttp3:okhttp:5.3.0" version { | ||
| strictly '[5.0,5.99999]' | ||
| prefer "5.3.0" | ||
| } | ||
| implementation "com.squareup.okio:okio:3.14.0" | ||
|
|
||
| implementation javax.validation | ||
| } |
120 changes: 120 additions & 0 deletions
120
...mercetools-okhttp-client5/src/jmh/java/com/commercetools/http/okhttp4/UnzipBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
|
|
||
| package com.commercetools.http.okhttp4; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.zip.GZIPOutputStream; | ||
|
|
||
| import io.vrap.rmf.base.client.ApiHttpResponse; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.openjdk.jmh.annotations.*; | ||
|
|
||
| import okhttp3.*; | ||
| import okio.Okio; | ||
|
|
||
| public class UnzipBenchmark { | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class InterceptorState { | ||
| private CtOkHttp4Client.UnzippingInterceptor interceptor; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void init() { | ||
| interceptor = new CtOkHttp4Client.UnzippingInterceptor(); | ||
| printUsedMemory(); | ||
|
|
||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() { | ||
| printUsedMemory(); | ||
| } | ||
| } | ||
| @Benchmark | ||
| public void unzip(InterceptorState state) throws IOException { | ||
|
|
||
| ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
| GZIPOutputStream gzipOs = new GZIPOutputStream(os); | ||
| byte[] buffer = "Sample Text".getBytes(); | ||
| gzipOs.write(buffer, 0, buffer.length); | ||
| gzipOs.close(); | ||
| ByteArrayInputStream inputStream = new ByteArrayInputStream(os.toByteArray()); | ||
|
|
||
| Response gzipped = new Response.Builder().request(new Request.Builder().url("http://localhost").build()) | ||
| .protocol(Protocol.HTTP_1_1) | ||
| .addHeader("content-encoding", "gzip") | ||
| .addHeader("content-type", "application/json") | ||
| .code(200) | ||
| .message("Ok") | ||
| .body(ResponseBody.create(MediaType.parse("application/json"), -1L, | ||
| Okio.buffer(Okio.source(inputStream)))) | ||
| .build(); | ||
| Assertions.assertThat(gzipped.body().source().isOpen()).isTrue(); | ||
|
|
||
| Response unzipped = state.interceptor.unzip(gzipped); | ||
|
|
||
| Assertions.assertThat(gzipped.body().source().isOpen()).isTrue(); | ||
| Assertions.assertThat(unzipped.body().source().isOpen()).isTrue(); | ||
| Assertions.assertThat(inputStream.available()).isEqualTo(31); | ||
|
|
||
| ApiHttpResponse<byte[]> response = CtOkHttp4Client.toResponse(unzipped); | ||
|
|
||
| Assertions.assertThat(gzipped.body().source().isOpen()).isFalse(); | ||
| Assertions.assertThat(unzipped.body().source().isOpen()).isFalse(); | ||
| Assertions.assertThat(inputStream.available()).isEqualTo(0); | ||
|
|
||
| String text = new String(response.getBody(), StandardCharsets.UTF_8); | ||
| Assertions.assertThat(text).isEqualTo("Sample Text"); | ||
| } | ||
|
|
||
| public static void printUsedMemory() { | ||
| long _usedMem; | ||
| long _total; | ||
| long _total2; | ||
| long _count = -1; | ||
| System.out.flush(); | ||
| // loop to get a stable reading, since memory may be resized between the method calls | ||
| do { | ||
| _count++; | ||
| _total = Runtime.getRuntime().totalMemory(); | ||
| try { | ||
| Thread.sleep(12); | ||
| } | ||
| catch (Exception ignore) { | ||
| } | ||
| long _free = Runtime.getRuntime().freeMemory(); | ||
| _total2 = Runtime.getRuntime().totalMemory(); | ||
| _usedMem = _total - _free; | ||
| } while (_total != _total2); | ||
| System.out.println("before GC: used=" + _usedMem + ", loopCount=" + _count + ", total=" + _total); | ||
| try { | ||
| Runtime.getRuntime().gc(); | ||
| Thread.sleep(55); | ||
| Runtime.getRuntime().gc(); | ||
| Thread.sleep(55); | ||
| Runtime.getRuntime().gc(); | ||
| Thread.sleep(55); | ||
| Runtime.getRuntime().gc(); | ||
| Thread.sleep(55); | ||
| } | ||
| catch (Exception ignore) { | ||
| } | ||
| // loop to get a stable reading, since memory may be resized between the method calls | ||
| do { | ||
| _count++; | ||
| _total = Runtime.getRuntime().totalMemory(); | ||
| try { | ||
| Thread.sleep(12); | ||
| } | ||
| catch (Exception ignore) { | ||
| } | ||
| long _free = Runtime.getRuntime().freeMemory(); | ||
| _total2 = Runtime.getRuntime().totalMemory(); | ||
| _usedMem = _total - _free; | ||
| } while (_total != _total2); | ||
| System.out.println("after GC: used=" + _usedMem + ", loopCount=" + _count + ", total=" + _total); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
...ercetools-okhttp-client5/src/main/java/com/commercetools/http/okhttp4/BuilderOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
|
|
||
| package com.commercetools.http.okhttp4; | ||
|
|
||
| import okhttp3.OkHttpClient; | ||
|
|
||
| @FunctionalInterface | ||
| public interface BuilderOptions { | ||
| OkHttpClient.Builder plus(OkHttpClient.Builder builder); | ||
| } |
226 changes: 226 additions & 0 deletions
226
...rcetools-okhttp-client5/src/main/java/com/commercetools/http/okhttp4/CtOkHttp5Client.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
|
|
||
| package com.commercetools.http.okhttp4; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.*; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import io.vrap.rmf.base.client.*; | ||
| import io.vrap.rmf.base.client.utils.Utils; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import okhttp3.OkHttpClient; | ||
| import okio.GzipSource; | ||
| import okio.Okio; | ||
|
|
||
| public class CtOkHttp4Client extends HttpClientBase { | ||
ShipilA marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static final int MAX_REQUESTS = 64; | ||
| private final Supplier<OkHttpClient.Builder> clientBuilder = () -> new OkHttpClient.Builder() | ||
| .connectTimeout(120, TimeUnit.SECONDS) | ||
| .writeTimeout(120, TimeUnit.SECONDS) | ||
| .readTimeout(120, TimeUnit.SECONDS) | ||
| .protocols(Collections.singletonList(okhttp3.Protocol.HTTP_1_1)) | ||
| .addInterceptor(new UnzippingInterceptor()); | ||
|
|
||
| private final OkHttpClient okHttpClient; | ||
|
|
||
| public CtOkHttp4Client() { | ||
| super(); | ||
| okHttpClient = clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS)).build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final BuilderOptions options) { | ||
| super(); | ||
| okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS))) | ||
| .build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final Supplier<OkHttpClient.Builder> builderSupplier) { | ||
| super(); | ||
| okHttpClient = builderSupplier.get().build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final int maxRequests, final int maxRequestsPerHost) { | ||
| super(); | ||
| okHttpClient = clientBuilder.get().dispatcher(createDispatcher(maxRequests, maxRequestsPerHost)).build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final int maxRequests, final int maxRequestsPerHost, final BuilderOptions options) { | ||
| super(); | ||
| okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(maxRequests, maxRequestsPerHost))) | ||
| .build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final ExecutorService executor) { | ||
| super(executor); | ||
| okHttpClient = clientBuilder.get().dispatcher(createDispatcher(executor, MAX_REQUESTS, MAX_REQUESTS)).build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final ExecutorService executor, final BuilderOptions options) { | ||
| super(executor); | ||
| okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS))) | ||
| .build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final ExecutorService executor, final int maxRequests, final int maxRequestsPerHost) { | ||
| super(executor); | ||
| okHttpClient = clientBuilder.get() | ||
| .dispatcher(createDispatcher(executor, maxRequests, maxRequestsPerHost)) | ||
| .build(); | ||
| } | ||
|
|
||
| public CtOkHttp4Client(final ExecutorService executor, final int maxRequests, final int maxRequestsPerHost, | ||
| final BuilderOptions options) { | ||
| super(executor); | ||
| okHttpClient = options | ||
| .plus(clientBuilder.get().dispatcher(createDispatcher(executor, maxRequests, maxRequestsPerHost))) | ||
| .build(); | ||
| } | ||
|
|
||
| public okhttp3.Dispatcher createDispatcher(final int maxRequests, final int maxRequestsPerHost) { | ||
| final okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(); | ||
| dispatcher.setMaxRequests(maxRequests); | ||
| dispatcher.setMaxRequestsPerHost(maxRequestsPerHost); | ||
| return dispatcher; | ||
| } | ||
|
|
||
| public okhttp3.Dispatcher createDispatcher(final ExecutorService executor, final int maxRequests, | ||
| final int maxRequestsPerHost) { | ||
| final okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(executor); | ||
| dispatcher.setMaxRequests(maxRequests); | ||
| dispatcher.setMaxRequestsPerHost(maxRequestsPerHost); | ||
| return dispatcher; | ||
| } | ||
|
|
||
| private static final String CONTENT_TYPE = "Content-Type"; | ||
| private static final okhttp3.MediaType JSON = okhttp3.MediaType.get("application/json; charset=utf-8"); | ||
| private static final byte[] emptyBody = new byte[0]; | ||
|
|
||
| @Override | ||
| public CompletableFuture<ApiHttpResponse<byte[]>> execute(final ApiHttpRequest request) { | ||
| return makeRequest(okHttpClient, toRequest(request)).thenApplyAsync(CtOkHttp4Client::toResponse, executor()); | ||
|
|
||
| } | ||
|
|
||
| static ApiHttpResponse<byte[]> toResponse(final okhttp3.Response response) { | ||
| final ApiHttpHeaders apiHttpHeaders = new ApiHttpHeaders(response.headers() | ||
| .toMultimap() | ||
| .entrySet() | ||
| .stream() | ||
| .flatMap(e -> e.getValue().stream().map(value -> ApiHttpHeaders.headerEntry(e.getKey(), value))) | ||
| .collect(Collectors.toList())); | ||
|
|
||
| final ApiHttpResponse<byte[]> apiHttpResponse = new ApiHttpResponse<>(response.code(), apiHttpHeaders, | ||
| Optional.ofNullable(response.body()) | ||
| .map(Utils.wrapToCompletionException(okhttp3.ResponseBody::bytes)) | ||
| .orElse(null), | ||
| response.message()); | ||
| if (response.body() != null) { | ||
| response.close(); | ||
| } | ||
| return apiHttpResponse; | ||
| } | ||
|
|
||
| private static okhttp3.Request toRequest(final ApiHttpRequest apiHttpRequest) { | ||
|
|
||
| okhttp3.Request.Builder httpRequestBuilder = new okhttp3.Request.Builder().url(apiHttpRequest.getUrl()); | ||
|
|
||
| //set headers | ||
| for (Map.Entry<String, String> entry : apiHttpRequest.getHeaders().getHeaders()) { | ||
| httpRequestBuilder = httpRequestBuilder.header(entry.getKey(), entry.getValue()); | ||
| } | ||
|
|
||
| if (apiHttpRequest.getMethod() == null) { | ||
| throw new IllegalStateException("apiHttpRequest method should be non null"); | ||
| } | ||
|
|
||
| //default media type is JSON, if other media type is set as a header, use it | ||
| okhttp3.MediaType mediaType = JSON; | ||
| if (apiHttpRequest.getHeaders() | ||
| .getHeaders() | ||
| .stream() | ||
| .anyMatch(s -> s.getKey().equalsIgnoreCase(CONTENT_TYPE))) { | ||
| mediaType = okhttp3.MediaType | ||
| .get(Objects.requireNonNull(apiHttpRequest.getHeaders().getFirst(ApiHttpHeaders.CONTENT_TYPE))); | ||
| } | ||
|
|
||
| try { | ||
| final okhttp3.RequestBody body = apiHttpRequest.getBody() == null ? null | ||
| : okhttp3.RequestBody.create(apiHttpRequest.getBody(), mediaType); | ||
| httpRequestBuilder.method(apiHttpRequest.getMethod().name(), body); | ||
| } | ||
| catch (NoSuchMethodError error) { | ||
| throw new IllegalStateException( | ||
| "Request class is not compatible with this HTTP client implementation. Probably a wrong http client package is used. Please try \"commercetools-okhttp-client3\" instead"); | ||
| } | ||
| return httpRequestBuilder.build(); | ||
| } | ||
|
|
||
| private CompletableFuture<okhttp3.Response> makeRequest(final OkHttpClient client, final okhttp3.Request request) { | ||
| final okhttp3.Call call = client.newCall(request); | ||
| final OkHttpResponseFuture result = new OkHttpResponseFuture(); | ||
| call.enqueue(result); | ||
| return result.future; | ||
| } | ||
|
|
||
| private static class OkHttpResponseFuture implements okhttp3.Callback { | ||
| public final CompletableFuture<okhttp3.Response> future = new CompletableFuture<>(); | ||
|
|
||
| public OkHttpResponseFuture() { | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(final okhttp3.Call call, final IOException e) { | ||
| future.completeExceptionally(e); | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(final okhttp3.Call call, final okhttp3.Response response) throws IOException { | ||
| future.complete(response); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void closeDelegate() throws IOException { | ||
| okHttpClient.dispatcher().executorService().shutdown(); | ||
| okHttpClient.connectionPool().evictAll(); | ||
| if (okHttpClient.cache() != null) | ||
| Objects.requireNonNull(okHttpClient.cache()).close(); | ||
| } | ||
|
|
||
| public static class UnzippingInterceptor implements okhttp3.Interceptor { | ||
| @Override | ||
| @NotNull | ||
| public okhttp3.Response intercept(Chain chain) throws IOException { | ||
| okhttp3.Response response = chain.proceed(chain.request()); | ||
| return unzip(response); | ||
| } | ||
|
|
||
| okhttp3.Response unzip(final okhttp3.Response response) { | ||
| if (!"gzip".equalsIgnoreCase(response.header("Content-Encoding"))) { | ||
| return response; | ||
| } | ||
|
|
||
| if (response.body() == null) { | ||
| return response; | ||
| } | ||
|
|
||
| okhttp3.Headers strippedHeaders = response.headers() | ||
| .newBuilder() | ||
| .removeAll("Content-Encoding") | ||
| .removeAll("Content-Length") | ||
| .build(); | ||
| String contentType = response.header("Content-Type"); | ||
| return response.newBuilder() | ||
| .headers(strippedHeaders) | ||
| .body(okhttp3.ResponseBody.create(Okio.buffer(new GzipSource(response.body().source())), | ||
| okhttp3.MediaType.get(contentType), -1L)) | ||
| .build(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.