|
| 1 | + |
| 2 | +package com.commercetools.http.okhttp5; |
| 3 | + |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.*; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.function.Supplier; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import io.vrap.rmf.base.client.*; |
| 13 | +import io.vrap.rmf.base.client.utils.Utils; |
| 14 | + |
| 15 | +import jakarta.validation.constraints.NotNull; |
| 16 | +import okhttp3.OkHttpClient; |
| 17 | +import okio.GzipSource; |
| 18 | +import okio.Okio; |
| 19 | + |
| 20 | +public class CtOkHttp5Client extends HttpClientBase { |
| 21 | + |
| 22 | + public static final int MAX_REQUESTS = 64; |
| 23 | + private final Supplier<OkHttpClient.Builder> clientBuilder = () -> new OkHttpClient.Builder() |
| 24 | + .connectTimeout(120, TimeUnit.SECONDS) |
| 25 | + .writeTimeout(120, TimeUnit.SECONDS) |
| 26 | + .readTimeout(120, TimeUnit.SECONDS) |
| 27 | + .protocols(Collections.singletonList(okhttp3.Protocol.HTTP_1_1)) |
| 28 | + .addInterceptor(new UnzippingInterceptor()); |
| 29 | + |
| 30 | + private final OkHttpClient okHttpClient; |
| 31 | + |
| 32 | + public CtOkHttp5Client() { |
| 33 | + super(); |
| 34 | + okHttpClient = clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS)).build(); |
| 35 | + } |
| 36 | + |
| 37 | + public CtOkHttp5Client(final BuilderOptions options) { |
| 38 | + super(); |
| 39 | + okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS))) |
| 40 | + .build(); |
| 41 | + } |
| 42 | + |
| 43 | + public CtOkHttp5Client(final Supplier<OkHttpClient.Builder> builderSupplier) { |
| 44 | + super(); |
| 45 | + okHttpClient = builderSupplier.get().build(); |
| 46 | + } |
| 47 | + |
| 48 | + public CtOkHttp5Client(final int maxRequests, final int maxRequestsPerHost) { |
| 49 | + super(); |
| 50 | + okHttpClient = clientBuilder.get().dispatcher(createDispatcher(maxRequests, maxRequestsPerHost)).build(); |
| 51 | + } |
| 52 | + |
| 53 | + public CtOkHttp5Client(final int maxRequests, final int maxRequestsPerHost, final BuilderOptions options) { |
| 54 | + super(); |
| 55 | + okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(maxRequests, maxRequestsPerHost))) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + |
| 59 | + public CtOkHttp5Client(final ExecutorService executor) { |
| 60 | + super(executor); |
| 61 | + okHttpClient = clientBuilder.get().dispatcher(createDispatcher(executor, MAX_REQUESTS, MAX_REQUESTS)).build(); |
| 62 | + } |
| 63 | + |
| 64 | + public CtOkHttp5Client(final ExecutorService executor, final BuilderOptions options) { |
| 65 | + super(executor); |
| 66 | + okHttpClient = options.plus(clientBuilder.get().dispatcher(createDispatcher(MAX_REQUESTS, MAX_REQUESTS))) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + public CtOkHttp5Client(final ExecutorService executor, final int maxRequests, final int maxRequestsPerHost) { |
| 71 | + super(executor); |
| 72 | + okHttpClient = clientBuilder.get() |
| 73 | + .dispatcher(createDispatcher(executor, maxRequests, maxRequestsPerHost)) |
| 74 | + .build(); |
| 75 | + } |
| 76 | + |
| 77 | + public CtOkHttp5Client(final ExecutorService executor, final int maxRequests, final int maxRequestsPerHost, |
| 78 | + final BuilderOptions options) { |
| 79 | + super(executor); |
| 80 | + okHttpClient = options |
| 81 | + .plus(clientBuilder.get().dispatcher(createDispatcher(executor, maxRequests, maxRequestsPerHost))) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + public okhttp3.Dispatcher createDispatcher(final int maxRequests, final int maxRequestsPerHost) { |
| 86 | + final okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(); |
| 87 | + dispatcher.setMaxRequests(maxRequests); |
| 88 | + dispatcher.setMaxRequestsPerHost(maxRequestsPerHost); |
| 89 | + return dispatcher; |
| 90 | + } |
| 91 | + |
| 92 | + public okhttp3.Dispatcher createDispatcher(final ExecutorService executor, final int maxRequests, |
| 93 | + final int maxRequestsPerHost) { |
| 94 | + final okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(executor); |
| 95 | + dispatcher.setMaxRequests(maxRequests); |
| 96 | + dispatcher.setMaxRequestsPerHost(maxRequestsPerHost); |
| 97 | + return dispatcher; |
| 98 | + } |
| 99 | + |
| 100 | + private static final String CONTENT_TYPE = "Content-Type"; |
| 101 | + private static final okhttp3.MediaType JSON = okhttp3.MediaType.get("application/json; charset=utf-8"); |
| 102 | + private static final byte[] emptyBody = new byte[0]; |
| 103 | + |
| 104 | + @Override |
| 105 | + public CompletableFuture<ApiHttpResponse<byte[]>> execute(final ApiHttpRequest request) { |
| 106 | + return makeRequest(okHttpClient, toRequest(request)).thenApplyAsync(CtOkHttp5Client::toResponse, executor()); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + static ApiHttpResponse<byte[]> toResponse(final okhttp3.Response response) { |
| 111 | + final ApiHttpHeaders apiHttpHeaders = new ApiHttpHeaders(response.headers() |
| 112 | + .toMultimap() |
| 113 | + .entrySet() |
| 114 | + .stream() |
| 115 | + .flatMap(e -> e.getValue().stream().map(value -> ApiHttpHeaders.headerEntry(e.getKey(), value))) |
| 116 | + .collect(Collectors.toList())); |
| 117 | + |
| 118 | + final ApiHttpResponse<byte[]> apiHttpResponse = new ApiHttpResponse<>(response.code(), apiHttpHeaders, |
| 119 | + Optional.ofNullable(response.body()) |
| 120 | + .map(Utils.wrapToCompletionException(okhttp3.ResponseBody::bytes)) |
| 121 | + .orElse(null), |
| 122 | + response.message()); |
| 123 | + if (response.body() != null) { |
| 124 | + response.close(); |
| 125 | + } |
| 126 | + return apiHttpResponse; |
| 127 | + } |
| 128 | + |
| 129 | + private static okhttp3.Request toRequest(final ApiHttpRequest apiHttpRequest) { |
| 130 | + |
| 131 | + okhttp3.Request.Builder httpRequestBuilder = new okhttp3.Request.Builder().url(apiHttpRequest.getUrl()); |
| 132 | + |
| 133 | + //set headers |
| 134 | + for (Map.Entry<String, String> entry : apiHttpRequest.getHeaders().getHeaders()) { |
| 135 | + httpRequestBuilder = httpRequestBuilder.header(entry.getKey(), entry.getValue()); |
| 136 | + } |
| 137 | + |
| 138 | + if (apiHttpRequest.getMethod() == null) { |
| 139 | + throw new IllegalStateException("apiHttpRequest method should be non null"); |
| 140 | + } |
| 141 | + |
| 142 | + //default media type is JSON, if other media type is set as a header, use it |
| 143 | + okhttp3.MediaType mediaType = JSON; |
| 144 | + if (apiHttpRequest.getHeaders() |
| 145 | + .getHeaders() |
| 146 | + .stream() |
| 147 | + .anyMatch(s -> s.getKey().equalsIgnoreCase(CONTENT_TYPE))) { |
| 148 | + mediaType = okhttp3.MediaType |
| 149 | + .get(Objects.requireNonNull(apiHttpRequest.getHeaders().getFirst(ApiHttpHeaders.CONTENT_TYPE))); |
| 150 | + } |
| 151 | + |
| 152 | + try { |
| 153 | + final okhttp3.RequestBody body = apiHttpRequest.getBody() == null ? null |
| 154 | + : okhttp3.RequestBody.create(apiHttpRequest.getBody(), mediaType); |
| 155 | + httpRequestBuilder.method(apiHttpRequest.getMethod().name(), body); |
| 156 | + } |
| 157 | + catch (NoSuchMethodError error) { |
| 158 | + throw new IllegalStateException( |
| 159 | + "Request class is not compatible with this HTTP client implementation. Probably a wrong http client package is used. Please try \"commercetools-okhttp-client3\" instead"); |
| 160 | + } |
| 161 | + return httpRequestBuilder.build(); |
| 162 | + } |
| 163 | + |
| 164 | + private CompletableFuture<okhttp3.Response> makeRequest(final OkHttpClient client, final okhttp3.Request request) { |
| 165 | + final okhttp3.Call call = client.newCall(request); |
| 166 | + final OkHttpResponseFuture result = new OkHttpResponseFuture(); |
| 167 | + call.enqueue(result); |
| 168 | + return result.future; |
| 169 | + } |
| 170 | + |
| 171 | + private static class OkHttpResponseFuture implements okhttp3.Callback { |
| 172 | + public final CompletableFuture<okhttp3.Response> future = new CompletableFuture<>(); |
| 173 | + |
| 174 | + public OkHttpResponseFuture() { |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void onFailure(final okhttp3.Call call, final IOException e) { |
| 179 | + future.completeExceptionally(e); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void onResponse(final okhttp3.Call call, final okhttp3.Response response) throws IOException { |
| 184 | + future.complete(response); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void closeDelegate() throws IOException { |
| 190 | + okHttpClient.dispatcher().executorService().shutdown(); |
| 191 | + okHttpClient.connectionPool().evictAll(); |
| 192 | + if (okHttpClient.cache() != null) |
| 193 | + Objects.requireNonNull(okHttpClient.cache()).close(); |
| 194 | + } |
| 195 | + |
| 196 | + public static class UnzippingInterceptor implements okhttp3.Interceptor { |
| 197 | + @Override |
| 198 | + @NotNull |
| 199 | + public okhttp3.Response intercept(Chain chain) throws IOException { |
| 200 | + okhttp3.Response response = chain.proceed(chain.request()); |
| 201 | + return unzip(response); |
| 202 | + } |
| 203 | + |
| 204 | + okhttp3.Response unzip(final okhttp3.Response response) { |
| 205 | + if (!"gzip".equalsIgnoreCase(response.header("Content-Encoding"))) { |
| 206 | + return response; |
| 207 | + } |
| 208 | + |
| 209 | + if (response.body() == null) { |
| 210 | + return response; |
| 211 | + } |
| 212 | + |
| 213 | + okhttp3.Headers strippedHeaders = response.headers() |
| 214 | + .newBuilder() |
| 215 | + .removeAll("Content-Encoding") |
| 216 | + .removeAll("Content-Length") |
| 217 | + .build(); |
| 218 | + String contentType = response.header("Content-Type"); |
| 219 | + return response.newBuilder() |
| 220 | + .headers(strippedHeaders) |
| 221 | + .body(okhttp3.ResponseBody.create(Okio.buffer(new GzipSource(response.body().source())), |
| 222 | + okhttp3.MediaType.get(contentType), -1L)) |
| 223 | + .build(); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments