|
| 1 | +package io.split.httpmodules.okhttp; |
| 2 | + |
| 3 | +import io.split.client.RequestDecorator; |
| 4 | +import io.split.client.dtos.SplitHttpResponse; |
| 5 | +import io.split.client.utils.SDKMetadata; |
| 6 | +import io.split.engine.common.FetchOptions; |
| 7 | +import io.split.service.SplitHttpClient; |
| 8 | + |
| 9 | +import okhttp3.*; |
| 10 | +import okhttp3.OkHttpClient.Builder; |
| 11 | +import okhttp3.Request.*; |
| 12 | +import okhttp3.logging.HttpLoggingInterceptor; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import split.org.apache.hc.client5.http.classic.methods.HttpGet; |
| 17 | +import split.org.apache.hc.core5.http.Header; |
| 18 | +import split.org.apache.hc.core5.http.HttpEntity; |
| 19 | +import split.org.apache.hc.core5.http.HttpRequest; |
| 20 | +import split.org.apache.hc.core5.http.io.entity.EntityUtils; |
| 21 | +import split.org.apache.hc.core5.http.message.BasicHeader; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.HttpURLConnection; |
| 25 | +import java.net.Proxy; |
| 26 | +import java.net.URI; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +public class OkHttpClientImpl implements SplitHttpClient { |
| 34 | + public final OkHttpClient httpClient; |
| 35 | + private static final Logger _log = LoggerFactory.getLogger(OkHttpClientImpl.class); |
| 36 | + private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control"; |
| 37 | + private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache"; |
| 38 | + private static final String HEADER_API_KEY = "Authorization"; |
| 39 | + private static final String HEADER_CLIENT_KEY = "SplitSDKClientKey"; |
| 40 | + private static final String HEADER_CLIENT_MACHINE_NAME = "SplitSDKMachineName"; |
| 41 | + private static final String HEADER_CLIENT_MACHINE_IP = "SplitSDKMachineIP"; |
| 42 | + private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion"; |
| 43 | + private RequestDecorator _requestDecorator; |
| 44 | + private String _apikey; |
| 45 | + private SDKMetadata _metadata; |
| 46 | + |
| 47 | + public OkHttpClientImpl(String apiToken, SDKMetadata sdkMetadata, RequestDecorator requestDecorator, |
| 48 | + Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled, |
| 49 | + int readTimeout, int connectionTimeout) throws IOException { |
| 50 | + _apikey = apiToken; |
| 51 | + _metadata = sdkMetadata; |
| 52 | + _requestDecorator = requestDecorator; |
| 53 | + |
| 54 | + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); |
| 55 | + if (debugEnabled) { |
| 56 | + logging.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| 57 | + } else { |
| 58 | + logging.setLevel(HttpLoggingInterceptor.Level.NONE); |
| 59 | + } |
| 60 | + |
| 61 | + Map<String, String> kerberosOptions = new HashMap<>(); |
| 62 | + kerberosOptions.put("com.sun.security.auth.module.Krb5LoginModule", "required"); |
| 63 | + kerberosOptions.put("refreshKrb5Config", "false"); |
| 64 | + kerberosOptions.put("doNotPrompt", "false"); |
| 65 | + kerberosOptions.put("useTicketCache", "true"); |
| 66 | + |
| 67 | + Authenticator proxyAuthenticator = getProxyAuthenticator(proxyAuthKerberosPrincipalName, kerberosOptions); |
| 68 | + |
| 69 | + httpClient = new okhttp3.OkHttpClient.Builder() |
| 70 | + .proxy(proxy) |
| 71 | + .readTimeout(readTimeout, TimeUnit.MILLISECONDS) |
| 72 | + .connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS) |
| 73 | + .addInterceptor(logging) |
| 74 | + .proxyAuthenticator(proxyAuthenticator) |
| 75 | + .build(); |
| 76 | + } |
| 77 | + |
| 78 | + public HTTPKerberosAuthInterceptor getProxyAuthenticator(String proxyKerberosPrincipalName, |
| 79 | + Map<String, String> kerberosOptions) throws IOException { |
| 80 | + return new HTTPKerberosAuthInterceptor(proxyKerberosPrincipalName, kerberosOptions); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<String>> additionalHeaders) { |
| 85 | + try { |
| 86 | + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); |
| 87 | + requestBuilder.url(uri.toString()); |
| 88 | + setBasicHeaders(requestBuilder); |
| 89 | + setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders); |
| 90 | + if (options.cacheControlHeadersEnabled()) { |
| 91 | + requestBuilder.addHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE); |
| 92 | + } |
| 93 | + |
| 94 | + Request request = requestBuilder.build(); |
| 95 | + _log.debug(String.format("Request Headers: %s", request.headers())); |
| 96 | + |
| 97 | + Response response = httpClient.newCall(request).execute(); |
| 98 | + |
| 99 | + int responseCode = response.code(); |
| 100 | + |
| 101 | + _log.debug(String.format("[GET] %s. Status code: %s", |
| 102 | + request.url().toString(), |
| 103 | + responseCode)); |
| 104 | + |
| 105 | + String statusMessage = ""; |
| 106 | + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { |
| 107 | + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, |
| 108 | + response.message())); |
| 109 | + statusMessage = response.message(); |
| 110 | + } |
| 111 | + |
| 112 | + String responseBody = response.body().string(); |
| 113 | + response.close(); |
| 114 | + |
| 115 | + return new SplitHttpResponse(responseCode, |
| 116 | + statusMessage, |
| 117 | + responseBody, |
| 118 | + getResponseHeaders(response)); |
| 119 | + } catch (Exception e) { |
| 120 | + throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public SplitHttpResponse post(URI url, HttpEntity entity, |
| 126 | + Map<String, List<String>> additionalHeaders) { |
| 127 | + try { |
| 128 | + okhttp3.Request.Builder requestBuilder = getRequestBuilder(); |
| 129 | + requestBuilder.url(url.toString()); |
| 130 | + setBasicHeaders(requestBuilder); |
| 131 | + setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders); |
| 132 | + requestBuilder.addHeader("Accept-Encoding", "gzip"); |
| 133 | + requestBuilder.addHeader("Content-Type", "application/json"); |
| 134 | + String post = EntityUtils.toString((HttpEntity) entity); |
| 135 | + RequestBody postBody = RequestBody.create(post.getBytes()); |
| 136 | + requestBuilder.post(postBody); |
| 137 | + |
| 138 | + Request request = getRequest(requestBuilder); |
| 139 | + _log.debug(String.format("Request Headers: %s", request.headers())); |
| 140 | + |
| 141 | + Response response = httpClient.newCall(request).execute(); |
| 142 | + |
| 143 | + int responseCode = response.code(); |
| 144 | + |
| 145 | + _log.debug(String.format("[GET] %s. Status code: %s", |
| 146 | + request.url().toString(), |
| 147 | + responseCode)); |
| 148 | + |
| 149 | + String statusMessage = ""; |
| 150 | + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { |
| 151 | + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, |
| 152 | + response.message())); |
| 153 | + statusMessage = response.message(); |
| 154 | + } |
| 155 | + response.close(); |
| 156 | + |
| 157 | + return new SplitHttpResponse(responseCode, statusMessage, "", getResponseHeaders(response)); |
| 158 | + } catch (Exception e) { |
| 159 | + throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + protected okhttp3.Request.Builder getRequestBuilder() { |
| 164 | + return new okhttp3.Request.Builder(); |
| 165 | + } |
| 166 | + |
| 167 | + protected Request getRequest(okhttp3.Request.Builder requestBuilder) { |
| 168 | + return requestBuilder.build(); |
| 169 | + } |
| 170 | + protected void setBasicHeaders(okhttp3.Request.Builder requestBuilder) { |
| 171 | + requestBuilder.addHeader(HEADER_API_KEY, "Bearer " + _apikey); |
| 172 | + requestBuilder.addHeader(HEADER_CLIENT_VERSION, _metadata.getSdkVersion()); |
| 173 | + requestBuilder.addHeader(HEADER_CLIENT_MACHINE_IP, _metadata.getMachineIp()); |
| 174 | + requestBuilder.addHeader(HEADER_CLIENT_MACHINE_NAME, _metadata.getMachineName()); |
| 175 | + requestBuilder.addHeader(HEADER_CLIENT_KEY, _apikey.length() > 4 |
| 176 | + ? _apikey.substring(_apikey.length() - 4) |
| 177 | + : _apikey); |
| 178 | + } |
| 179 | + |
| 180 | + protected void setAdditionalAndDecoratedHeaders(okhttp3.Request.Builder requestBuilder, Map<String, List<String>> additionalHeaders) { |
| 181 | + if (additionalHeaders != null) { |
| 182 | + for (Map.Entry<String, List<String>> entry : additionalHeaders.entrySet()) { |
| 183 | + for (String value : entry.getValue()) { |
| 184 | + requestBuilder.addHeader(entry.getKey(), value); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + HttpRequest request = new HttpGet(""); |
| 189 | + _requestDecorator.decorateHeaders(request); |
| 190 | + for (Header header : request.getHeaders()) { |
| 191 | + requestBuilder.addHeader(header.getName(), header.getValue()); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + protected Header[] getResponseHeaders(Response response) { |
| 196 | + List<BasicHeader> responseHeaders = new ArrayList<>(); |
| 197 | + Map<String, List<String>> map = response.headers().toMultimap(); |
| 198 | + for (Map.Entry<String, List<String>> entry : map.entrySet()) { |
| 199 | + if (entry.getKey() != null) { |
| 200 | + BasicHeader responseHeader = new BasicHeader(entry.getKey(), entry.getValue()); |
| 201 | + responseHeaders.add(responseHeader); |
| 202 | + } |
| 203 | + } |
| 204 | + return responseHeaders.toArray(new split.org.apache.hc.core5.http.Header[0]); |
| 205 | + } |
| 206 | + @Override |
| 207 | + public void close() throws IOException { |
| 208 | + httpClient.dispatcher().executorService().shutdown(); |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments