Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semver release #506

Merged
merged 134 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
9ece1c4
Update CHANGES.txt
sanzmauro Nov 8, 2023
8cd72f3
[SDKS-7890] Add java conventions and remove imports not using
nmayorsplit Jan 5, 2024
52e0cdb
Merge branch 'development' into SDKS-7890
nmayorsplit Jan 11, 2024
4908571
Deprecate get_telemetryRefreshRate method
nmayorsplit Jan 11, 2024
9d58e00
added request decorator class
Mar 12, 2024
0d41c4f
polish
Mar 12, 2024
a335a4a
Merge pull request #477 from splitio/custom-auth-decorator
chillaq Mar 12, 2024
cf1bd48
added httpclient wrapper
Mar 12, 2024
a372032
polish
Mar 12, 2024
5ae5405
fixed tests and added splithttpclient interface
Mar 12, 2024
1958b82
added splithttpresponse dto
Mar 13, 2024
33e0454
removed exception when status is not 200
Mar 13, 2024
f9a6e94
fixed test
Mar 13, 2024
a9c3eab
updated split change fetcher
Mar 13, 2024
12633b1
Merge pull request #478 from splitio/custom-auth-httpclient
chillaq Mar 13, 2024
8041204
moved flagset warning out of splithttp client and moved the client cl…
Mar 13, 2024
ab42b94
updated segment fetcher class
Mar 14, 2024
7dd45c2
polish
Mar 14, 2024
0675dc4
polish
Mar 14, 2024
a6d725c
updated impression sender class
Mar 14, 2024
d10e3dc
polish
Mar 14, 2024
6e0c64b
polishMerge branch 'Feature/CustomAuth' of github.com:splitio/java-cl…
Mar 14, 2024
827e501
Merge pull request #480 from splitio/custom-auth-segmentfetcher
chillaq Mar 14, 2024
3b4147a
Merge pull request #481 from splitio/custom-auth-impressionsender
chillaq Mar 18, 2024
b8fdfdd
updated Telemetry submitter and sender classes
Mar 18, 2024
61446c8
fixed tests
Mar 18, 2024
67a4f16
Merge pull request #483 from splitio/custom-auth-telemetrysender
chillaq Mar 18, 2024
7ca4ed8
updated factory and config
Mar 18, 2024
19013dc
fixed tests
Mar 18, 2024
106ad38
Merge branch 'Feature/CustomAuth' into custom-auth-factory
chillaq Mar 18, 2024
50ba346
polish
Mar 18, 2024
dd5004b
Merge pull request #484 from splitio/custom-auth-factory
chillaq Mar 19, 2024
6baef40
updated sse classes
Mar 19, 2024
feadd1a
Merge pull request #485 from splitio/custom-auth-sse
chillaq Mar 19, 2024
c04dbfc
Updated version and changes
Mar 20, 2024
77eaedc
Merge pull request #486 from splitio/custom-auth-dev
chillaq Mar 20, 2024
58dcfad
polishing
Mar 20, 2024
72679b9
polish
Mar 20, 2024
02b802f
polish
Mar 20, 2024
43489fe
Merge pull request #487 from splitio/Feature/CustomAuth
chillaq Mar 21, 2024
6288552
Converted reserved headers to lower to have case insesitive check, an…
Mar 25, 2024
35f3e4d
fix checkstyle test
Mar 25, 2024
0060f98
fixing sonar covering fail
Mar 25, 2024
4c33372
Fixing sonar coverage
Mar 25, 2024
77ad4a1
Merge pull request #488 from splitio/custom-auth-lowercase-check
chillaq Mar 26, 2024
67f98ab
Merge branch 'master' into update-changes
chillaq Mar 26, 2024
ffabca0
Merge branch 'development' into update-changes
chillaq Mar 26, 2024
5541213
Merge pull request #461 from splitio/update-changes
chillaq Mar 26, 2024
aa283e3
added headers to httpsplitresponse and polish
Mar 26, 2024
06bec68
used setHeader instead of addHeader for decorator
Mar 26, 2024
49724ce
Merge branch 'development' of github.com:splitio/java-client into dev…
mmelograno Mar 27, 2024
2eb378e
allowed multiple values per header
Mar 27, 2024
83aeb2a
fixed test
Mar 27, 2024
807a815
fixed test
Mar 27, 2024
aa9aafd
Renamed UserCustomHeaderDecorator to CustomHeaderDecorator
Mar 27, 2024
493c8f6
added RequestContext and refactored RequestDecorator classes
Mar 27, 2024
13bcbd4
Merge pull request #491 from splitio/add-response-headers
mmelograno Mar 29, 2024
3b8255d
Merge branch 'development' of github.com:splitio/java-client into dev…
mmelograno Mar 29, 2024
495d39c
fix header overrides
mredolatti Mar 29, 2024
7189a33
java 8 compliance
mredolatti Mar 29, 2024
546a220
forward decorator into streaming
mmelograno Mar 29, 2024
7896c26
add repeated headers one by one
mredolatti Mar 29, 2024
67460f3
merged
mmelograno Mar 29, 2024
ed63169
fix header count
mredolatti Mar 29, 2024
77c8bd0
Merge branch 'fix/custom_header_overrides' of github.com:splitio/java…
mmelograno Mar 29, 2024
372cc05
remove header count from test
mredolatti Mar 29, 2024
51083d7
Merge branch 'fix/custom_header_overrides' of github.com:splitio/java…
mmelograno Mar 29, 2024
b65e667
remove getHeaders in test
mredolatti Mar 29, 2024
36c0e03
Merge branch 'fix/custom_header_overrides' of github.com:splitio/java…
mmelograno Mar 29, 2024
c81ea7e
added impressionsMode in failure test
mmelograno Mar 29, 2024
537c8cf
Merge pull request #492 from splitio/fix/custom_header_overrides
mmelograno Mar 29, 2024
93c6a81
Merge branch 'fix/custom_header_overrides' of github.com:splitio/java…
mmelograno Mar 29, 2024
875e0dd
Merge branch 'development' of github.com:splitio/java-client into tas…
mmelograno Mar 29, 2024
1df6d66
preparing rc
mmelograno Mar 29, 2024
06f84cc
Merge pull request #493 from splitio/task/streamingDecorator
mmelograno Mar 29, 2024
bdc409e
close http client
mredolatti Mar 29, 2024
c917ff9
bump version in pom.xml files
mredolatti Mar 29, 2024
8fa0c9c
Merge pull request #494 from splitio/fix/close_http_client
mmelograno Mar 29, 2024
4468b2a
added unsupported matcher flow
Apr 19, 2024
8e7d57e
polish
Apr 19, 2024
e9e852e
polish
Apr 19, 2024
4502429
polish
Apr 19, 2024
8d4bc04
polish
Apr 22, 2024
3a41831
Merge pull request #496 from splitio/unsupported-matcher-flow
chillaq Apr 23, 2024
6e2a8ec
added semver class
Apr 23, 2024
4b12f7f
added equalto semver matcher
Apr 26, 2024
ac565aa
polish
Apr 26, 2024
9d4b21e
added greater or equalto semver matcher
Apr 26, 2024
b200a20
plish and test
Apr 26, 2024
ab2fb7f
polish
Apr 26, 2024
d06e38e
polish
Apr 26, 2024
dc30d42
polish
Apr 26, 2024
f68827b
added less than or equal semver matcher
Apr 26, 2024
7cc744b
Merge branch 'semver-equalto-matcher' into semver-greater-equalto-mat…
chillaq Apr 26, 2024
325d47d
added inlist semver matcher
Apr 29, 2024
d0a2f94
polish
Apr 29, 2024
249209c
added between semver matcher
Apr 29, 2024
110c038
polish
Apr 29, 2024
5d0cf8a
updated unsupported matcher label
Apr 29, 2024
2e0dbbb
added spec to splitchanges and auth urls, and updated tests
Apr 30, 2024
630363e
fix test
Apr 30, 2024
90cb602
added removing zero leading numbers in pre release
May 2, 2024
f3dbbbc
polish
May 2, 2024
84af71b
polish
May 2, 2024
28ef210
polish
May 2, 2024
6fa6faf
Merge branch 'semver-greater-equalto-matcher' into semver-less-equalt…
chillaq May 2, 2024
780ceee
polish
May 2, 2024
0fde0eb
Merge pull request #497 from splitio/semver-class
chillaq May 3, 2024
c62964e
polish
May 3, 2024
fdbd17f
polish
May 3, 2024
19ee1d2
polish
May 3, 2024
2538c8c
Merge pull request #498 from splitio/semver-equalto-matcher
chillaq May 3, 2024
e3ff49c
Merge pull request #499 from splitio/semver-greater-equalto-matcher
chillaq May 3, 2024
a75b063
Merge branch 'Feature/Semver' into semver-inlist-matcher
chillaq May 3, 2024
4abdc3e
Merge pull request #501 from splitio/semver-inlist-matcher
chillaq May 3, 2024
b48a831
Merge pull request #502 from splitio/semver-between-matcher
chillaq May 3, 2024
1204cce
Merge pull request #503 from splitio/semver-api-url
chillaq May 3, 2024
11a247a
Merge branch 'Feature/Semver' into semver-less-equalto-matcher
chillaq May 3, 2024
0f40305
Merge pull request #500 from splitio/semver-less-equalto-matcher
chillaq May 3, 2024
d8d884a
updated changes
May 3, 2024
5bc4f25
updated test coverage and version
May 3, 2024
aad9b02
updated testing version
May 3, 2024
676c481
Fix empty token handling in RawAuthResponse constructor
hpark-miovision May 9, 2024
e421434
update with last changes in development
nmayorsplit May 14, 2024
7506116
Merge pull request #469 from splitio/SDKS-7890
nmayorsplit May 14, 2024
d281a69
Update changelog and rc version
nmayorsplit May 14, 2024
0dffcd2
Merge branch 'development' into handle_auth_response_with_empty_token
nmayorsplit May 14, 2024
cda3711
Merge pull request #505 from hpark-miovision/handle_auth_response_wit…
nmayorsplit May 14, 2024
43d25ad
Pr suggestions
nmayorsplit May 14, 2024
8073fed
Update rc version
nmayorsplit May 15, 2024
9ae9fe4
Sonarqube suggestions
nmayorsplit May 15, 2024
3b813d8
Update with sonarqube suggestions
nmayorsplit May 15, 2024
b754a5d
Update to final version
nmayorsplit May 15, 2024
426ea5a
Merge pull request #504 from splitio/Feature/Semver
mmelograno May 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
4.12.0 (May 15, 2024)
- Added support for targeting rules based on semantic versions (https://semver.org/).
- Added the logic to handle correctly when the SDK receives an unsupported Matcher type.
- Enhanced SDK Headers for Authorization Frameworks
- Cleaned unused imports and renaming some methods
- Fixed empty token handler thanks to @hpark-miovision

4.11.1 (Feb 29, 2024)
- Fixed deadlock in UniqueKeysTracker when sending Unique Keys.

Expand All @@ -9,7 +16,7 @@
- Added getTreatmentsByFlagSets without attributes.
- Fixed some issues for flag sets: Not logging a warning when using flag sets that don't contain cached feature flags.

4.10.1 (Nov 9, 2023)
4.10.1 (Nov 8, 2023)
- Fixed handler for response http headers.

4.10.0 (Nov 2, 2023)
Expand Down
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>4.11.1</version>
<version>4.12.0</version>
</parent>
<artifactId>java-client</artifactId>
<packaging>jar</packaging>
Expand Down
11 changes: 11 additions & 0 deletions client/src/main/java/io/split/Spec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.split;

public final class Spec {

private Spec() {
// restrict instantiation
}

public static final String SPEC_VERSION = "1.1";
}

15 changes: 15 additions & 0 deletions client/src/main/java/io/split/client/CustomHeaderDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.split.client;

import io.split.client.dtos.RequestContext;

import java.util.Map;
import java.util.List;

public interface CustomHeaderDecorator
{
/**
* Get the additional headers needed for all http operations
* @return HashMap of addition headers
*/
Map<String, List<String>> getHeaderOverrides(RequestContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

import com.google.common.annotations.VisibleForTesting;
import io.split.client.dtos.SegmentChange;
import io.split.client.dtos.SplitHttpResponse;
import io.split.client.utils.Json;
import io.split.client.utils.Utils;
import io.split.engine.common.FetchOptions;
import io.split.engine.segments.SegmentChangeFetcher;
import io.split.service.SplitHttpClient;
import io.split.telemetry.domain.enums.HTTPLatenciesEnum;
import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum;
import io.split.telemetry.domain.enums.ResourceEnum;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -33,20 +30,17 @@ public final class HttpSegmentChangeFetcher implements SegmentChangeFetcher {

private static final String SINCE = "since";
private static final String TILL = "till";
private static final String PREFIX = "segmentChangeFetcher";
private static final String CACHE_CONTROL_HEADER_NAME = "Cache-Control";
private static final String CACHE_CONTROL_HEADER_VALUE = "no-cache";

private final CloseableHttpClient _client;
private final SplitHttpClient _client;
private final URI _target;
private final TelemetryRuntimeProducer _telemetryRuntimeProducer;

public static HttpSegmentChangeFetcher create(CloseableHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer)
public static HttpSegmentChangeFetcher create(SplitHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer)
throws URISyntaxException {
return new HttpSegmentChangeFetcher(client, Utils.appendPath(root, "api/segmentChanges"), telemetryRuntimeProducer);
}

private HttpSegmentChangeFetcher(CloseableHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer) {
private HttpSegmentChangeFetcher(SplitHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer) {
_client = client;
_target = uri;
checkNotNull(_target);
Expand All @@ -57,8 +51,6 @@ private HttpSegmentChangeFetcher(CloseableHttpClient client, URI uri, TelemetryR
public SegmentChange fetch(String segmentName, long since, FetchOptions options) {
long start = System.currentTimeMillis();

CloseableHttpResponse response = null;

try {
String path = _target.getPath() + "/" + segmentName;
URIBuilder uriBuilder = new URIBuilder(_target)
Expand All @@ -69,45 +61,27 @@ public SegmentChange fetch(String segmentName, long since, FetchOptions options)
}

URI uri = uriBuilder.build();
HttpGet request = new HttpGet(uri);

if(options.cacheControlHeadersEnabled()) {
request.setHeader(CACHE_CONTROL_HEADER_NAME, CACHE_CONTROL_HEADER_VALUE);
}

response = _client.execute(request);

int statusCode = response.getCode();

if (_log.isDebugEnabled()) {
_log.debug(String.format("[%s] %s. Status code: %s", request.getMethod(), uri.toURL(), statusCode));
}
SplitHttpResponse response = _client.get(uri, options, null);

if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SEGMENT_SYNC, statusCode);
_log.error(String.format("Response status was: %s. Reason: %s", statusCode , response.getReasonPhrase()));
if (statusCode == HttpStatus.SC_FORBIDDEN) {
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SEGMENT_SYNC, response.statusCode());
if (response.statusCode() == HttpStatus.SC_FORBIDDEN) {
_log.error("factory instantiation: you passed a client side type sdkKey, " +
"please grab an sdk key from the Split user interface that is of type server side");
}
throw new IllegalStateException(String.format("Could not retrieve segment changes for %s, since %s; http return code %s",
segmentName, since, statusCode));
segmentName, since, response.statusCode()));
}

_telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.SEGMENTS, System.currentTimeMillis());

String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

return Json.fromJson(json, SegmentChange.class);
return Json.fromJson(response.body(), SegmentChange.class);
} catch (Exception e) {
throw new IllegalStateException(String.format("Error occurred when trying to sync segment: %s, since: %s. Details: %s",
segmentName, since, e), e);
} finally {
_telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SEGMENTS, System.currentTimeMillis()-start);
Utils.forceClose(response);
}


}

