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+ request .addHeader (entry .getKey (), entry .getValue ());
5167 }
5268 }
53- if (options .cacheControlHeadersEnabled ()) {
69+ if (options .cacheControlHeadersEnabled ()) {
5470 request .setHeader (HEADER_CACHE_CONTROL_NAME , HEADER_CACHE_CONTROL_VALUE );
5571 }
56- request = (HttpGet ) _requestDecorator .decorateHeaders (request );
72+
73+ _requestDecorator .decorateHeaders (request );
5774
5875 response = _client .execute (request );
5976
6077 if (_log .isDebugEnabled ()) {
61- _log .debug (String .format ("[%s] %s. Status code: %s" , request .getMethod (), uri .toURL (), response .getCode ()));
78+ _log .debug (String .format ("[%s] %s. Status code: %s" , request .getMethod (), uri .toURL (),
79+ response .getCode ()));
6280 }
6381
6482 String statusMessage = "" ;
6583 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 ()));
84+ _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (),
85+ response .getReasonPhrase ()));
6786 statusMessage = response .getReasonPhrase ();
6887 }
6988 return new SplitHttpResponse (response .getCode (),
70- statusMessage ,
71- EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 ),
72- response .getHeaders ());
89+ statusMessage ,
90+ EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 ),
91+ response .getHeaders ());
7392 } catch (Exception e ) {
7493 throw new IllegalStateException (String .format ("Problem in http get operation: %s" , e ), e );
7594 } finally {
7695 Utils .forceClose (response );
7796 }
7897 }
7998
80- public SplitHttpResponse post
81- (URI uri ,
82- HttpEntity entity ,
83- Map <String , String > additionalHeaders ) throws IOException {
99+ public SplitHttpResponse post (URI uri , HttpEntity entity , Map <String , List <String >> additionalHeaders )
100+ throws IOException {
101+
84102 CloseableHttpResponse response = null ;
85103 try {
86104 HttpPost request = new HttpPost (uri );
105+ setBasicHeaders (request );
87106 if (additionalHeaders != null ) {
88- for (Map .Entry entry : additionalHeaders .entrySet ()) {
89- request .addHeader (entry .getKey (). toString () , entry .getValue ());
107+ for (Map .Entry < String , List < String >> entry : additionalHeaders .entrySet ()) {
108+ request .addHeader (entry .getKey (), entry .getValue ());
90109 }
91110 }
92111 request .setEntity (entity );
@@ -97,7 +116,8 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, String>
97116 String statusMessage = "" ;
98117 if (response .getCode () < HttpStatus .SC_OK || response .getCode () >= HttpStatus .SC_MULTIPLE_CHOICES ) {
99118 statusMessage = response .getReasonPhrase ();
100- _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (), response .getReasonPhrase ()));
119+ _log .warn (String .format ("Response status was: %s. Reason: %s" , response .getCode (),
120+ response .getReasonPhrase ()));
101121 }
102122 return new SplitHttpResponse (response .getCode (), statusMessage , "" , response .getHeaders ());
103123 } catch (Exception e ) {
@@ -106,4 +126,14 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, String>
106126 Utils .forceClose (response );
107127 }
108128 }
129+
130+ private void setBasicHeaders (HttpRequest request ) {
131+ request .setHeader (HEADER_API_KEY , "Bearer " + _apikey );
132+ request .setHeader (HEADER_CLIENT_VERSION , _metadata .getSdkVersion ());
133+ request .setHeader (HEADER_CLIENT_MACHINE_IP , _metadata .getMachineIp ());
134+ request .setHeader (HEADER_CLIENT_MACHINE_NAME , _metadata .getMachineName ());
135+ request .setHeader (HEADER_CLIENT_KEY , _apikey .length () > 4
136+ ? _apikey .substring (_apikey .length () - 4 )
137+ : _apikey );
138+ }
109139}
0 commit comments