Skip to content

Commit

Permalink
[7445] Add InputStreamProviderException, add getInputStreamProviderAn…
Browse files Browse the repository at this point in the history
…dFileType in SplitFactoryImpl and some improvements
  • Loading branch information
nmayorsplit committed Aug 30, 2023
1 parent d76fa70 commit cfde3f7
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.stream.JsonReader;
import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.Json;
import io.split.client.utils.LocalhostSanitizer;
Expand All @@ -12,7 +13,6 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
Expand All @@ -31,17 +31,19 @@ public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider)
@Override
public SplitChange fetch(long since, FetchOptions options) {
try {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), "UTF-8"));
JsonReader jsonReader = new JsonReader(streamReader);
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), "UTF-8")));
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
return processSplitChange(splitChange, since);
} catch (InputStreamProviderException i) {
_log.warn(String.format("Problem to fetch split change using file named %s", i.getFileName()));
throw new IllegalStateException("Problem fetching splitChanges: " + i.getMessage(), i);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using a file"), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}

private SplitChange processSplitChange(SplitChange splitChange, long changeNumber) throws NoSuchAlgorithmException, UnsupportedEncodingException {
private SplitChange processSplitChange(SplitChange splitChange, long changeNumber) throws NoSuchAlgorithmException {
SplitChange splitChangeToProcess = LocalhostSanitizer.sanitization(splitChange);
// if the till is less than storage CN and different from the default till ignore the change
if (splitChangeToProcess.till < changeNumber && splitChangeToProcess.till != -1) {
Expand Down
84 changes: 48 additions & 36 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import io.split.client.interceptors.GzipEncoderRequestInterceptor;
import io.split.client.interceptors.SdkMetadataInterceptorFilter;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.FileTypeEnum;
import io.split.client.utils.InputStreamProviderImp;
import io.split.client.utils.LocalhostPair;
import io.split.client.utils.SDKMetadata;
import io.split.engine.SDKReadinessGates;
import io.split.engine.common.ConsumerSyncManager;
Expand Down Expand Up @@ -102,6 +103,7 @@
import pluggable.CustomStorageWrapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -369,44 +371,19 @@ protected SplitFactoryImpl(SplitClientConfig config) {
config.getThreadFactory());

// SplitFetcher
LocalhostPair pair = getInputStreamProviderAndFileType(config.splitFile(), config.inputStream(), config.fileType());
SplitChangeFetcher splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile());
InputStreamProvider inputStreamProvider;
String splitFile = config.splitFile();
if (splitFile != null) {
try {
if (splitFile.toLowerCase().endsWith(".json")) {
inputStreamProvider = new FileInputStreamProvider(splitFile);
splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
} else if (!splitFile.isEmpty() && (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml"))) {
inputStreamProvider = new FileInputStreamProvider(splitFile);
splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider);
}
} catch (Exception e) {
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
splitFile, splitFile), e);
}
} else if (config.inputStream() != null) {
inputStreamProvider = new InputStreamProviderImp(config.inputStream());
if (config.fileType() != null) {
switch (config.fileType()) {
case JSON:
splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
break;
case YAML:
splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider);
break;
}
} else {
_log.warn("Should add an fileType in the config if there is an inputStream");
}
if (pair == null) {
_log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config.");
} else {
_log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile doesn't add it in the config.");
switch (pair.getFileTypeEnum()) {
case JSON:
splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(pair.getInputStreamProvider());
break;
case YAML:
splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(pair.getInputStreamProvider());
}
}

SplitParser splitParser = new SplitParser();

_splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer);
Expand Down Expand Up @@ -674,4 +651,39 @@ private UniqueKeysTracker createUniqueKeysTracker(SplitClientConfig config){
}
return null;
}

private LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputStream inputStream,
FileTypeEnum fileType) {
if (splitFile != null && inputStream != null) {
_log.warn("splitFile or inputStreamProvider should have a value, not both");
return null;
}
if (inputStream != null && fileType == null) {
_log.warn("If inputStreamProvider is not null, then fileType must also have a non-null value");
return null;
}
if (inputStream == null && splitFile == null){
_log.warn("splitFile or inputStreamProvider should have a value");
return null;
}
if (splitFile != null) {
try {
if (splitFile.toLowerCase().endsWith(".json")) {
return new LocalhostPair(new FileInputStreamProvider(splitFile), FileTypeEnum.JSON);
} else if (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml")) {
return new LocalhostPair(new FileInputStreamProvider(splitFile), FileTypeEnum.YAML);
}
} catch (Exception e) {
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
splitFile, splitFile), e);
}
} else if (inputStream != null) {
return new LocalhostPair(new InputStreamProviderImp(inputStream), fileType);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.split.client.exceptions;

public class InputStreamProviderException extends Exception {
private final String _fileName;

public InputStreamProviderException(String fileName, String message) {
super(message);
_fileName = fileName;
}

public String getFileName() {
return _fileName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.split.client.utils;

import io.split.client.exceptions.InputStreamProviderException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
Expand All @@ -13,7 +15,11 @@ public FileInputStreamProvider(String fileName) {
}

@Override
public InputStream get() throws FileNotFoundException {
return new FileInputStream(_fileName);
public InputStream get() throws InputStreamProviderException {
try {
return new FileInputStream(_fileName);
} catch (FileNotFoundException f) {
throw new InputStreamProviderException(_fileName, f.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.split.client.utils;

import java.io.FileNotFoundException;
import io.split.client.exceptions.InputStreamProviderException;

import java.io.InputStream;

public interface InputStreamProvider {

public InputStream get() throws FileNotFoundException;
InputStream get() throws InputStreamProviderException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.split.client.utils;

import java.io.FileNotFoundException;
import io.split.client.exceptions.InputStreamProviderException;

import java.io.InputStream;

public class InputStreamProviderImp implements InputStreamProvider {
Expand All @@ -11,7 +12,7 @@ public InputStreamProviderImp(InputStream inputStream){
}

@Override
public InputStream get() throws FileNotFoundException {
public InputStream get() throws InputStreamProviderException {
return _inputStream;
}
}
20 changes: 20 additions & 0 deletions client/src/main/java/io/split/client/utils/LocalhostPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.split.client.utils;

public class LocalhostPair {

private final InputStreamProvider _inputStreamProvider;
private final FileTypeEnum _fileTypeEnum;

public LocalhostPair(InputStreamProvider inputStreamProvider, FileTypeEnum fileType) {
_inputStreamProvider = inputStreamProvider;
_fileTypeEnum = fileType;
}

public InputStreamProvider getInputStreamProvider() {
return _inputStreamProvider;
}

public FileTypeEnum getFileTypeEnum() {
return _fileTypeEnum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down

0 comments on commit cfde3f7

Please sign in to comment.