@VisibleForTesting
Expand Down
63 changes: 22 additions & 41 deletions client/src/main/java/io/split/client/HttpSplitChangeFetcher.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package io.split.client;

import com.google.common.annotations.VisibleForTesting;

import io.split.client.dtos.SplitChange;
import io.split.client.dtos.SplitHttpResponse;
import io.split.client.exceptions.UriTooLongException;
import io.split.client.utils.Json;
import io.split.client.utils.Utils;
import io.split.engine.common.FetchOptions;
import io.split.engine.experiments.SplitChangeFetcher;
import io.split.service.SplitHttpClient;
import io.split.telemetry.domain.enums.HTTPLatenciesEnum;
import io.split.telemetry.domain.enums.ResourceEnum;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.split.Spec.SPEC_VERSION;

/**
* Created by adilaijaz on 5/30/15.
Expand All @@ -34,20 +33,17 @@ public final class HttpSplitChangeFetcher implements SplitChangeFetcher {
private static final String SINCE = "since";
private static final String TILL = "till";
private static final String SETS = "sets";

private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control";
private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache";

private final CloseableHttpClient _client;
private static final String SPEC = "s";
private final SplitHttpClient _client;
private final URI _target;
private final TelemetryRuntimeProducer _telemetryRuntimeProducer;

public static HttpSplitChangeFetcher create(CloseableHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer)
public static HttpSplitChangeFetcher create(SplitHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer)
throws URISyntaxException {
return new HttpSplitChangeFetcher(client, Utils.appendPath(root, "api/splitChanges"), telemetryRuntimeProducer);
}

private HttpSplitChangeFetcher(CloseableHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer) {
private HttpSplitChangeFetcher(SplitHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer) {
_client = client;
_target = uri;
checkNotNull(_target);
Expand All @@ -64,49 +60,34 @@ public SplitChange fetch(long since, FetchOptions options) {

long start = System.currentTimeMillis();

CloseableHttpResponse response = null;

try {
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SINCE, "" + since);
if (options.hasCustomCN()) {
uriBuilder.addParameter(TILL, "" + options.targetCN());
}
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + SPEC_VERSION);
uriBuilder.addParameter(SINCE, "" + since);
if (!options.flagSetsFilter().isEmpty()) {
uriBuilder.addParameter(SETS, "" + options.flagSetsFilter());
}
URI uri = uriBuilder.build();

HttpGet request = new HttpGet(uri);
if(options.cacheControlHeadersEnabled()) {
request.setHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE);
}

response = _client.execute(request);

int statusCode = response.getCode();

if (_log.isDebugEnabled()) {
_log.debug(String.format("[%s] %s. Status code: %s", request.getMethod(), uri.toURL(), statusCode));
if (options.hasCustomCN()) {
uriBuilder.addParameter(TILL, "" + options.targetCN());
}
URI uri = uriBuilder.build();
SplitHttpResponse response = _client.get(uri, options, null);

if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SPLIT_SYNC, statusCode);
if (statusCode == HttpStatus.SC_REQUEST_URI_TOO_LONG) {
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
if (response.statusCode() == HttpStatus.SC_REQUEST_URI_TOO_LONG) {
_log.error("The amount of flag sets provided are big causing uri length error.");
throw new UriTooLongException(String.format("Status code: %s. Message: %s", statusCode, response.getReasonPhrase()));
throw new UriTooLongException(String.format("Status code: %s. Message: %s", response.statusCode(), response.statusMessage()));
}
_log.warn(String.format("Response status was: %s. Reason: %s", statusCode , response.getReasonPhrase()));
throw new IllegalStateException(String.format("Could not retrieve splitChanges since %s; http return code %s", since, statusCode));
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SPLIT_SYNC, response.statusCode());
throw new IllegalStateException(
String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode())
);
}

String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

return Json.fromJson(json, SplitChange.class);
return Json.fromJson(response.body(), SplitChange.class);
} catch (Exception e) {
throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e);
} finally {
_telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SPLITS, System.currentTimeMillis()-start);
Utils.forceClose(response);
}
}

Expand Down
18 changes: 18 additions & 0 deletions client/src/main/java/io/split/client/NoOpHeaderDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.split.client;

import io.split.client.CustomHeaderDecorator;
import io.split.client.dtos.RequestContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class NoOpHeaderDecorator implements CustomHeaderDecorator {
public NoOpHeaderDecorator() {
}

@Override
public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
return new HashMap<>();
}
}
77 changes: 77 additions & 0 deletions client/src/main/java/io/split/client/RequestDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.split.client;

import io.split.client.dtos.RequestContext;

import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.Header;

import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

public final class RequestDecorator {
CustomHeaderDecorator _headerDecorator;

private static final Set<String> forbiddenHeaders = new HashSet<>(Arrays.asList(
"splitsdkversion",
"splitmachineip",
"splitmachinename",
"splitimpressionsmode",
"host",
"referrer",
"content-type",
"content-length",
"content-encoding",
"accept",
"keep-alive",
"x-fastly-debug"));

public RequestDecorator(CustomHeaderDecorator headerDecorator) {
_headerDecorator = (headerDecorator == null)
? new NoOpHeaderDecorator()
: headerDecorator;
}

public HttpRequest decorateHeaders(HttpRequest request) {
try {
Map<String, List<String>> headers = _headerDecorator
.getHeaderOverrides(new RequestContext(convertToMap(request.getHeaders())));
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
if (isHeaderAllowed(entry.getKey())) {
List<String> values = entry.getValue();
for (int i = 0; i < values.size(); i++) {
if (i == 0) {
request.setHeader(entry.getKey(), values.get(i));
} else {
request.addHeader(entry.getKey(), values.get(i));
}
}
}
}
} catch (Exception e) {
throw new IllegalArgumentException(
String.format("Problem adding custom headers to request decorator: %s", e), e);
}

return request;
}

private boolean isHeaderAllowed(String headerName) {
return !forbiddenHeaders.contains(headerName.toLowerCase());
}

private Map<String, List<String>> convertToMap(Header[] to_convert) {
Map<String, List<String>> to_return = new HashMap<String, List<String>>();
for (Integer i = 0; i < to_convert.length; i++) {
if (!to_return.containsKey(to_convert[i].getName())) {
to_return.put(to_convert[i].getName(), new ArrayList<String>());
}
to_return.get(to_convert[i].getName()).add(to_convert[i].getValue());
}
return to_return;
}
}
Loading
Loading