11package io .split .service ;
22
33import io .split .client .RequestDecorator ;
4+ import io .split .client .utils .SDKMetadata ;
45import io .split .client .utils .Utils ;
56import io .split .engine .common .FetchOptions ;
67import io .split .client .dtos .SplitHttpResponse ;
1617import java .io .IOException ;
1718import java .net .URI ;
1819import java .net .URISyntaxException ;
20+ import org .apache .hc .core5 .http .HttpRequest ;
1921import java .nio .charset .StandardCharsets ;
22+ import java .util .List ;
2023import java .util .Map ;
2124
2225public final class SplitHttpClientImpl implements SplitHttpClient {
26+
2327 private static final Logger _log = LoggerFactory .getLogger (SplitHttpClient .class );
2428 private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control" ;
2529 private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache" ;
30+ private static final String HEADER_API_KEY = "Authorization" ;
31+ private static final String HEADER_CLIENT_KEY = "SplitSDKClientKey" ;
32+ private static final String HEADER_CLIENT_MACHINE_NAME = "SplitSDKMachineName" ;
33+ private static final String HEADER_CLIENT_MACHINE_IP = "SplitSDKMachineIP" ;
34+ private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion" ;
35+
2636 private final CloseableHttpClient _client ;
2737 private final RequestDecorator _requestDecorator ;
38+ private final String _apikey ;
39+ private final SDKMetadata _metadata ;
2840
29- public static SplitHttpClientImpl create (
30- CloseableHttpClient client ,
31- RequestDecorator requestDecorator
32- ) throws URISyntaxException {
33- return new SplitHttpClientImpl (client , requestDecorator );
41+ public static SplitHttpClientImpl create (CloseableHttpClient client ,
42+ RequestDecorator requestDecorator ,
43+ String apikey ,
44+ SDKMetadata metadata ) throws URISyntaxException {
45+ return new SplitHttpClientImpl (client , requestDecorator , apikey , metadata );
3446 }
3547
36- private SplitHttpClientImpl
37- (CloseableHttpClient client ,
38- RequestDecorator requestDecorator ) {
48+ private SplitHttpClientImpl (CloseableHttpClient client ,
49+ RequestDecorator requestDecorator ,
50+ String apikey ,
51+ SDKMetadata metadata ) {
3952 _client = client ;
4053 _requestDecorator = requestDecorator ;
54+ _apikey = apikey ;
55+ _metadata = metadata ;
4156 }
4257
43- public SplitHttpResponse get (URI uri , FetchOptions options , Map <String , String > additionalHeaders ) {
58+ public SplitHttpResponse get (URI uri , FetchOptions options , Map <String , List < String > > additionalHeaders ) {
4459 CloseableHttpResponse response = null ;
4560
4661 try {
4762 HttpGet request = new HttpGet (uri );
63+ setBasicHeaders (request );
4864 if (additionalHeaders != null ) {
49- for (Map .Entry entry : additionalHeaders .entrySet ()) {
50- request .addHeader (entry .getKey ().toString (), entry .getValue ());
65+ for (Map .Entry <String , List <String >> entry : additionalHeaders .entrySet ()) {
66+ for (String value : entry .getValue ()) {
67+ request .addHeader (entry .getKey (), value );
68+ }
5169 }
5270 }
53- if (options .cacheControlHeadersEnabled ()) {
71+ if (options .cacheControlHeadersEnabled ()) {
5472 request .setHeader (HEADER_CACHE_CONTROL_NAME , HEADER_CACHE_CONTROL_VALUE );
5573 }
56- request = (HttpGet ) _requestDecorator .decorateHeaders (request );
74+
75+ _requestDecorator .decorateHeaders (request );
5776
5877 response = _client .execute (request );
5978
6079 if (_log .isDebugEnabled ()) {
61- _log .debug (String .format ("[%s] %s. Status code: %s" , request .getMethod (), uri .toURL (), response .getCode ()));
80+ _log .debug (String .format ("[%s] %s. Status code: %s" , request .getMethod (), uri .toURL (),
81+ response .getCode ()));
6282 }
6383
6484 String statusMessage = "" ;
6585 if (response .getCode () < HttpStatus .SC_OK || response .getCode () >= HttpStatus .SC_MULTIPLE_CHOICES ) {
66- _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode () , response .getReasonPhrase ()));
86+ _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (),
87+ response .getReasonPhrase ()));
6788 statusMessage = response .getReasonPhrase ();
6889 }
6990 return new SplitHttpResponse (response .getCode (),
70- statusMessage ,
71- EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 ),
72- response .getHeaders ());
91+ statusMessage ,
92+ EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 ),
93+ response .getHeaders ());
7394 } catch (Exception e ) {
7495 throw new IllegalStateException (String .format ("Problem in http get operation: %s" , e ), e );
7596 } finally {
7697 Utils .forceClose (response );
7798 }
7899 }
79100
80- public SplitHttpResponse post
81- (URI uri ,
82- HttpEntity entity ,
83- Map <String , String > additionalHeaders ) throws IOException {
101+ public SplitHttpResponse post (URI uri , HttpEntity entity , Map <String , List <String >> additionalHeaders )
102+ throws IOException {
103+
84104 CloseableHttpResponse response = null ;
85105 try {
86106 HttpPost request = new HttpPost (uri );
107+ setBasicHeaders (request );
87108 if (additionalHeaders != null ) {
88- for (Map .Entry entry : additionalHeaders .entrySet ()) {
89- request .addHeader (entry .getKey ().toString (), entry .getValue ());
109+ for (Map .Entry <String , List <String >> entry : additionalHeaders .entrySet ()) {
110+ for (String value : entry .getValue ()) {
111+ request .addHeader (entry .getKey (), value );
112+ }
90113 }
91114 }
92115 request .setEntity (entity );
@@ -97,7 +120,8 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, String>
97120 String statusMessage = "" ;
98121 if (response .getCode () < HttpStatus .SC_OK || response .getCode () >= HttpStatus .SC_MULTIPLE_CHOICES ) {
99122 statusMessage = response .getReasonPhrase ();
100- _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (), response .getReasonPhrase ()));
123+ _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (),
124+ response .getReasonPhrase ()));
101125 }
102126 return new SplitHttpResponse (response .getCode (), statusMessage , "" , response .getHeaders ());
103127 } catch (Exception e ) {
@@ -106,4 +130,14 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, String>
106130 Utils .forceClose (response );
107131 }
108132 }
133+
134+ private void setBasicHeaders (HttpRequest request ) {
135+ request .setHeader (HEADER_API_KEY , "Bearer " + _apikey );
136+ request .setHeader (HEADER_CLIENT_VERSION , _metadata .getSdkVersion ());
137+ request .setHeader (HEADER_CLIENT_MACHINE_IP , _metadata .getMachineIp ());
138+ request .setHeader (HEADER_CLIENT_MACHINE_NAME , _metadata .getMachineName ());
139+ request .setHeader (HEADER_CLIENT_KEY , _apikey .length () > 4
140+ ? _apikey .substring (_apikey .length () - 4 )
141+ : _apikey );
142+ }
109143}
0 commit comments