|
| 1 | +package io.split.service; |
| 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 | + |
| 8 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 9 | +import org.apache.hc.core5.http.Header; |
| 10 | +import org.apache.hc.core5.http.HttpEntity; |
| 11 | +import org.apache.hc.core5.http.HttpRequest; |
| 12 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 13 | +import org.apache.hc.core5.http.message.BasicHeader; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.io.InputStreamReader; |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.HttpURLConnection; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +public class SplitHttpClientKerberosImpl implements SplitHttpClient { |
| 29 | + |
| 30 | + private static final Logger _log = LoggerFactory.getLogger(SplitHttpClientKerberosImpl.class); |
| 31 | + private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control"; |
| 32 | + private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache"; |
| 33 | + private static final String HEADER_API_KEY = "Authorization"; |
| 34 | + private static final String HEADER_CLIENT_KEY = "SplitSDKClientKey"; |
| 35 | + private static final String HEADER_CLIENT_MACHINE_NAME = "SplitSDKMachineName"; |
| 36 | + private static final String HEADER_CLIENT_MACHINE_IP = "SplitSDKMachineIP"; |
| 37 | + private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion"; |
| 38 | + |
| 39 | + private final RequestDecorator _requestDecorator; |
| 40 | + private final String _apikey; |
| 41 | + private final SDKMetadata _metadata; |
| 42 | + |
| 43 | + public static SplitHttpClientKerberosImpl create(RequestDecorator requestDecorator, |
| 44 | + String apikey, |
| 45 | + SDKMetadata metadata) { |
| 46 | + return new SplitHttpClientKerberosImpl(requestDecorator, apikey, metadata); |
| 47 | + } |
| 48 | + |
| 49 | + SplitHttpClientKerberosImpl(RequestDecorator requestDecorator, |
| 50 | + String apikey, |
| 51 | + SDKMetadata metadata) { |
| 52 | + _requestDecorator = requestDecorator; |
| 53 | + _apikey = apikey; |
| 54 | + _metadata = metadata; |
| 55 | + } |
| 56 | + |
| 57 | + public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<String>> additionalHeaders) { |
| 58 | + HttpURLConnection getHttpURLConnection = null; |
| 59 | + try { |
| 60 | + getHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection(); |
| 61 | + return doGet(getHttpURLConnection, options, additionalHeaders); |
| 62 | + } catch (Exception e) { |
| 63 | + throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e); |
| 64 | + } finally { |
| 65 | + try { |
| 66 | + if (getHttpURLConnection != null) { |
| 67 | + getHttpURLConnection.disconnect(); |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + _log.error(String.format("Could not close HTTP URL Connection: %s", e), e); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + public SplitHttpResponse doGet(HttpURLConnection getHttpURLConnection, FetchOptions options, Map<String, List<String>> additionalHeaders) { |
| 75 | + try { |
| 76 | + getHttpURLConnection.setRequestMethod("GET"); |
| 77 | + setBasicHeaders(getHttpURLConnection); |
| 78 | + setAdditionalAndDecoratedHeaders(getHttpURLConnection, additionalHeaders); |
| 79 | + |
| 80 | + if (options.cacheControlHeadersEnabled()) { |
| 81 | + getHttpURLConnection.setRequestProperty(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE); |
| 82 | + } |
| 83 | + |
| 84 | + _log.debug(String.format("Request Headers: %s", getHttpURLConnection.getRequestProperties())); |
| 85 | + |
| 86 | + int responseCode = getHttpURLConnection.getResponseCode(); |
| 87 | + |
| 88 | + if (_log.isDebugEnabled()) { |
| 89 | + _log.debug(String.format("[%s] %s. Status code: %s", |
| 90 | + getHttpURLConnection.getRequestMethod(), |
| 91 | + getHttpURLConnection.getURL().toString(), |
| 92 | + responseCode)); |
| 93 | + } |
| 94 | + |
| 95 | + String statusMessage = ""; |
| 96 | + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { |
| 97 | + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, |
| 98 | + getHttpURLConnection.getResponseMessage())); |
| 99 | + statusMessage = getHttpURLConnection.getResponseMessage(); |
| 100 | + } |
| 101 | + |
| 102 | + InputStreamReader inputStreamReader = new InputStreamReader(getHttpURLConnection.getInputStream()); |
| 103 | + BufferedReader br = new BufferedReader(inputStreamReader); |
| 104 | + String strCurrentLine; |
| 105 | + StringBuilder bld = new StringBuilder(); |
| 106 | + while ((strCurrentLine = br.readLine()) != null) { |
| 107 | + bld.append(strCurrentLine); |
| 108 | + } |
| 109 | + String responseBody = bld.toString(); |
| 110 | + inputStreamReader.close(); |
| 111 | + return new SplitHttpResponse(responseCode, |
| 112 | + statusMessage, |
| 113 | + responseBody, |
| 114 | + getResponseHeaders(getHttpURLConnection)); |
| 115 | + } catch (Exception e) { |
| 116 | + throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<String, List<String>> additionalHeaders) throws IOException { |
| 121 | + HttpURLConnection postHttpURLConnection = null; |
| 122 | + try { |
| 123 | + postHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection(); |
| 124 | + return doPost(postHttpURLConnection, entity, additionalHeaders); |
| 125 | + } catch (Exception e) { |
| 126 | + throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e); |
| 127 | + } finally { |
| 128 | + try { |
| 129 | + if (postHttpURLConnection != null) { |
| 130 | + postHttpURLConnection.disconnect(); |
| 131 | + } |
| 132 | + } catch (Exception e) { |
| 133 | + _log.error(String.format("Could not close URL Connection: %s", e), e); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public SplitHttpResponse doPost(HttpURLConnection postHttpURLConnection, |
| 139 | + HttpEntity entity, |
| 140 | + Map<String, List<String>> additionalHeaders) { |
| 141 | + try { |
| 142 | + postHttpURLConnection.setRequestMethod("POST"); |
| 143 | + setBasicHeaders(postHttpURLConnection); |
| 144 | + setAdditionalAndDecoratedHeaders(postHttpURLConnection, additionalHeaders); |
| 145 | + |
| 146 | + postHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip"); |
| 147 | + postHttpURLConnection.setRequestProperty("Content-Type", "application/json"); |
| 148 | + _log.debug(String.format("Request Headers: %s", postHttpURLConnection.getRequestProperties())); |
| 149 | + |
| 150 | + postHttpURLConnection.setDoOutput(true); |
| 151 | + String postBody = EntityUtils.toString(entity); |
| 152 | + OutputStream os = postHttpURLConnection.getOutputStream(); |
| 153 | + os.write(postBody.getBytes(StandardCharsets.UTF_8)); |
| 154 | + os.flush(); |
| 155 | + os.close(); |
| 156 | + _log.debug(String.format("Posting: %s", postBody)); |
| 157 | + |
| 158 | + int responseCode = postHttpURLConnection.getResponseCode(); |
| 159 | + String statusMessage = ""; |
| 160 | + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { |
| 161 | + statusMessage = postHttpURLConnection.getResponseMessage(); |
| 162 | + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, |
| 163 | + statusMessage)); |
| 164 | + } |
| 165 | + return new SplitHttpResponse(responseCode, statusMessage, "", getResponseHeaders(postHttpURLConnection)); |
| 166 | + } catch (Exception e) { |
| 167 | + throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private void setBasicHeaders(HttpURLConnection urlConnection) { |
| 172 | + urlConnection.setRequestProperty(HEADER_API_KEY, "Bearer " + _apikey); |
| 173 | + urlConnection.setRequestProperty(HEADER_CLIENT_VERSION, _metadata.getSdkVersion()); |
| 174 | + urlConnection.setRequestProperty(HEADER_CLIENT_MACHINE_IP, _metadata.getMachineIp()); |
| 175 | + urlConnection.setRequestProperty(HEADER_CLIENT_MACHINE_NAME, _metadata.getMachineName()); |
| 176 | + urlConnection.setRequestProperty(HEADER_CLIENT_KEY, _apikey.length() > 4 |
| 177 | + ? _apikey.substring(_apikey.length() - 4) |
| 178 | + : _apikey); |
| 179 | + } |
| 180 | + |
| 181 | + private void setAdditionalAndDecoratedHeaders(HttpURLConnection urlConnection, Map<String, List<String>> additionalHeaders) { |
| 182 | + if (additionalHeaders != null) { |
| 183 | + for (Map.Entry<String, List<String>> entry : additionalHeaders.entrySet()) { |
| 184 | + for (String value : entry.getValue()) { |
| 185 | + urlConnection.setRequestProperty(entry.getKey(), value); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + HttpRequest request = new HttpGet(""); |
| 190 | + _requestDecorator.decorateHeaders(request); |
| 191 | + for (Header header : request.getHeaders()) { |
| 192 | + urlConnection.setRequestProperty(header.getName(), header.getValue()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private Header[] getResponseHeaders(HttpURLConnection urlConnection) { |
| 197 | + List<BasicHeader> responseHeaders = new ArrayList<>(); |
| 198 | + Map<String, List<String>> map = urlConnection.getHeaderFields(); |
| 199 | + for (Map.Entry<String, List<String>> entry : map.entrySet()) { |
| 200 | + if (entry.getKey() != null) { |
| 201 | + BasicHeader responseHeader = new BasicHeader(entry.getKey(), entry.getValue()); |
| 202 | + responseHeaders.add(responseHeader); |
| 203 | + } |
| 204 | + } |
| 205 | + return responseHeaders.toArray(new Header[0]); |
| 206 | + } |
| 207 | + @Override |
| 208 | + public void close() throws IOException { |
| 209 | + // Added for compatibility with HttpSplitClient, no action needed as URLConnection objects are closed. |
| 210 | + } |
| 211 | +} |
0 commit comments