"
4 | labels: ["enhancement"]
5 | body:
6 | - type: textarea
7 | id: description
8 | attributes:
9 | label: "Description"
10 | description: Briefly describe the enhancement in a few sentences.
11 | placeholder: Short description...
12 | validations:
13 | required: true
14 | - type: textarea
15 | id: benefits
16 | attributes:
17 | label: "Benefits"
18 | description: How would the enhancement benefit to your product or usage?
19 | placeholder: Benefits...
20 | validations:
21 | required: true
22 | - type: textarea
23 | id: detail
24 | attributes:
25 | label: "Detail"
26 | description: How would you like the enhancement to work? Please provide as much detail as possible
27 | placeholder: Detailed description...
28 | validations:
29 | required: false
30 | - type: textarea
31 | id: examples
32 | attributes:
33 | label: "Examples"
34 | description: Are there any examples of this enhancement in other products/services? If so, please provide links or references.
35 | placeholder: Links/References...
36 | validations:
37 | required: false
38 | - type: textarea
39 | id: risks
40 | attributes:
41 | label: "Risks/Downsides"
42 | description: Do you think this enhancement could have any potential downsides or risks?
43 | placeholder: Risks/Downsides...
44 | validations:
45 | required: false
46 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/FEATURE-REQUEST.md:
--------------------------------------------------------------------------------
1 |
4 | ## Feedback requesting a new feature can be shared [here.](https://feedback.optimizely.com/)
5 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/config.yml:
--------------------------------------------------------------------------------
1 | blank_issues_enabled: false
2 | contact_links:
3 | - name: 💡Feature Requests
4 | url: https://feedback.optimizely.com/
5 | about: Feedback requesting a new feature can be shared here.
6 |
--------------------------------------------------------------------------------
/.github/pull_request_template.md:
--------------------------------------------------------------------------------
1 | ## Summary
2 | - The "what"; a concise description of each logical change
3 | - Another change
4 |
5 | The "why", or other context.
6 |
7 | ## Test plan
8 |
9 | ## Issues
10 | - "THING-1234" or "Fixes #123"
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Reusable action of building snapshot and publish
2 |
3 | on:
4 | workflow_call:
5 | inputs:
6 | action:
7 | required: true
8 | type: string
9 | github_tag:
10 | required: true
11 | type: string
12 | secrets:
13 | MAVEN_SIGNING_KEY_BASE64:
14 | required: true
15 | MAVEN_SIGNING_PASSPHRASE:
16 | required: true
17 | MAVEN_CENTRAL_USERNAME:
18 | required: true
19 | MAVEN_CENTRAL_PASSWORD:
20 | required: true
21 | jobs:
22 | run_build:
23 | runs-on: ubuntu-latest
24 | steps:
25 | - uses: actions/checkout@v4
26 | - name: set up JDK 8
27 | uses: actions/setup-java@v2
28 | with:
29 | java-version: '8'
30 | distribution: 'temurin'
31 | - name: Grant execute permission for gradlew
32 | run: chmod +x gradlew
33 | - name: ${{ inputs.action }}
34 | env:
35 | MAVEN_SIGNING_KEY_BASE64: ${{ secrets.MAVEN_SIGNING_KEY_BASE64 }}
36 | MAVEN_SIGNING_PASSPHRASE: ${{ secrets.MAVEN_SIGNING_PASSPHRASE }}
37 | MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
38 | MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
39 | run: GITHUB_TAG=${{ inputs.github_tag }} ./gradlew ${{ inputs.action }}
40 |
--------------------------------------------------------------------------------
/.github/workflows/integration_test.yml:
--------------------------------------------------------------------------------
1 | name: Reusable action of running integration of production suite
2 |
3 | on:
4 | workflow_call:
5 | inputs:
6 | FULLSTACK_TEST_REPO:
7 | required: false
8 | type: string
9 | secrets:
10 | CI_USER_TOKEN:
11 | required: true
12 | jobs:
13 | test:
14 | runs-on: ubuntu-latest
15 | steps:
16 | - uses: actions/checkout@v4
17 | with:
18 | # You should create a personal access token and store it in your repository
19 | token: ${{ secrets.CI_USER_TOKEN }}
20 | repository: 'optimizely/ci-helper-tools'
21 | path: 'home/runner/ci-helper-tools'
22 | ref: 'master'
23 | - name: set SDK Branch if PR
24 | env:
25 | HEAD_REF: ${{ github.head_ref }}
26 | if: ${{ github.event_name == 'pull_request' }}
27 | run: |
28 | echo "SDK_BRANCH=$HEAD_REF" >> $GITHUB_ENV
29 | - name: set SDK Branch if not pull request
30 | env:
31 | REF_NAME: ${{ github.ref_name }}
32 | if: ${{ github.event_name != 'pull_request' }}
33 | run: |
34 | echo "SDK_BRANCH=$REF_NAME" >> $GITHUB_ENV
35 | - name: Trigger build
36 | env:
37 | SDK: java
38 | FULLSTACK_TEST_REPO: ${{ inputs.FULLSTACK_TEST_REPO }}
39 | BUILD_NUMBER: ${{ github.run_id }}
40 | TESTAPP_BRANCH: master
41 | GITHUB_TOKEN: ${{ secrets.CI_USER_TOKEN }}
42 | EVENT_TYPE: ${{ github.event_name }}
43 | GITHUB_CONTEXT: ${{ toJson(github) }}
44 | PULL_REQUEST_SLUG: ${{ github.repository }}
45 | UPSTREAM_REPO: ${{ github.repository }}
46 | PULL_REQUEST_SHA: ${{ github.event.pull_request.head.sha }}
47 | PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
48 | UPSTREAM_SHA: ${{ github.sha }}
49 | EVENT_MESSAGE: ${{ github.event.message }}
50 | HOME: 'home/runner'
51 | run: |
52 | echo "$GITHUB_CONTEXT"
53 | home/runner/ci-helper-tools/trigger-script-with-status-update.sh
54 |
--------------------------------------------------------------------------------
/.github/workflows/source_clear_cron.yml:
--------------------------------------------------------------------------------
1 | name: Source clear
2 |
3 | on:
4 | schedule:
5 | # Runs "weekly"
6 | - cron: '0 0 * * 0'
7 |
8 | jobs:
9 | source_clear:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v2
13 | - name: Source clear scan
14 | env:
15 | SRCCLR_API_TOKEN: ${{ secrets.SRCCLR_API_TOKEN }}
16 | run: curl -sSL https://download.sourceclear.com/ci.sh | bash -s – scan
17 |
--------------------------------------------------------------------------------
/.github/workflows/ticket_reference_check.yml:
--------------------------------------------------------------------------------
1 | name: Jira ticket reference check
2 |
3 | on:
4 | pull_request:
5 | types: [opened, edited, reopened, synchronize]
6 |
7 | jobs:
8 |
9 | jira_ticket_reference_check:
10 | runs-on: ubuntu-latest
11 |
12 | steps:
13 | - name: Check for Jira ticket reference
14 | uses: optimizely/github-action-ticket-reference-checker-public@master
15 | with:
16 | bodyRegex: 'FSSDK-(?\d+)'
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.un~
2 | .gradle
3 | .settings
4 | *.swp
5 |
6 | # intellij config files
7 | *.iml
8 | *.ipr
9 | *.iml
10 | *.iws
11 | .idea
12 |
13 | # eclipse config files
14 | .classpath
15 | .project
16 |
17 | # android studio config files
18 | local.properties
19 |
20 | **/build
21 | bin
22 | out
23 | classes
24 |
25 | .vagrant
26 | .DS_Store
27 | .venv
28 |
--------------------------------------------------------------------------------
/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # This is a comment.
2 | # Each line is a file pattern followed by one or more owners.
3 |
4 | # These owners will be the default owners for everything in the repo.
5 | # Unless a later match takes precedence, @global-owner1 and @global-owner2
6 | # will be requested for review when someone opens a pull request.
7 | * @optimizely/fullstack-devs
8 |
9 | # Order is important; the last matching pattern takes the most precedence.
10 | # When someone opens a pull request that only modifies JS files, only @js-owner
11 | # and not the global owner(s) will be requested for a review.
12 | #*.js @js-owner
13 |
14 | # You can also use email addresses if you prefer. They'll be used to look up
15 | # users just like we do for commit author emails.
16 | #docs/* docs@example.com
17 |
--------------------------------------------------------------------------------
/core-api/build.gradle:
--------------------------------------------------------------------------------
1 | dependencies {
2 | implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
3 | implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: jacksonVersion
4 | implementation group: 'com.google.code.findbugs', name: 'annotations', version: findbugsAnnotationVersion
5 | implementation group: 'com.google.code.findbugs', name: 'jsr305', version: findbugsJsrVersion
6 | testImplementation group: 'junit', name: 'junit', version: junitVersion
7 | testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion
8 |
9 | // an assortment of json parsers
10 | compileOnly group: 'com.google.code.gson', name: 'gson', version: gsonVersion, optional
11 | compileOnly group: 'org.json', name: 'json', version: jsonVersion, optional
12 | compileOnly group: 'com.googlecode.json-simple', name: 'json-simple', version: jsonSimpleVersion, optional
13 | compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion, optional
14 | }
15 |
16 | tasks.named('processJmhResources') {
17 | duplicatesStrategy = DuplicatesStrategy.WARN
18 | }
19 |
20 |
21 | test {
22 | useJUnit {
23 | excludeCategories 'com.optimizely.ab.categories.ExhaustiveTest'
24 | }
25 | }
26 |
27 | task exhaustiveTest(type: Test) {
28 | useJUnit {
29 | includeCategories 'com.optimizely.ab.categories.ExhaustiveTest'
30 | }
31 | }
32 |
33 |
34 | task generateVersionFile {
35 | // add the build version information into a file that'll go into the distribution
36 | ext.buildVersion = new File(projectDir, "src/main/resources/optimizely-build-version")
37 | buildVersion.text = version
38 | }
39 |
40 | build.finalizedBy(generateVersionFile)
41 |
--------------------------------------------------------------------------------
/core-api/src/jmh/java/com/optimizely/ab/BenchmarkUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import com.google.common.base.Charsets;
20 | import com.google.common.io.Resources;
21 |
22 | import java.io.IOException;
23 |
24 | public final class BenchmarkUtils {
25 |
26 | private BenchmarkUtils() {}
27 |
28 | public static String getProfilingDatafile(String datafilePath) throws IOException {
29 | return Resources.toString(Resources.getResource(datafilePath), Charsets.UTF_8);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/jmh/java/com/optimizely/ab/config/parser/JacksonConfigParserBenchmark.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.DatafileProjectConfigTestUtils;
21 | import org.openjdk.jmh.annotations.*;
22 |
23 | import java.io.IOException;
24 | import java.util.concurrent.TimeUnit;
25 |
26 | @BenchmarkMode(Mode.AverageTime)
27 | @OutputTimeUnit(TimeUnit.MICROSECONDS)
28 | @Fork(2)
29 | @Warmup(iterations = 10)
30 | @Measurement(iterations = 20)
31 | @State(Scope.Benchmark)
32 | public class JacksonConfigParserBenchmark {
33 | JacksonConfigParser parser;
34 | String jsonV2;
35 | String jsonV3;
36 | String jsonV4;
37 |
38 | @Setup
39 | public void setUp() throws IOException {
40 | parser = new JacksonConfigParser();
41 | jsonV2 = DatafileProjectConfigTestUtils.validConfigJsonV2();
42 | jsonV3 = DatafileProjectConfigTestUtils.validConfigJsonV3();
43 | jsonV4 = DatafileProjectConfigTestUtils.validConfigJsonV4();
44 | }
45 |
46 | @Benchmark
47 | public ProjectConfig parseV2() throws ConfigParseException {
48 | return parser.parseProjectConfig(jsonV2);
49 | }
50 |
51 | @Benchmark
52 | public ProjectConfig parseV3() throws ConfigParseException {
53 | return parser.parseProjectConfig(jsonV3);
54 | }
55 |
56 | @Benchmark
57 | public ProjectConfig parseV4() throws ConfigParseException {
58 | return parser.parseProjectConfig(jsonV4);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/core-api/src/jmh/resources/benchmark.properties:
--------------------------------------------------------------------------------
1 | datafilePathTemplate=config/profiling-test-data-%d-experiments.json
2 | activateGroupExperimentUserIdPropTemplate=activateGroupExperiment%dExperimentsUserId
3 | activateGroupExperiment10ExperimentsUserId=no
4 | activateGroupExperiment25ExperimentsUserId=test
5 | activateGroupExperiment50ExperimentsUserId=optimizely_user
6 |
7 | activateGroupExperimentAttributesUserIdPropTemplate=activateGroupExperimentAttributes%dExperimentsUserId
8 | activateGroupExperimentAttributes10ExperimentsUserId=test
9 | activateGroupExperimentAttributes25ExperimentsUserId=yes
10 | activateGroupExperimentAttributes50ExperimentsUserId=test
11 |
12 | trackGroupExperimentUserIdPropTemplate=trackGroupExperiment%dExperimentsUserId
13 | trackGroupExperiment10ExperimentsUserId=no
14 | trackGroupExperiment25ExperimentsUserId=optimizely_user
15 | trackGroupExperiment50ExperimentsUserId=optimizely_user
16 |
17 | trackGroupExperimentAttributesUserIdPropTemplate=trackGroupExperimentAttributes%dExperimentsUserId
18 | trackGroupExperimentAttributes10ExperimentsUserId=optimizely_user
19 | trackGroupExperimentAttributes25ExperimentsUserId=yes
20 | trackGroupExperimentAttributes50ExperimentsUserId=test
--------------------------------------------------------------------------------
/core-api/src/jmh/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/OptimizelyDecisionContext.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import javax.annotation.Nonnull;
20 | import javax.annotation.Nullable;
21 |
22 | public class OptimizelyDecisionContext {
23 | public static final String OPTI_NULL_RULE_KEY = "$opt-null-rule-key";
24 | public static final String OPTI_KEY_DIVIDER = "-$opt$-";
25 |
26 | private String flagKey;
27 | private String ruleKey;
28 |
29 | public OptimizelyDecisionContext(@Nonnull String flagKey, @Nullable String ruleKey) {
30 | if (flagKey == null) throw new NullPointerException("FlagKey must not be null, please provide a valid input.");
31 | this.flagKey = flagKey;
32 | this.ruleKey = ruleKey;
33 | }
34 |
35 | public String getFlagKey() {
36 | return flagKey;
37 | }
38 |
39 | public String getRuleKey() {
40 | return ruleKey != null ? ruleKey : OPTI_NULL_RULE_KEY;
41 | }
42 |
43 | public String getKey() {
44 | StringBuilder keyBuilder = new StringBuilder();
45 | keyBuilder.append(flagKey);
46 | keyBuilder.append(OPTI_KEY_DIVIDER);
47 | keyBuilder.append(getRuleKey());
48 | return keyBuilder.toString();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/OptimizelyForcedDecision.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import javax.annotation.Nonnull;
20 |
21 | public class OptimizelyForcedDecision {
22 | private String variationKey;
23 |
24 | public OptimizelyForcedDecision(@Nonnull String variationKey) {
25 | this.variationKey = variationKey;
26 | }
27 |
28 | public String getVariationKey() {
29 | return variationKey;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/OptimizelyRuntimeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | /**
20 | * Base class for all Optimizely Exceptions.
21 | */
22 | public class OptimizelyRuntimeException extends RuntimeException {
23 |
24 | public OptimizelyRuntimeException() {
25 | }
26 |
27 | public OptimizelyRuntimeException(Exception exception) {
28 | super(exception);
29 | }
30 |
31 | public OptimizelyRuntimeException(String message) {
32 | super(message);
33 | }
34 |
35 | public OptimizelyRuntimeException(String message, Throwable cause) {
36 | super(message, cause);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/UnknownEventTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.EventType;
21 |
22 | /**
23 | * Exception thrown when attempting to use/refer to an {@link EventType} that isn't present in the current
24 | * {@link ProjectConfig}.
25 | */
26 | public class UnknownEventTypeException extends OptimizelyRuntimeException {
27 |
28 | public UnknownEventTypeException(String message) {
29 | super(message);
30 | }
31 |
32 | public UnknownEventTypeException(String message, Throwable cause) {
33 | super(message, cause);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/UnknownExperimentException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.Experiment;
21 |
22 | /**
23 | * Exception thrown when attempting to use/refer to an {@link Experiment} that isn't present in the current
24 | * {@link ProjectConfig}.
25 | *
26 | * This may occur if code changes are made prior to the experiment being setup in the Optimizely application.
27 | */
28 | public class UnknownExperimentException extends OptimizelyRuntimeException {
29 |
30 | public UnknownExperimentException(String message) {
31 | super(message);
32 | }
33 |
34 | public UnknownExperimentException(String message, Throwable cause) {
35 | super(message, cause);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/annotations/VisibleForTesting.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.annotations;
18 |
19 | /**
20 | * Indicates that the annotated resources is non-private for testing purposes only.
21 | */
22 | public @interface VisibleForTesting { }
23 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/AtomicProjectConfigManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, 2023, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import java.util.concurrent.atomic.AtomicReference;
20 |
21 | public class AtomicProjectConfigManager implements ProjectConfigManager {
22 |
23 | private final AtomicReference projectConfigReference = new AtomicReference<>();
24 |
25 | @Override
26 | public ProjectConfig getConfig() {
27 | return projectConfigReference.get();
28 | }
29 |
30 | /**
31 | * Access to current cached project configuration.
32 | *
33 | * @return {@link ProjectConfig}
34 | */
35 | @Override
36 | public ProjectConfig getCachedConfig() {
37 | return projectConfigReference.get();
38 | }
39 |
40 | @Override
41 | public String getSDKKey() {
42 | return null;
43 | }
44 |
45 | public void setConfig(ProjectConfig projectConfig) {
46 | projectConfigReference.set(projectConfig);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/Attribute.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | import javax.annotation.concurrent.Immutable;
24 |
25 | /**
26 | * Represents the Optimizely Attribute configuration.
27 | *
28 | * @see Project JSON
29 | */
30 | @Immutable
31 | @JsonIgnoreProperties(ignoreUnknown = true)
32 | public class Attribute implements IdKeyMapped {
33 |
34 | private final String id;
35 | private final String key;
36 | private final String segmentId;
37 |
38 | public Attribute(String id, String key) {
39 | this(id, key, null);
40 | }
41 |
42 | @JsonCreator
43 | public Attribute(@JsonProperty("id") String id,
44 | @JsonProperty("key") String key,
45 | @JsonProperty("segmentId") String segmentId) {
46 | this.id = id;
47 | this.key = key;
48 | this.segmentId = segmentId;
49 | }
50 |
51 | public String getId() {
52 | return id;
53 | }
54 |
55 | public String getKey() {
56 | return key;
57 | }
58 |
59 | public String getSegmentId() {
60 | return segmentId;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return "Attribute{" +
66 | "id='" + id + '\'' +
67 | ", key='" + key + '\'' +
68 | ", segmentId='" + segmentId + '\'' +
69 | '}';
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/FeatureVariableUsageInstance.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | /**
24 | * Represents the value of a feature variable for a variation
25 | */
26 | @JsonIgnoreProperties(ignoreUnknown = true)
27 | public class FeatureVariableUsageInstance implements IdMapped {
28 |
29 | private final String id;
30 | private final String value;
31 |
32 | @JsonCreator
33 | public FeatureVariableUsageInstance(@JsonProperty("id") String id,
34 | @JsonProperty("value") String value) {
35 | this.id = id;
36 | this.value = value;
37 | }
38 |
39 | public String getId() {
40 | return id;
41 | }
42 |
43 | public String getValue() {
44 | return value;
45 | }
46 |
47 | @Override
48 | public boolean equals(Object o) {
49 | if (this == o) return true;
50 | if (o == null || getClass() != o.getClass()) return false;
51 |
52 | FeatureVariableUsageInstance that = (FeatureVariableUsageInstance) o;
53 |
54 | return id.equals(that.id) && value.equals(that.value);
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | int result = id.hashCode();
60 | result = 31 * result + value.hashCode();
61 | return result;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/IdKeyMapped.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | /**
20 | * Represents that an implementing class provides a {@literal key <-> id} mapping.
21 | */
22 | public interface IdKeyMapped extends IdMapped {
23 | String getKey();
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/IdMapped.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | /**
20 | * Base interface to help with templated convenience mappings.
21 | */
22 | public interface IdMapped {
23 | String getId();
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/Integration.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | import javax.annotation.Nonnull;
24 | import javax.annotation.Nullable;
25 | import javax.annotation.concurrent.Immutable;
26 |
27 | /**
28 | * Represents the Optimizely Integration configuration.
29 | *
30 | * @see Project JSON
31 | */
32 | @Immutable
33 | @JsonIgnoreProperties(ignoreUnknown = true)
34 | public class Integration {
35 | private final String key;
36 | private final String host;
37 | private final String publicKey;
38 |
39 | @JsonCreator
40 | public Integration(@JsonProperty("key") String key,
41 | @JsonProperty("host") String host,
42 | @JsonProperty("publicKey") String publicKey) {
43 | this.key = key;
44 | this.host = host;
45 | this.publicKey = publicKey;
46 | }
47 |
48 | @Nonnull
49 | public String getKey() {
50 | return key;
51 | }
52 |
53 | @Nullable
54 | public String getHost() { return host; }
55 |
56 | @Nullable
57 | public String getPublicKey() { return publicKey; }
58 |
59 | @Override
60 | public String toString() {
61 | return "Integration{" +
62 | "key='" + key + '\'' +
63 | ((this.host != null) ? (", host='" + host + '\'') : "") +
64 | ((this.publicKey != null) ? (", publicKey='" + publicKey + '\'') : "") +
65 | '}';
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/ProjectConfigManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, 2023, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | public interface ProjectConfigManager {
22 | /**
23 | * Implementations of this method should block until a datafile is available.
24 | *
25 | * @return ProjectConfig
26 | */
27 | ProjectConfig getConfig();
28 |
29 | /**
30 | * Implementations of this method should not block until a datafile is available, instead return current cached project configuration.
31 | * return null if ProjectConfig is not ready at the moment.
32 | *
33 | * NOTE: To use ODP segments, implementation of this function is required to return current project configuration.
34 | * @return ProjectConfig
35 | */
36 | @Nullable
37 | ProjectConfig getCachedConfig();
38 |
39 | /**
40 | * Implementations of this method should return SDK key. If there is no SDKKey then it should return null.
41 | *
42 | * NOTE: To update ODP segments configuration via polling, it is required to return sdkKey.
43 | * @return String
44 | */
45 | @Nullable
46 | String getSDKKey();
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/Rollout.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | import javax.annotation.concurrent.Immutable;
24 | import java.util.List;
25 |
26 | /**
27 | * Represents a Optimizely Rollout configuration
28 | *
29 | * @see Project JSON
30 | */
31 | @Immutable
32 | @JsonIgnoreProperties(ignoreUnknown = true)
33 | public class Rollout implements IdMapped {
34 |
35 | private final String id;
36 | private final List experiments;
37 |
38 | @JsonCreator
39 | public Rollout(@JsonProperty("id") String id,
40 | @JsonProperty("experiments") List experiments) {
41 | this.id = id;
42 | this.experiments = experiments;
43 | }
44 |
45 | @Override
46 | public String getId() {
47 | return id;
48 | }
49 |
50 | public List getExperiments() {
51 | return experiments;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "Rollout{" +
57 | "id='" + id + '\'' +
58 | ", experiments=" + experiments +
59 | '}';
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/TrafficAllocation.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | /**
24 | * Represents the Optimizely Traffic Allocation configuration.
25 | *
26 | * @see Project JSON
27 | */
28 | @JsonIgnoreProperties(ignoreUnknown = true)
29 | public class TrafficAllocation {
30 |
31 | private final String entityId;
32 | private final int endOfRange;
33 |
34 | @JsonCreator
35 | public TrafficAllocation(@JsonProperty("entityId") String entityId,
36 | @JsonProperty("endOfRange") int endOfRange) {
37 | this.entityId = entityId;
38 | this.endOfRange = endOfRange;
39 | }
40 |
41 | public String getEntityId() {
42 | return entityId;
43 | }
44 |
45 | public int getEndOfRange() {
46 | return endOfRange;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "TrafficAllocation{" +
52 | "entityId='" + entityId + '\'' +
53 | ", endOfRange=" + endOfRange +
54 | '}';
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/AttributeType.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience;
18 |
19 | public enum AttributeType {
20 | CUSTOM_ATTRIBUTE("custom_attribute"),
21 | THIRD_PARTY_DIMENSION("third_party_dimension");
22 |
23 | private final String key;
24 |
25 | AttributeType(String key) {
26 | this.key = key;
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return key;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/Condition.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2018, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience;
18 |
19 | import com.optimizely.ab.OptimizelyUserContext;
20 | import com.optimizely.ab.config.ProjectConfig;
21 |
22 | import javax.annotation.Nullable;
23 | import java.util.List;
24 | import java.util.Map;
25 |
26 | /**
27 | * Interface implemented by all conditions condition objects to aid in condition evaluation.
28 | */
29 | public interface Condition {
30 |
31 | @Nullable
32 | Boolean evaluate(ProjectConfig config, OptimizelyUserContext user);
33 |
34 | String toJson();
35 |
36 | String getOperandOrId();
37 |
38 | List getConditions();
39 | }
40 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/EmptyCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2019, 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.audience;
17 |
18 | import com.optimizely.ab.OptimizelyUserContext;
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nullable;
22 | import java.util.Map;
23 |
24 | public class EmptyCondition extends LeafCondition {
25 | @Nullable
26 | @Override
27 | public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
28 | return true;
29 | }
30 |
31 | @Override
32 | public String toJson() { return null; }
33 |
34 | @Override
35 | public String getOperandOrId() {
36 | return null;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/LeafCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.audience;
17 |
18 | import java.util.List;
19 |
20 | public abstract class LeafCondition implements Condition {
21 |
22 | @Override
23 | public List getConditions() {
24 | return null;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/NullCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2019, 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.audience;
17 |
18 | import com.optimizely.ab.OptimizelyUserContext;
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nullable;
22 | import java.util.Map;
23 |
24 | public class NullCondition extends LeafCondition {
25 | @Nullable
26 | @Override
27 | public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
28 | return null;
29 | }
30 |
31 | @Override
32 | public String toJson() { return null; }
33 |
34 | @Override
35 | public String getOperandOrId() {
36 | return null;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/TypedAudience.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonProperty;
21 |
22 | public class TypedAudience extends Audience {
23 | @JsonCreator
24 | public TypedAudience(@JsonProperty("id") String id,
25 | @JsonProperty("name") String name,
26 | @JsonProperty("conditions") Condition conditions) {
27 | super(id, name, conditions);
28 | }
29 | }
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/DefaultMatchForLegacyAttributes.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * This is a temporary class. It mimics the current behaviour for
23 | * legacy custom attributes. This will be dropped for ExactMatch and the unit tests need to be fixed.
24 | */
25 | class DefaultMatchForLegacyAttributes implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (!(conditionValue instanceof String)) {
29 | throw new UnexpectedValueTypeException();
30 | }
31 | if (attributeValue == null) {
32 | return false;
33 | }
34 | return conditionValue.toString().equals(attributeValue.toString());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/ExactMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;
22 |
23 | /**
24 | * ExactMatch supports matching Numbers, Strings and Booleans. Numbers are first converted to doubles
25 | * before the comparison is evaluated. See {@link NumberComparator} Strings and Booleans are evaulated
26 | * via the Object equals method.
27 | */
28 | class ExactMatch implements Match {
29 | @Nullable
30 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
31 | if (attributeValue == null) return null;
32 |
33 | if (isValidNumber(attributeValue)) {
34 | if (isValidNumber(conditionValue)) {
35 | return NumberComparator.compareUnsafe(attributeValue, conditionValue) == 0;
36 | }
37 | return null;
38 | }
39 |
40 | if (!(conditionValue instanceof String || conditionValue instanceof Boolean)) {
41 | throw new UnexpectedValueTypeException();
42 | }
43 |
44 | if (attributeValue.getClass() != conditionValue.getClass()) {
45 | return null;
46 | }
47 |
48 | return conditionValue.equals(attributeValue);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/ExistsMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * ExistsMatch checks that the attribute value is NOT null.
23 | */
24 | class ExistsMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) {
27 | return attributeValue != null;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/GEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * GEMatch performs a "greater than or equal to" number comparison via {@link NumberComparator}.
23 | */
24 | class GEMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnknownValueTypeException {
27 | return NumberComparator.compare(attributeValue, conditionValue) >= 0;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/GTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * GTMatch performs a "greater than" number comparison via {@link NumberComparator}.
23 | */
24 | class GTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnknownValueTypeException {
27 | return NumberComparator.compare(attributeValue, conditionValue) > 0;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/LEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * GEMatch performs a "less than or equal to" number comparison via {@link NumberComparator}.
23 | */
24 | class LEMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnknownValueTypeException {
27 | return NumberComparator.compare(attributeValue, conditionValue) <= 0;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/LTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * GTMatch performs a "less than" number comparison via {@link NumberComparator}.
23 | */
24 | class LTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnknownValueTypeException {
27 | return NumberComparator.compare(attributeValue, conditionValue) < 0;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/Match.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | public interface Match {
22 | @Nullable
23 | Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException, UnknownValueTypeException;
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/NumberComparator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;
20 |
21 | /**
22 | * NumberComparator performs a numeric comparison. The input values are assumed to be numbers else
23 | * compare will throw an {@link UnknownValueTypeException}.
24 | */
25 | public class NumberComparator {
26 | public static int compare(Object o1, Object o2) throws UnknownValueTypeException {
27 | if (!isValidNumber(o1) || !isValidNumber(o2)) {
28 | throw new UnknownValueTypeException();
29 | }
30 |
31 | return compareUnsafe(o1, o2);
32 | }
33 |
34 | /**
35 | * compareUnsafe is provided to avoid checking the input values are numbers. It's assumed that the inputs
36 | * are known to be Numbers.
37 | */
38 | static int compareUnsafe(Object o1, Object o2) {
39 | return Double.compare(((Number) o1).doubleValue(), ((Number) o2).doubleValue());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionEqualsMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionEqualsMatch performs a equality comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionEqualsMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) == 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionGEMatch performs a "greater than or equal to" comparison
23 | * via {@link SemanticVersion#compare(Object, Object)}.
24 | */
25 | class SemanticVersionGEMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
29 | return SemanticVersion.compare(attributeValue, conditionValue) >= 0;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionGTMatch performs a "greater than" comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionGTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) > 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionLEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionLEMatch performs a "less than or equal to" comparison
23 | * via {@link SemanticVersion#compare(Object, Object)}.
24 | */
25 | class SemanticVersionLEMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
29 | return SemanticVersion.compare(attributeValue, conditionValue) <= 0;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionLTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionLTMatch performs a "less than" comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionLTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) < 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SubstringMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SubstringMatch checks if the attribute value contains the condition value.
23 | * This assumes both the condition and attribute values are provided as Strings.
24 | */
25 | class SubstringMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (!(conditionValue instanceof String)) {
29 | throw new UnexpectedValueTypeException();
30 | }
31 |
32 | if (!(attributeValue instanceof String)) {
33 | return null;
34 | }
35 |
36 | try {
37 | return attributeValue.toString().contains(conditionValue.toString());
38 | } catch (Exception e) {
39 | return null;
40 | }
41 | }
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/UnexpectedValueTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.config.audience.match;
19 |
20 | /**
21 | * UnexpectedValueTypeException is thrown when the condition value found in the datafile is
22 | * not one of an expected type for this version of the SDK.
23 | */
24 | public class UnexpectedValueTypeException extends Exception {
25 | private static String message = "has an unsupported condition value. You may need to upgrade to a newer release of the Optimizely SDK.";
26 |
27 | public UnexpectedValueTypeException() {
28 | super(message);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/UnknownMatchTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.config.audience.match;
19 |
20 | /**
21 | * UnknownMatchTypeException is thrown when the specified match type cannot be mapped via the MatchRegistry.
22 | */
23 | public class UnknownMatchTypeException extends Exception {
24 | private static String message = "uses an unknown match type. You may need to upgrade to a newer release of the Optimizely SDK.";
25 |
26 | public UnknownMatchTypeException() {
27 | super(message);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/UnknownValueTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.config.audience.match;
19 |
20 | /**
21 | * UnknownValueTypeException is thrown when the passed in value for a user attribute does
22 | * not map to a known allowable type.
23 | */
24 | public class UnknownValueTypeException extends Exception {
25 | private static String message = "has an unsupported attribute value.";
26 |
27 | public UnknownValueTypeException() {
28 | super(message);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/AudienceJacksonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.fasterxml.jackson.core.JsonParser;
20 | import com.fasterxml.jackson.core.ObjectCodec;
21 | import com.fasterxml.jackson.databind.DeserializationContext;
22 | import com.fasterxml.jackson.databind.JsonDeserializer;
23 | import com.fasterxml.jackson.databind.JsonNode;
24 | import com.fasterxml.jackson.databind.ObjectMapper;
25 | import com.optimizely.ab.config.audience.*;
26 |
27 | import java.io.IOException;
28 |
29 | public class AudienceJacksonDeserializer extends JsonDeserializer {
30 | private ObjectMapper objectMapper;
31 |
32 | public AudienceJacksonDeserializer() {
33 | this(new ObjectMapper());
34 | }
35 |
36 | AudienceJacksonDeserializer(ObjectMapper objectMapper) {
37 | this.objectMapper = objectMapper;
38 | }
39 |
40 | @Override
41 | public Audience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
42 | ObjectCodec codec = parser.getCodec();
43 | JsonNode node = codec.readTree(parser);
44 |
45 | String id = node.get("id").textValue();
46 | String name = node.get("name").textValue();
47 |
48 | JsonNode conditionsJson = node.get("conditions");
49 | conditionsJson = objectMapper.readTree(conditionsJson.textValue());
50 |
51 | Condition conditions = ConditionJacksonDeserializer.parseCondition(UserAttribute.class, objectMapper, conditionsJson);
52 |
53 | return new Audience(id, name, conditions);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/ConfigParseException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | /**
20 | * Wrapper around all types of JSON parser exceptions.
21 | */
22 | public final class ConfigParseException extends Exception {
23 | public ConfigParseException(String message) {
24 | super(message);
25 | }
26 |
27 | public ConfigParseException(String message, Throwable cause) {
28 | super(message, cause);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/ConfigParser.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017,2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nonnull;
22 |
23 | /**
24 | * Config parser wrapper to allow multiple library implementations to be used.
25 | *
26 | * @see GsonConfigParser
27 | * @see JacksonConfigParser
28 | * @see JsonConfigParser
29 | * @see JsonConfigParser
30 | *
31 | * @see Project JSON
32 | */
33 | public interface ConfigParser {
34 |
35 | /**
36 | * @param json The json to parse
37 | * @return The {@code ProjectConfig} configuration from the provided json
38 | * @throws ConfigParseException when there's an issue parsing the provided project config
39 | */
40 | ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException;
41 |
42 | /**
43 | * OptimizelyJSON parsing
44 | *
45 | * @param src The OptimizelyJSON
46 | * @return The serialized String
47 | * @throws JsonParseException when parsing JSON fails
48 | */
49 | String toJson(Object src) throws JsonParseException;
50 | T fromJson(String json, Class clazz) throws JsonParseException;
51 | }
52 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/ExperimentGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.google.gson.JsonDeserializationContext;
20 | import com.google.gson.JsonDeserializer;
21 | import com.google.gson.JsonElement;
22 | import com.google.gson.JsonObject;
23 | import com.google.gson.JsonParseException;
24 |
25 | import com.optimizely.ab.config.Experiment;
26 |
27 | import java.lang.reflect.Type;
28 |
29 | final class ExperimentGsonDeserializer implements JsonDeserializer {
30 |
31 | @Override
32 | public Experiment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
33 | throws JsonParseException {
34 |
35 | JsonObject jsonObject = json.getAsJsonObject();
36 |
37 | return GsonHelpers.parseExperiment(jsonObject, context);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/FeatureFlagGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.google.gson.JsonDeserializationContext;
20 | import com.google.gson.JsonDeserializer;
21 | import com.google.gson.JsonElement;
22 | import com.google.gson.JsonObject;
23 | import com.google.gson.JsonParseException;
24 | import com.optimizely.ab.config.FeatureFlag;
25 |
26 | import java.lang.reflect.Type;
27 |
28 | public class FeatureFlagGsonDeserializer implements JsonDeserializer {
29 | @Override
30 | public FeatureFlag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
31 | throws JsonParseException {
32 |
33 | JsonObject jsonObject = json.getAsJsonObject();
34 | return GsonHelpers.parseFeatureFlag(jsonObject, context);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/GroupGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.google.gson.JsonArray;
20 | import com.google.gson.JsonDeserializationContext;
21 | import com.google.gson.JsonDeserializer;
22 | import com.google.gson.JsonElement;
23 | import com.google.gson.JsonObject;
24 | import com.google.gson.JsonParseException;
25 |
26 | import com.optimizely.ab.config.Experiment;
27 | import com.optimizely.ab.config.Group;
28 | import com.optimizely.ab.config.TrafficAllocation;
29 |
30 | import java.lang.reflect.Type;
31 | import java.util.ArrayList;
32 | import java.util.List;
33 |
34 | public class GroupGsonDeserializer implements JsonDeserializer {
35 |
36 | @Override
37 | public Group deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
38 | throws JsonParseException {
39 |
40 | JsonObject jsonObject = json.getAsJsonObject();
41 |
42 | String id = jsonObject.get("id").getAsString();
43 | String policy = jsonObject.get("policy").getAsString();
44 |
45 | List experiments = new ArrayList();
46 | JsonArray experimentsJson = jsonObject.getAsJsonArray("experiments");
47 | for (Object obj : experimentsJson) {
48 | JsonObject experimentObj = (JsonObject) obj;
49 | experiments.add(GsonHelpers.parseExperiment(experimentObj, id, context));
50 | }
51 |
52 | List trafficAllocations =
53 | GsonHelpers.parseTrafficAllocation(jsonObject.getAsJsonArray("trafficAllocation"));
54 |
55 | return new Group(id, policy, experiments, trafficAllocations);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/JacksonHelpers.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.fasterxml.jackson.core.ObjectCodec;
20 | import com.fasterxml.jackson.databind.JsonNode;
21 |
22 | import java.io.IOException;
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | final class JacksonHelpers {
27 | private JacksonHelpers() {
28 | }
29 |
30 | static List arrayNodeToList(JsonNode arrayNode, Class itemClass, ObjectCodec codec) throws IOException {
31 | if (arrayNode == null || arrayNode.isNull() || !arrayNode.isArray()) {
32 | return null;
33 | }
34 |
35 | List items = new ArrayList<>(arrayNode.size());
36 |
37 | for (int i = 0; i < arrayNode.size(); i++) {
38 | JsonNode itemNode = arrayNode.get(i);
39 | if (itemNode.isNull()) {
40 | continue;
41 | }
42 | items.add(codec.treeToValue(itemNode, itemClass));
43 | }
44 |
45 | return items;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/JsonParseException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | public final class JsonParseException extends Exception {
20 | public JsonParseException(String message) {
21 | super(message);
22 | }
23 |
24 | public JsonParseException(String message, Throwable cause) {
25 | super(message, cause);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/MissingJsonParserException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | /**
22 | * Exception thrown when no supported JSON parser is found.
23 | */
24 | public final class MissingJsonParserException extends OptimizelyRuntimeException {
25 | public MissingJsonParserException(String message) {
26 | super(message);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/TypedAudienceJacksonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2019, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.parser;
17 |
18 | import com.fasterxml.jackson.core.JsonParser;
19 | import com.fasterxml.jackson.core.ObjectCodec;
20 | import com.fasterxml.jackson.databind.DeserializationContext;
21 | import com.fasterxml.jackson.databind.JsonDeserializer;
22 | import com.fasterxml.jackson.databind.JsonNode;
23 | import com.fasterxml.jackson.databind.ObjectMapper;
24 | import com.optimizely.ab.config.audience.Condition;
25 | import com.optimizely.ab.config.audience.TypedAudience;
26 | import com.optimizely.ab.config.audience.UserAttribute;
27 |
28 | import java.io.IOException;
29 |
30 | public class TypedAudienceJacksonDeserializer extends JsonDeserializer {
31 | private ObjectMapper objectMapper;
32 |
33 | public TypedAudienceJacksonDeserializer() {
34 | this(new ObjectMapper());
35 | }
36 |
37 | TypedAudienceJacksonDeserializer(ObjectMapper objectMapper) {
38 | this.objectMapper = objectMapper;
39 | }
40 |
41 | @Override
42 | public TypedAudience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
43 | ObjectCodec codec = parser.getCodec();
44 | JsonNode node = codec.readTree(parser);
45 |
46 | String id = node.get("id").textValue();
47 | String name = node.get("name").textValue();
48 |
49 | JsonNode conditionsJson = node.get("conditions");
50 |
51 | Condition conditions = ConditionJacksonDeserializer.parseCondition(UserAttribute.class, objectMapper, conditionsJson);
52 |
53 | return new TypedAudience(id, name, conditions);
54 | }
55 |
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/error/ErrorHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.error;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | /**
22 | * Implementations are responsible for handling errors.
23 | */
24 | public interface ErrorHandler {
25 |
26 | void handleError(T exception) throws T;
27 | }
28 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/error/NoOpErrorHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.error;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | /**
22 | * {@link ErrorHandler} that handles an error silently
23 | */
24 | public class NoOpErrorHandler implements ErrorHandler {
25 |
26 | @Override
27 | public void handleError(T exception) {
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/error/RaiseExceptionErrorHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.error;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | /**
22 | * {@link ErrorHandler} that handles an error by raising an exception
23 | */
24 | public class RaiseExceptionErrorHandler implements ErrorHandler {
25 |
26 | @Override
27 | public void handleError(T exception) {
28 | throw exception;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/EventHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | /**
20 | * Implementations are responsible for dispatching event's to the Optimizely event end-point.
21 | */
22 | public interface EventHandler {
23 | void dispatchEvent(LogEvent logEvent) throws Exception;
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/EventProcessor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import com.optimizely.ab.event.internal.UserEvent;
20 | import com.optimizely.ab.event.internal.payload.EventBatch;
21 | import com.optimizely.ab.notification.NotificationHandler;
22 |
23 | /**
24 | * EventProcessor interface is used to provide an intermediary processing stage within
25 | * event production. It's assumed that the EventProcessor dispatches events via a provided
26 | * {@link EventHandler}.
27 | */
28 | public interface EventProcessor {
29 | void process(UserEvent userEvent);
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/ForwardingEventProcessor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import com.optimizely.ab.event.internal.EventFactory;
20 | import com.optimizely.ab.event.internal.UserEvent;
21 | import com.optimizely.ab.notification.NotificationCenter;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | /**
26 | * ForwardingEventProcessor is a basic transformation stage for converting
27 | * the event batch into a LogEvent to be dispatched.
28 | */
29 | public class ForwardingEventProcessor implements EventProcessor {
30 |
31 | private static final Logger logger = LoggerFactory.getLogger(ForwardingEventProcessor.class);
32 |
33 | private final EventHandler eventHandler;
34 | private final NotificationCenter notificationCenter;
35 |
36 | public ForwardingEventProcessor(EventHandler eventHandler, NotificationCenter notificationCenter) {
37 | this.eventHandler = eventHandler;
38 | this.notificationCenter = notificationCenter;
39 | }
40 |
41 | @Override
42 | public void process(UserEvent userEvent) {
43 | LogEvent logEvent = EventFactory.createLogEvent(userEvent);
44 |
45 | if (notificationCenter != null) {
46 | notificationCenter.send(logEvent);
47 | }
48 |
49 | try {
50 | eventHandler.dispatchEvent(logEvent);
51 | } catch(Exception e) {
52 | logger.error("Error dispatching event: {}", logEvent, e);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/NoopEventHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import org.slf4j.Logger;
20 | import org.slf4j.LoggerFactory;
21 |
22 | import java.util.Map;
23 |
24 | /**
25 | * {@link EventHandler} that logs events but does not perform any dispatching.
26 | */
27 | public class NoopEventHandler implements EventHandler {
28 |
29 | private static final Logger logger = LoggerFactory.getLogger(NoopEventHandler.class);
30 |
31 | @Override
32 | public void dispatchEvent(LogEvent logEvent) {
33 | logger.debug("Called dispatchEvent with URL: {} and params: {}", logEvent.getEndpointUrl(),
34 | logEvent.getRequestParams());
35 | }
36 | }
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/BaseEvent.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal;
18 |
19 | import java.util.UUID;
20 |
21 | /**
22 | * BaseEvent provides a GUID implementation along with a system timestamp.
23 | */
24 | public class BaseEvent {
25 |
26 | private final String uuid = UUID.randomUUID().toString();
27 | private final long timestamp = System.currentTimeMillis();
28 |
29 | public final String getUUID() {
30 | return uuid;
31 | }
32 |
33 | public final long getTimestamp() {
34 | return timestamp;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/UserEvent.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal;
18 |
19 | /**
20 | * UserEvent interface is used to identify events containing a {@link UserContext}
21 | * Examples include:
22 | *
23 | *
{@link ConversionEvent}
24 | *
{@link ImpressionEvent}
25 | *
26 | */
27 | public interface UserEvent {
28 | UserContext getUserContext();
29 | String getUUID();
30 | long getTimestamp();
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/GsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | import com.google.gson.FieldNamingPolicy;
20 | import com.google.gson.Gson;
21 | import com.google.gson.GsonBuilder;
22 |
23 | class GsonSerializer implements Serializer {
24 |
25 | private Gson gson = new GsonBuilder()
26 | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
27 | .create();
28 |
29 | public String serialize(T payload) {
30 | return gson.toJson(payload);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/JacksonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | import com.fasterxml.jackson.annotation.JsonInclude;
20 | import com.fasterxml.jackson.core.JsonProcessingException;
21 | import com.fasterxml.jackson.databind.ObjectMapper;
22 | import com.fasterxml.jackson.databind.PropertyNamingStrategy;
23 |
24 | class JacksonSerializer implements Serializer {
25 |
26 | private ObjectMapper mapper =
27 | new ObjectMapper().setPropertyNamingStrategy(
28 | PropertyNamingStrategy.SNAKE_CASE);
29 |
30 | public String serialize(T payload) {
31 | mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
32 | try {
33 | return mapper.writeValueAsString(payload);
34 | } catch (JsonProcessingException e) {
35 | throw new SerializationException("Unable to serialize payload", e);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/JsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | import org.json.JSONObject;
20 |
21 | class JsonSerializer implements Serializer {
22 |
23 | public String serialize(T payload) {
24 | JSONObject payloadJsonObject = new JSONObject(payload);
25 | String jsonResponse = payloadJsonObject.toString();
26 | StringBuilder stringBuilder = new StringBuilder();
27 |
28 | for (int i = 0; i < jsonResponse.length(); i++) {
29 | Character ch = jsonResponse.charAt(i);
30 | Character nextChar = null;
31 | if (i + 1 < jsonResponse.length()) {
32 | nextChar = jsonResponse.charAt(i + 1);
33 | }
34 | if ((Character.isLetter(ch) || Character.isDigit(ch)) && Character.isUpperCase(ch) &&
35 | ((Character.isLetter(nextChar) || Character.isDigit(nextChar)))) {
36 | stringBuilder.append('_');
37 | stringBuilder.append(Character.toLowerCase(ch));
38 | } else {
39 | stringBuilder.append(ch);
40 | }
41 | }
42 |
43 | return stringBuilder.toString();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/SerializationException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | /**
22 | * Wrapper around all types of JSON serialization exceptions.
23 | */
24 | public class SerializationException extends OptimizelyRuntimeException {
25 |
26 | public SerializationException(String message, Throwable cause) {
27 | super(message, cause);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/Serializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | public interface Serializer {
20 | String serialize(T payload) throws SerializationException;
21 | }
22 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/Cache.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | public interface Cache {
20 | int DEFAULT_MAX_SIZE = 10000;
21 | int DEFAULT_TIMEOUT_SECONDS = 600;
22 | void save(String key, T value);
23 | T lookup(String key);
24 | void reset();
25 | }
26 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/ControlAttribute.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | public enum ControlAttribute {
20 | BOT_FILTERING_ATTRIBUTE("$opt_bot_filtering"),
21 | USER_AGENT_ATTRIBUTE("$opt_user_agent"),
22 | BUCKETING_ATTRIBUTE("$opt_bucketing_id");
23 |
24 | private final String key;
25 |
26 | ControlAttribute(String key) {
27 | this.key = key;
28 | }
29 |
30 | @Override
31 | public String toString() {
32 | return key;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/InvalidAudienceCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 |
21 | public class InvalidAudienceCondition extends OptimizelyRuntimeException {
22 |
23 | public InvalidAudienceCondition(String message) {
24 | super(message);
25 | }
26 |
27 | public InvalidAudienceCondition(String message, Throwable cause) {
28 | super(message, cause);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/LoggingConstants.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | public class LoggingConstants {
20 | public static class LoggingEntityType {
21 | public static final String EXPERIMENT = "experiment";
22 | public static final String RULE = "rule";
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/NotificationRegistry.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2023, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import com.optimizely.ab.notification.NotificationCenter;
20 |
21 | import javax.annotation.Nonnull;
22 | import java.util.Map;
23 | import java.util.concurrent.ConcurrentHashMap;
24 |
25 | public class NotificationRegistry {
26 | private final static Map _notificationCenters = new ConcurrentHashMap<>();
27 |
28 | private NotificationRegistry()
29 | {
30 | }
31 |
32 | public static NotificationCenter getInternalNotificationCenter(@Nonnull String sdkKey)
33 | {
34 | NotificationCenter notificationCenter = null;
35 | if (sdkKey != null) {
36 | if (_notificationCenters.containsKey(sdkKey)) {
37 | notificationCenter = _notificationCenters.get(sdkKey);
38 | } else {
39 | notificationCenter = new NotificationCenter();
40 | _notificationCenters.put(sdkKey, notificationCenter);
41 | }
42 | }
43 | return notificationCenter;
44 | }
45 |
46 | public static void clearNotificationCenterRegistry(@Nonnull String sdkKey) {
47 | if (sdkKey != null) {
48 | _notificationCenters.remove(sdkKey);
49 | }
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/ReservedEventKey.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | public enum ReservedEventKey {
20 | REVENUE("revenue"),
21 | VALUE("value");
22 |
23 | private final String key;
24 |
25 | ReservedEventKey(String key) {
26 | this.key = key;
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return key;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/SafetyUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019,2021, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import org.slf4j.Logger;
20 | import org.slf4j.LoggerFactory;
21 |
22 | /**
23 | * Collection of utils used to prevent the Optimizely SDK from throwing or crashing the hosting application.
24 | */
25 | public class SafetyUtils {
26 |
27 | private static final Logger logger = LoggerFactory.getLogger(SafetyUtils.class);
28 |
29 | /**
30 | * Helper method which checks if Object is an instance of AutoCloseable and calls close() on it.
31 | *
32 | * @param obj The object
33 | */
34 | public static void tryClose(Object obj) {
35 | if (!(obj instanceof AutoCloseable)) {
36 | return;
37 | }
38 |
39 | try {
40 | ((AutoCloseable) obj).close();
41 | } catch (Exception e) {
42 | logger.warn("Unexpected exception on trying to close {}.", obj);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/ActivateNotificationListenerInterface.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | import com.optimizely.ab.config.Experiment;
20 | import com.optimizely.ab.config.Variation;
21 | import com.optimizely.ab.event.LogEvent;
22 |
23 | import javax.annotation.Nonnull;
24 | import java.util.Map;
25 |
26 | /**
27 | * ActivateNotificationListenerInterface provides and interface for activate event notification.
28 | *
29 | * @deprecated along with {@link ActivateNotification} and {@link ActivateNotificationListener}
30 | * and users should implement NotificationHandler<DecisionNotification> directly.
31 | */
32 | @Deprecated
33 | public interface ActivateNotificationListenerInterface {
34 | /**
35 | * onActivate called when an activate was triggered
36 | *
37 | * @param experiment - The experiment object being activated.
38 | * @param userId - The userId passed into activate.
39 | * @param attributes - The filtered attribute list passed into activate
40 | * @param variation - The variation that was returned from activate.
41 | * @param event - The impression event that was triggered.
42 | */
43 | public void onActivate(@Nonnull Experiment experiment,
44 | @Nonnull String userId,
45 | @Nonnull Map attributes,
46 | @Nonnull Variation variation,
47 | @Nonnull LogEvent event);
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/FeatureTestSourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.HashMap;
20 | import java.util.Map;
21 |
22 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
23 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
24 |
25 | public class FeatureTestSourceInfo implements SourceInfo {
26 | private String experimentKey;
27 | private String variationKey;
28 |
29 | public FeatureTestSourceInfo(String experimentKey, String variationKey) {
30 | this.experimentKey = experimentKey;
31 | this.variationKey = variationKey;
32 | }
33 |
34 | @Override
35 | public Map get() {
36 | Map sourceInfo = new HashMap<>();
37 | sourceInfo.put(EXPERIMENT_KEY, experimentKey);
38 | sourceInfo.put(VARIATION_KEY, variationKey);
39 |
40 | return sourceInfo;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/NotificationHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | /**
20 | * NotificationHandler is a generic interface Optimizely notification listeners.
21 | * This interface replaces {@link NotificationListener} which didn't provide adequate type safety.
22 | *
23 | * While this class adds generic handler implementations to be created, the domain of supported
24 | * implementations is maintained by the {@link NotificationCenter}
25 | */
26 | public interface NotificationHandler {
27 | void handle(T message) throws Exception;
28 | }
29 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/NotificationListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | /**
20 | * An interface class for Optimizely notification listeners.
21 | *
22 | * We changed this from a abstract class to a interface to support lambdas moving forward in Java 8 and beyond.
23 | *
24 | * @deprecated in favor of the {@link NotificationHandler} interface.
25 | */
26 | @Deprecated
27 | public interface NotificationListener {
28 |
29 | /**
30 | * This is the base method of notification. Implementation classes such as {@link ActivateNotificationListener}
31 | * will implement this call and provide another method with the correct parameters
32 | * Notify called when a notification is triggered via the {@link com.optimizely.ab.notification.NotificationCenter}
33 | *
34 | * @param args - variable argument list based on the type of notification.
35 | */
36 | public void notify(Object... args);
37 | }
38 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/RolloutSourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.Collections;
20 | import java.util.Map;
21 |
22 | public class RolloutSourceInfo implements SourceInfo {
23 | @Override
24 | public Map get() {
25 | return Collections.EMPTY_MAP;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/SourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.Map;
20 |
21 | public interface SourceInfo {
22 | Map get();
23 | }
24 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/TrackNotificationListenerInterface.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | import com.optimizely.ab.event.LogEvent;
20 |
21 | import javax.annotation.Nonnull;
22 | import java.util.Map;
23 |
24 | /**
25 | * TrackNotificationListenerInterface provides an interface for track event notification.
26 | *
27 | * @deprecated and users should implement NotificationHandler<TrackNotification> directly.
28 | */
29 | @Deprecated
30 | public interface TrackNotificationListenerInterface {
31 | /**
32 | * onTrack is called when a track event is triggered
33 | *
34 | * @param eventKey - The event key that was triggered.
35 | * @param userId - user id passed into track.
36 | * @param attributes - filtered attributes list after passed into track
37 | * @param eventTags - event tags if any were passed in.
38 | * @param event - The event being recorded.
39 | */
40 | public void onTrack(@Nonnull String eventKey,
41 | @Nonnull String userId,
42 | @Nonnull Map attributes,
43 | @Nonnull Map eventTags,
44 | @Nonnull LogEvent event);
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/UpdateConfigNotification.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | /**
20 | * UpdateConfigNotification signals a change in the current configuration.
21 | */
22 | public class UpdateConfigNotification {
23 | }
24 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/ODPApiManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022-2023, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp;
17 |
18 | import java.util.List;
19 | import java.util.Set;
20 |
21 | public interface ODPApiManager {
22 | List fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set segmentsToCheck);
23 |
24 | Integer sendEvents(String apiKey, String apiEndpoint, String eventPayload);
25 | }
26 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentCallback.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.odp;
18 |
19 | @FunctionalInterface
20 | public interface ODPSegmentCallback {
21 | void onCompleted(Boolean isFetchSuccessful);
22 | }
23 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentOption.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.odp;
18 |
19 | public enum ODPSegmentOption {
20 |
21 | IGNORE_CACHE,
22 |
23 | RESET_CACHE;
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/ODPUserKey.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022-2023, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.odp;
18 |
19 | public enum ODPUserKey {
20 |
21 | VUID("vuid"),
22 |
23 | FS_USER_ID("fs_user_id"),
24 |
25 | FS_USER_ID_ALIAS("fs-user-id");
26 |
27 | private final String keyString;
28 |
29 | ODPUserKey(String keyString) {
30 | this.keyString = keyString;
31 | }
32 |
33 | public String getKeyString() {
34 | return keyString;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/parser/ResponseJsonParser.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.parser;
17 |
18 | import java.util.List;
19 |
20 | public interface ResponseJsonParser {
21 | public List parseQualifiedSegments(String responseJson);
22 | }
23 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/parser/ResponseJsonParserFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.parser;
17 |
18 | import com.optimizely.ab.internal.JsonParserProvider;
19 | import com.optimizely.ab.odp.parser.impl.GsonParser;
20 | import com.optimizely.ab.odp.parser.impl.JacksonParser;
21 | import com.optimizely.ab.odp.parser.impl.JsonParser;
22 | import com.optimizely.ab.odp.parser.impl.JsonSimpleParser;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | public class ResponseJsonParserFactory {
27 | private static final Logger logger = LoggerFactory.getLogger(ResponseJsonParserFactory.class);
28 |
29 | public static ResponseJsonParser getParser() {
30 | JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser();
31 | ResponseJsonParser jsonParser = null;
32 | switch (parserProvider) {
33 | case GSON_CONFIG_PARSER:
34 | jsonParser = new GsonParser();
35 | break;
36 | case JACKSON_CONFIG_PARSER:
37 | jsonParser = new JacksonParser();
38 | break;
39 | case JSON_CONFIG_PARSER:
40 | jsonParser = new JsonParser();
41 | break;
42 | case JSON_SIMPLE_CONFIG_PARSER:
43 | jsonParser = new JsonSimpleParser();
44 | break;
45 | }
46 | logger.debug("Using " + parserProvider.toString() + " parser");
47 | return jsonParser;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/ODPJsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer;
17 |
18 | import com.optimizely.ab.odp.ODPEvent;
19 |
20 | import java.util.List;
21 |
22 | public interface ODPJsonSerializer {
23 | public String serializeEvents(List events);
24 | }
25 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/ODPJsonSerializerFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer;
17 |
18 | import com.optimizely.ab.internal.JsonParserProvider;
19 | import com.optimizely.ab.odp.serializer.impl.GsonSerializer;
20 | import com.optimizely.ab.odp.serializer.impl.JacksonSerializer;
21 | import com.optimizely.ab.odp.serializer.impl.JsonSerializer;
22 | import com.optimizely.ab.odp.serializer.impl.JsonSimpleSerializer;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | public class ODPJsonSerializerFactory {
27 | private static final Logger logger = LoggerFactory.getLogger(ODPJsonSerializerFactory.class);
28 |
29 | public static ODPJsonSerializer getSerializer() {
30 | JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser();
31 | ODPJsonSerializer jsonSerializer = null;
32 | switch (parserProvider) {
33 | case GSON_CONFIG_PARSER:
34 | jsonSerializer = new GsonSerializer();
35 | break;
36 | case JACKSON_CONFIG_PARSER:
37 | jsonSerializer = new JacksonSerializer();
38 | break;
39 | case JSON_CONFIG_PARSER:
40 | jsonSerializer = new JsonSerializer();
41 | break;
42 | case JSON_SIMPLE_CONFIG_PARSER:
43 | jsonSerializer = new JsonSimpleSerializer();
44 | break;
45 | }
46 | logger.info("Using " + parserProvider.toString() + " serializer");
47 | return jsonSerializer;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/GsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer.impl;
17 |
18 | import com.google.gson.Gson;
19 | import com.google.gson.GsonBuilder;
20 | import com.optimizely.ab.odp.ODPEvent;
21 | import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22 |
23 | import java.util.List;
24 |
25 | public class GsonSerializer implements ODPJsonSerializer {
26 | @Override
27 | public String serializeEvents(List events) {
28 | Gson gson = new GsonBuilder().serializeNulls().create();
29 | return gson.toJson(events);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JacksonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer.impl;
17 |
18 | import com.fasterxml.jackson.core.JsonProcessingException;
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.optimizely.ab.odp.ODPEvent;
21 | import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22 |
23 | import java.util.List;
24 |
25 | public class JacksonSerializer implements ODPJsonSerializer {
26 | @Override
27 | public String serializeEvents(List events) {
28 | ObjectMapper objectMapper = new ObjectMapper();
29 | try {
30 | return objectMapper.writeValueAsString(events);
31 | } catch (JsonProcessingException e) {
32 | // log error here
33 | }
34 | return null;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JsonSimpleSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer.impl;
17 |
18 | import com.optimizely.ab.odp.ODPEvent;
19 | import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
20 | import org.json.simple.JSONArray;
21 | import org.json.simple.JSONObject;
22 |
23 | import java.util.List;
24 | import java.util.Map;
25 |
26 | public class JsonSimpleSerializer implements ODPJsonSerializer {
27 | @Override
28 | public String serializeEvents(List events) {
29 | JSONArray jsonArray = new JSONArray();
30 | for (ODPEvent event: events) {
31 | JSONObject eventObject = new JSONObject();
32 | eventObject.put("type", event.getType());
33 | eventObject.put("action", event.getAction());
34 |
35 | if (event.getIdentifiers() != null) {
36 | JSONObject identifiers = new JSONObject();
37 | for (Map.Entry identifier : event.getIdentifiers().entrySet()) {
38 | identifiers.put(identifier.getKey(), identifier.getValue());
39 | }
40 | eventObject.put("identifiers", identifiers);
41 | }
42 |
43 | if (event.getData() != null) {
44 | JSONObject data = new JSONObject();
45 | for (Map.Entry dataEntry : event.getData().entrySet()) {
46 | data.put(dataEntry.getKey(), dataEntry.getValue());
47 | }
48 | eventObject.put("data", data);
49 | }
50 |
51 | jsonArray.add(eventObject);
52 | }
53 | return jsonArray.toJSONString();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyAttribute.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2021, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 | package com.optimizely.ab.optimizelyconfig;
17 |
18 | import com.optimizely.ab.config.IdKeyMapped;
19 |
20 | /**
21 | * Represents the Attribute's map {@link OptimizelyConfig}
22 | */
23 | public class OptimizelyAttribute implements IdKeyMapped {
24 |
25 | private String id;
26 | private String key;
27 |
28 | public OptimizelyAttribute(String id,
29 | String key) {
30 | this.id = id;
31 | this.key = key;
32 | }
33 |
34 | public String getId() { return id; }
35 |
36 | public String getKey() { return key; }
37 |
38 | @Override
39 | public boolean equals(Object obj) {
40 | if (obj == null || getClass() != obj.getClass()) return false;
41 | if (obj == this) return true;
42 | OptimizelyAttribute optimizelyAttribute = (OptimizelyAttribute) obj;
43 | return id.equals(optimizelyAttribute.getId()) &&
44 | key.equals(optimizelyAttribute.getKey());
45 | }
46 |
47 | @Override
48 | public int hashCode() {
49 | int hash = id.hashCode();
50 | hash = 31 * hash + key.hashCode();
51 | return hash;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyConfigManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelyconfig;
18 |
19 | public interface OptimizelyConfigManager {
20 | /**
21 | * Implementations of this method should return {@link OptimizelyConfig}
22 | *
23 | * @return {@link OptimizelyConfig}
24 | */
25 | OptimizelyConfig getOptimizelyConfig();
26 | }
27 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.optimizelydecision;
19 |
20 | public enum DecisionMessage {
21 | SDK_NOT_READY("Optimizely SDK not configured properly yet."),
22 | FLAG_KEY_INVALID("No flag was found for key \"%s\"."),
23 | VARIABLE_VALUE_INVALID("Variable value for key \"%s\" is invalid or wrong type.");
24 |
25 | private String format;
26 |
27 | DecisionMessage(String format) {
28 | this.format = format;
29 | }
30 |
31 | public String reason(Object... args){
32 | return String.format(format, args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionReasons.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelydecision;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | public class DecisionReasons {
23 |
24 | protected final List errors = new ArrayList<>();
25 | protected final List infos = new ArrayList<>();
26 |
27 | public void addError(String format, Object... args) {
28 | String message = String.format(format, args);
29 | errors.add(message);
30 | }
31 |
32 | public String addInfo(String format, Object... args) {
33 | String message = String.format(format, args);
34 | infos.add(message);
35 | return message;
36 | }
37 |
38 | public void merge(DecisionReasons target) {
39 | errors.addAll(target.errors);
40 | infos.addAll(target.infos);
41 | }
42 |
43 | public List toReport() {
44 | List reasons = new ArrayList<>(errors);
45 | reasons.addAll(infos);
46 | return reasons;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelydecision;
18 |
19 | import javax.annotation.Nonnull;
20 | import javax.annotation.Nullable;
21 |
22 | public class DecisionResponse {
23 | private T result;
24 | private DecisionReasons reasons;
25 |
26 | public DecisionResponse(@Nullable T result, @Nonnull DecisionReasons reasons) {
27 | this.result = result;
28 | this.reasons = reasons;
29 | }
30 |
31 | public static DecisionResponse responseNoReasons(@Nullable E result) {
32 | return new DecisionResponse(result, DefaultDecisionReasons.newInstance());
33 | }
34 |
35 | public static DecisionResponse nullNoReasons() {
36 | return new DecisionResponse(null, DefaultDecisionReasons.newInstance());
37 | }
38 |
39 | @Nullable
40 | public T getResult() {
41 | return result;
42 | }
43 |
44 | @Nonnull
45 | public DecisionReasons getReasons() {
46 | return reasons;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/OptimizelyDecideOption.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelydecision;
18 |
19 | public enum OptimizelyDecideOption {
20 | DISABLE_DECISION_EVENT,
21 | ENABLED_FLAGS_ONLY,
22 | IGNORE_USER_PROFILE_SERVICE,
23 | INCLUDE_REASONS,
24 | EXCLUDE_VARIABLES
25 | }
26 |
--------------------------------------------------------------------------------
/core-api/src/main/resources/.gitignore:
--------------------------------------------------------------------------------
1 | optimizely-build-version
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/OptimizelyDecisionContextTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import org.junit.Test;
20 | import static junit.framework.TestCase.assertEquals;
21 |
22 | public class OptimizelyDecisionContextTest {
23 |
24 | @Test
25 | public void initializeOptimizelyDecisionContextWithFlagKeyAndRuleKey() {
26 | String flagKey = "test-flag-key";
27 | String ruleKey = "1029384756";
28 | String expectedKey = flagKey + OptimizelyDecisionContext.OPTI_KEY_DIVIDER + ruleKey;
29 | OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
30 | assertEquals(flagKey, optimizelyDecisionContext.getFlagKey());
31 | assertEquals(ruleKey, optimizelyDecisionContext.getRuleKey());
32 | assertEquals(expectedKey, optimizelyDecisionContext.getKey());
33 | }
34 |
35 | @Test
36 | public void initializeOptimizelyDecisionContextWithFlagKey() {
37 | String flagKey = "test-flag-key";
38 | String expectedKey = flagKey + OptimizelyDecisionContext.OPTI_KEY_DIVIDER + OptimizelyDecisionContext.OPTI_NULL_RULE_KEY;
39 | OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
40 | assertEquals(flagKey, optimizelyDecisionContext.getFlagKey());
41 | assertEquals(OptimizelyDecisionContext.OPTI_NULL_RULE_KEY, optimizelyDecisionContext.getRuleKey());
42 | assertEquals(expectedKey, optimizelyDecisionContext.getKey());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/OptimizelyForcedDecisionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import org.junit.Test;
20 | import static junit.framework.TestCase.assertEquals;
21 |
22 | public class OptimizelyForcedDecisionTest {
23 |
24 | @Test
25 | public void initializeOptimizelyForcedDecision() {
26 | String variationKey = "test-variation-key";
27 | OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
28 | assertEquals(variationKey, optimizelyForcedDecision.getVariationKey());
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/categories/ExhaustiveTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.categories;
18 |
19 | /**
20 | * Tests that may be extremely slow because they're exhaustively testing some parameter space.
21 | */
22 | public interface ExhaustiveTest {
23 | }
24 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/config/AtomicProjectConfigManagerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import org.junit.Before;
20 | import org.junit.Test;
21 |
22 | import static com.optimizely.ab.config.DatafileProjectConfigTestUtils.validConfigJsonV4;
23 | import static org.junit.Assert.*;
24 |
25 | public class AtomicProjectConfigManagerTest {
26 |
27 | private AtomicProjectConfigManager projectConfigManager;
28 |
29 | @Before
30 | public void setUp() {
31 | projectConfigManager = new AtomicProjectConfigManager();
32 | }
33 |
34 | @Test
35 | public void testGetAndSetConfig() throws Exception {
36 | assertNull(projectConfigManager.getConfig());
37 |
38 | ProjectConfig projectConfig = new DatafileProjectConfig.Builder().withDatafile(validConfigJsonV4()).build();
39 | projectConfigManager.setConfig(projectConfig);
40 | assertEquals(projectConfig, projectConfigManager.getConfig());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/config/VariationTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import org.junit.Test;
20 |
21 | import static org.hamcrest.CoreMatchers.is;
22 | import static org.hamcrest.MatcherAssert.assertThat;
23 |
24 | /**
25 | * Tests for {@link Variation}.
26 | */
27 | public class VariationTest {
28 |
29 | /**
30 | * Not a grammatical error: verify that {@link Variation#is(String)} is comparing the provided value
31 | * with that given by {@link Variation#getKey()}.
32 | */
33 | @Test
34 | public void isUsesVariationKey() throws Exception {
35 | Variation variation = new Variation("1234", "key");
36 | assertThat(variation.is("blah"), is(false));
37 |
38 | // we shouldn't be comparing the ids
39 | assertThat(variation.is("1234"), is(false));
40 |
41 | // we *should* be comparing keys
42 | assertThat(variation.is("key"), is(true));
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/config/audience/match/SubstringMatchTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import org.junit.Before;
20 | import org.junit.Test;
21 |
22 | import java.util.Arrays;
23 | import java.util.Collections;
24 | import java.util.List;
25 |
26 | import static org.junit.Assert.*;
27 |
28 | public class SubstringMatchTest {
29 |
30 | private SubstringMatch match;
31 | private static final List