Skip to content

Commit 077accd

Browse files
author
Bilal Al
committed
added enum for AuthScheme
1 parent c1933cb commit 077accd

File tree

7 files changed

+33
-50
lines changed

7 files changed

+33
-50
lines changed

client/src/main/java/io/split/client/SplitClientConfig.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.split.integrations.IntegrationsConfig;
77
import io.split.storages.enums.OperationMode;
88
import io.split.storages.enums.StorageMode;
9+
import io.split.service.HttpAuthScheme;
910
import org.apache.hc.core5.http.HttpHost;
1011
import pluggable.CustomStorageWrapper;
1112

@@ -92,7 +93,7 @@ public class SplitClientConfig {
9293
private final HashSet<String> _flagSetsFilter;
9394
private final int _invalidSets;
9495
private final CustomHeaderDecorator _customHeaderDecorator;
95-
private final String _authScheme;
96+
private final HttpAuthScheme _authScheme;
9697

9798

9899
public static Builder builder() {
@@ -151,7 +152,7 @@ private SplitClientConfig(String endpoint,
151152
HashSet<String> flagSetsFilter,
152153
int invalidSets,
153154
CustomHeaderDecorator customHeaderDecorator,
154-
String authScheme) {
155+
HttpAuthScheme authScheme) {
155156
_endpoint = endpoint;
156157
_eventsEndpoint = eventsEndpoint;
157158
_featuresRefreshRate = pollForFeatureChangesEveryNSeconds;
@@ -412,7 +413,7 @@ public int getInvalidSets() {
412413
public CustomHeaderDecorator customHeaderDecorator() {
413414
return _customHeaderDecorator;
414415
}
415-
public String authScheme() {
416+
public HttpAuthScheme authScheme() {
416417
return _authScheme;
417418
}
418419

@@ -473,7 +474,7 @@ public static final class Builder {
473474
private HashSet<String> _flagSetsFilter = new HashSet<>();
474475
private int _invalidSetsCount = 0;
475476
private CustomHeaderDecorator _customHeaderDecorator = null;
476-
private String _authScheme = null;
477+
private HttpAuthScheme _authScheme = null;
477478

478479
public Builder() {
479480
}
@@ -974,7 +975,7 @@ public Builder customHeaderDecorator(CustomHeaderDecorator customHeaderDecorator
974975
* @param authScheme
975976
* @return this builder
976977
*/
977-
public Builder authScheme(String authScheme) {
978+
public Builder authScheme(HttpAuthScheme authScheme) {
978979
_authScheme = authScheme;
979980
return this;
980981
}
@@ -1087,13 +1088,6 @@ public SplitClientConfig build() {
10871088
_storageMode = StorageMode.PLUGGABLE;
10881089
}
10891090

1090-
if(_authScheme != null) {
1091-
if (!_authScheme.toLowerCase(Locale.ROOT).equals("kerberos")) {
1092-
throw new IllegalArgumentException("authScheme must be either null or `kerberos`.");
1093-
}
1094-
_authScheme = "kerberos";
1095-
}
1096-
10971091
return new SplitClientConfig(
10981092
_endpoint,
10991093
_eventsEndpoint,

client/src/main/java/io/split/client/SplitFactoryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import io.split.engine.segments.SegmentChangeFetcher;
5858
import io.split.engine.segments.SegmentSynchronizationTaskImp;
5959
import io.split.integrations.IntegrationsConfig;
60+
import io.split.service.HttpAuthScheme;
6061
import io.split.service.SplitHttpClient;
6162
import io.split.service.SplitHttpClientImpl;
6263
import io.split.service.SplitHttpClientKerberosImpl;
@@ -526,7 +527,7 @@ private static SplitHttpClient buildSplitHttpClient(String apiToken, SplitClient
526527
httpClientbuilder = setupProxy(httpClientbuilder, config);
527528
}
528529

529-
if (config.authScheme() != null) {
530+
if (config.authScheme() == HttpAuthScheme.KERBEROS) {
530531
return SplitHttpClientKerberosImpl.create(
531532
requestDecorator,
532533
apiToken,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.split.service;
2+
3+
public enum HttpAuthScheme {
4+
KERBEROS
5+
}

client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class SplitHttpClientKerberosImpl implements SplitHttpClient {
2525

26-
private static final Logger _log = LoggerFactory.getLogger(SplitHttpClient.class);
26+
private static final Logger _log = LoggerFactory.getLogger(SplitHttpClientKerberosImpl.class);
2727
private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control";
2828
private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache";
2929
private static final String HEADER_API_KEY = "Authorization";
@@ -54,7 +54,7 @@ public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map<Str
5454
HttpURLConnection getHttpURLConnection = null;
5555
try {
5656
getHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection();
57-
return _get(getHttpURLConnection, options, additionalHeaders);
57+
return doGet(getHttpURLConnection, options, additionalHeaders);
5858
} catch (Exception e) {
5959
throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e);
6060
} finally {
@@ -67,11 +67,10 @@ public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map<Str
6767
}
6868
}
6969
}
70-
public SplitHttpResponse _get(HttpURLConnection getHttpURLConnection, FetchOptions options, Map<String, List<String>> additionalHeaders) {
70+
public SplitHttpResponse doGet(HttpURLConnection getHttpURLConnection, FetchOptions options, Map<String, List<String>> additionalHeaders) {
7171
InputStreamReader inputStreamReader = null;
7272
try {
7373
getHttpURLConnection.setRequestMethod("GET");
74-
7574
setBasicHeaders(getHttpURLConnection);
7675
setAdditionalAndDecoratedHeaders(getHttpURLConnection, additionalHeaders);
7776

@@ -125,7 +124,7 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<Strin
125124
HttpURLConnection postHttpURLConnection = null;
126125
try {
127126
postHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection();
128-
return _post(postHttpURLConnection, entity, additionalHeaders);
127+
return doPost(postHttpURLConnection, entity, additionalHeaders);
129128
} catch (Exception e) {
130129
throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e);
131130
} finally {
@@ -139,18 +138,15 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<Strin
139138
}
140139
}
141140

142-
public SplitHttpResponse _post(HttpURLConnection postHttpURLConnection,
141+
public SplitHttpResponse doPost(HttpURLConnection postHttpURLConnection,
143142
HttpEntity entity,
144-
Map<String, List<String>> additionalHeaders)
145-
throws IOException {
143+
Map<String, List<String>> additionalHeaders) {
146144
try {
147145
postHttpURLConnection.setRequestMethod("POST");
148146
setBasicHeaders(postHttpURLConnection);
149147
setAdditionalAndDecoratedHeaders(postHttpURLConnection, additionalHeaders);
150148

151-
if (postHttpURLConnection.getHeaderField("Accept-Encoding") == null) {
152-
postHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
153-
}
149+
postHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
154150
postHttpURLConnection.setRequestProperty("Content-Type", "application/json");
155151
_log.debug(String.format("Request Headers: %s", postHttpURLConnection.getRequestProperties()));
156152

@@ -198,7 +194,6 @@ private void setAdditionalAndDecoratedHeaders(HttpURLConnection urlConnection, M
198194
for (Header header : request.getHeaders()) {
199195
urlConnection.setRequestProperty(header.getName(), header.getValue());
200196
}
201-
request = null;
202197
}
203198

204199
private Header[] getResponseHeaders(HttpURLConnection urlConnection) {
@@ -211,7 +206,6 @@ private Header[] getResponseHeaders(HttpURLConnection urlConnection) {
211206
}
212207
}
213208
return responseHeaders.toArray(new Header[0]);
214-
215209
}
216210
@Override
217211
public void close() throws IOException {

client/src/test/java/io/split/client/SplitClientConfigTest.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.split.client.impressions.ImpressionsManager;
77
import io.split.client.dtos.RequestContext;
88
import io.split.integrations.IntegrationsConfig;
9+
import io.split.service.HttpAuthScheme;
910
import org.junit.Assert;
1011
import org.junit.Test;
1112
import org.mockito.Mockito;
@@ -258,24 +259,11 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
258259
@Test
259260
public void checkExpectedAuthScheme() {
260261
SplitClientConfig cfg = SplitClientConfig.builder()
261-
.authScheme("kerberos")
262+
.authScheme(HttpAuthScheme.KERBEROS)
262263
.build();
263-
Assert.assertEquals("kerberos", cfg.authScheme());
264+
Assert.assertEquals(HttpAuthScheme.KERBEROS, cfg.authScheme());
264265

265266
cfg = SplitClientConfig.builder()
266-
.authScheme("KERberos")
267-
.build();
268-
Assert.assertEquals("kerberos", cfg.authScheme());
269-
270-
cfg = SplitClientConfig.builder()
271-
.build();
272-
Assert.assertEquals(null, cfg.authScheme());
273-
}
274-
275-
@Test(expected = IllegalArgumentException.class)
276-
public void checkUnexpectedAuthScheme() {
277-
SplitClientConfig cfg = SplitClientConfig.builder()
278-
.authScheme("proxy")
279267
.build();
280268
Assert.assertEquals(null, cfg.authScheme());
281269
}

client/src/test/java/io/split/client/SplitFactoryImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.split.client.utils.FileTypeEnum;
55
import io.split.client.utils.SDKMetadata;
66
import io.split.integrations.IntegrationsConfig;
7+
import io.split.service.HttpAuthScheme;
78
import io.split.service.SplitHttpClientKerberosImpl;
89
import io.split.storages.enums.OperationMode;
910
import io.split.storages.pluggable.domain.UserStorageWrapper;
@@ -353,7 +354,7 @@ public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxExc
353354
public void testFactoryKerberosInstance() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
354355
SplitClientConfig splitClientConfig = SplitClientConfig.builder()
355356
.setBlockUntilReadyTimeout(10000)
356-
.authScheme("kerberos")
357+
.authScheme(HttpAuthScheme.KERBEROS)
357358
.build();
358359
SplitFactoryImpl splitFactory = new SplitFactoryImpl("asdf", splitClientConfig);
359360

client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void testGetWithSpecialCharacters() throws URISyntaxException, IOExceptio
5353
Map<String, List<String>> additionalHeaders = Collections.singletonMap("AdditionalHeader",
5454
Collections.singletonList("add"));
5555

56-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
56+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
5757
new FetchOptions.Builder().cacheControlHeaders(true).build(), additionalHeaders);
5858
SplitChange change = Json.fromJson(splitHttpResponse.body(), SplitChange.class);
5959

@@ -84,7 +84,7 @@ public void testGetParameters() throws URISyntaxException, MalformedURLException
8484
ArgumentCaptor<HttpURLConnection> connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class);
8585
ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class);
8686
ArgumentCaptor<HashMap> headersCaptor = ArgumentCaptor.forClass(HashMap.class);
87-
verify(splitHtpClientKerberos)._get(connectionCaptor.capture(), optionsCaptor.capture(), headersCaptor.capture());
87+
verify(splitHtpClientKerberos).doGet(connectionCaptor.capture(), optionsCaptor.capture(), headersCaptor.capture());
8888

8989
assertThat(connectionCaptor.getValue().getRequestMethod(), is(equalTo("GET")));
9090
assertThat(connectionCaptor.getValue().getURL().toString(), is(equalTo(new URL("https://api.split.io/splitChanges?since=1234567").toString())));
@@ -103,7 +103,7 @@ public void testGetError() throws URISyntaxException, IOException {
103103
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);
104104

105105
SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
106-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
106+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
107107
new FetchOptions.Builder().cacheControlHeaders(true).build(), null);
108108
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode());
109109
}
@@ -120,7 +120,7 @@ public void testException() throws URISyntaxException, InvocationTargetException
120120
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);
121121

122122
SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
123-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
123+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
124124
new FetchOptions.Builder().cacheControlHeaders(true).build(), null);
125125
}
126126

@@ -149,7 +149,7 @@ public void testPost() throws URISyntaxException, IOException, ParseException {
149149
ByteArrayOutputStream mockOs = Mockito.mock( ByteArrayOutputStream.class);
150150
when(mockHttpURLConnection.getOutputStream()).thenReturn(mockOs);
151151

152-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection, Utils.toJsonEntity(toSend),
152+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection, Utils.toJsonEntity(toSend),
153153
additionalHeaders);
154154

155155
// Capture outgoing request and validate it
@@ -176,7 +176,7 @@ public void testPotParameters() throws URISyntaxException, IOException {
176176
ArgumentCaptor<HttpURLConnection> connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class);
177177
ArgumentCaptor<HttpEntity> entityCaptor = ArgumentCaptor.forClass(HttpEntity.class);
178178
ArgumentCaptor<HashMap> headersCaptor = ArgumentCaptor.forClass(HashMap.class);
179-
verify(splitHtpClientKerberos)._post(connectionCaptor.capture(), entityCaptor.capture(), headersCaptor.capture());
179+
verify(splitHtpClientKerberos).doPost(connectionCaptor.capture(), entityCaptor.capture(), headersCaptor.capture());
180180

181181
assertThat(connectionCaptor.getValue().getURL().toString(), is(equalTo(new URL("https://kubernetesturl.com/split/api/testImpressions/bulk").toString())));
182182
}
@@ -190,7 +190,7 @@ public void testPosttError() throws URISyntaxException, IOException {
190190
when(mockHttpURLConnection.getOutputStream()).thenReturn(mockOs);
191191

192192
SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
193-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
193+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection,
194194
Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null);
195195

196196
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode());
@@ -203,7 +203,7 @@ public void testPosttException() throws URISyntaxException, IOException {
203203
Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
204204

205205
SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
206-
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
206+
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection,
207207
Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null);
208208
}
209209

0 commit comments

Comments
 (0)