Skip to content

Commit 5919af8

Browse files
Merge pull request #54 from Flagsmith/release/5.0.0
Release version 5.0.0
2 parents 40bf983 + 16635f3 commit 5919af8

97 files changed

Lines changed: 5106 additions & 3518 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/maven.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ jobs:
3030

3131
steps:
3232
- name: Checkout repository
33-
uses: actions/checkout@v2
33+
uses: actions/checkout@v2.3.1
34+
with:
35+
submodules: recursive
3436

3537
- name: Set up JDK (${{ matrix.java }} ${{ matrix.distribution }})
3638
uses: actions/setup-java@v2

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
2+
path = src/test/java/com/flagsmith/flagengine/enginetestdata
3+
url = git@github.com:Flagsmith/engine-test-data.git

example/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
# Example Spring Boot Application
1+
# Flagsmith Java Example
22

3-
This is just a really simple Spring Boot application that demonstrates a super basic implementation of Flagsmith.
3+
This directory contains a basic Spring Boot application which utilises Flagsmith. To run the example application, you'll
4+
need to go through the following steps:
5+
6+
1. Create an account, organisation and project on [Flagsmith](https://flagsmith.com)
7+
2. Create a feature in the project called "secret_button"
8+
3. Give the feature a value using the json editor as follows:
9+
10+
```json
11+
{"colour": "#ababab"}
12+
```
13+
14+
15+
4. Set environment variable `FLAGSMITH_API_KEY` with your Flagsmith API key.
16+
5. If you are facing issues with maven dependency for flagsmith, you may run
17+
`mvn clean install` on the flagsmith-java-client root directory (also ../).
18+
6. Run this project using mvn spring-boot:run.
19+
7. The project is accessible at http://localhost:8080/
20+
21+
Now you can play around with the 'secret_button' feature in flagsmith, turn it on to show it and edit the colour in the
22+
json value to edit the colour of the button. You can also identify as a given user and then update the settings for the
23+
secret button feature for that user in the flagsmith interface to see the affect that has too.

example/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<java.version>1.8</java.version>
1919
</properties>
2020
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
24+
</dependency>
2125
<dependency>
2226
<groupId>org.springframework.boot</groupId>
2327
<artifactId>spring-boot-starter-web</artifactId>
@@ -30,7 +34,7 @@
3034
<dependency>
3135
<groupId>com.flagsmith</groupId>
3236
<artifactId>flagsmith-java-client</artifactId>
33-
<version>2.3</version>
37+
<version>5.0.0</version>
3438
</dependency>
3539
<dependency>
3640
<groupId>org.springframework.boot</groupId>

example/src/main/java/com/flagsmith/Application.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public class Application {
99
public static void main(String[] args) {
1010
SpringApplication.run(Application.class, args);
1111
}
12+
1213
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.flagsmith;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class FontColour {
6+
@JsonProperty("colour")
7+
private String colour;
8+
9+
public String getColour() {
10+
return colour;
11+
}
12+
13+
public void setColour(String colour) {
14+
this.colour = colour;
15+
}
16+
}
Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,98 @@
11
package com.flagsmith;
22

3-
import org.springframework.web.bind.annotation.RestController;
4-
import org.springframework.web.bind.annotation.RequestMapping;
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.DeserializationFeature;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.MapperFeature;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import com.fasterxml.jackson.databind.node.TextNode;
11+
import com.flagsmith.exceptions.FlagsmithApiError;
12+
import com.flagsmith.exceptions.FlagsmithClientError;
13+
import com.flagsmith.models.DefaultFlag;
14+
import com.flagsmith.models.Flags;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import org.apache.commons.lang3.StringUtils;
18+
import org.springframework.stereotype.Controller;
19+
import org.springframework.web.bind.annotation.GetMapping;
20+
import org.springframework.web.bind.annotation.RequestParam;
21+
import org.springframework.web.bind.annotation.ResponseBody;
22+
import org.springframework.web.servlet.ModelAndView;
523

6-
@RestController
24+
@Controller
725
public class HelloController {
26+
private static volatile ObjectMapper mapper = null;
827

9-
private static FlagsmithClient flagsmith = FlagsmithClient.newBuilder()
10-
.setApiKey("NowEDzKzNJXZVTVanLVdMQ")
28+
private static FlagsmithClient flagsmith = FlagsmithClient
29+
.newBuilder()
30+
.setDefaultFlagValueFunction(HelloController::defaultFlagHandler)
31+
.setApiKey(System.getenv("FLAGSMITH_API_KEY"))
1132
.build();
1233

13-
@RequestMapping("/")
14-
public String index() {
15-
String fontSize = flagsmith.getFeatureFlagValue("font_size");
34+
@GetMapping("")
35+
@ResponseBody
36+
public ModelAndView index(
37+
@RequestParam(name = "identifier", defaultValue = "") String identifier,
38+
@RequestParam(name = "trait-key", defaultValue = "") String traitKey,
39+
@RequestParam(name = "trait-value", defaultValue = "") String traitValue
40+
) throws FlagsmithApiError, FlagsmithClientError {
41+
String featureName = "secret_button";
42+
Flags flags;
1643

17-
return "Greetings from Spring Boot! Font size is " + fontSize;
44+
if (!StringUtils.isBlank(identifier)) {
45+
Map<String, Object> traits = new HashMap<String, Object>();
46+
if (!StringUtils.isBlank(traitKey) && !StringUtils.isBlank(traitValue)) {
47+
traits.put(traitKey, traitValue);
48+
}
49+
flags = flagsmith.getIdentityFlags(identifier, traits);
50+
} else {
51+
flags = flagsmith.getEnvironmentFlags();
52+
}
53+
54+
Boolean showButton = flags.isFeatureEnabled(featureName);
55+
Object value = flags.getFeatureValue(featureName);
56+
String buttonValue = value instanceof String ? (String) value : ((TextNode) value).textValue();
57+
58+
FontColour fontColor = parse(buttonValue, FontColour.class);
59+
60+
ModelAndView view = new ModelAndView();
61+
view.setViewName("index");
62+
view.addObject("show_button", showButton);
63+
view.addObject("font_colour", fontColor.getColour());
64+
return view;
65+
}
66+
67+
private static DefaultFlag defaultFlagHandler(String featureName) {
68+
DefaultFlag flag = new DefaultFlag();
69+
flag.setEnabled(Boolean.FALSE);
70+
71+
if (featureName.equals("secret_button")) {
72+
flag.setValue("{\"colour\": \"#ababab\"}");
73+
} else {
74+
flag.setValue(null);
75+
}
76+
77+
return flag;
78+
}
79+
80+
private static ObjectMapper getMapper() {
81+
if (null == mapper) {
82+
mapper = new ObjectMapper();
83+
mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
84+
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
85+
}
86+
87+
return mapper;
88+
}
89+
90+
private <T> T parse(String data, Class<T> clazz) {
91+
try {
92+
JsonNode json = getMapper().readTree(data);
93+
return getMapper().treeToValue(json, clazz);
94+
} catch (JsonProcessingException e) {
95+
return null;
96+
}
1897
}
1998
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Flagsmith Test</title>
6+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
7+
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
8+
crossorigin="anonymous"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Hello <span id="identifier-name" th:text="${identifier} ? identifier : 'World'">World!</span>,
13+
</p>
14+
<button
15+
id="secret-button"
16+
th:if="${show_button}"
17+
th:style="'color: ' + @{${font_colour}} + ';'"
18+
>Secret Button</button>
19+
<p>
20+
<form id="form" method="get">
21+
<h3>Identify as a user</h3>
22+
<label for="identifier">Identifier: </label>
23+
<input name="identifier" id="identifier"/>
24+
<br />
25+
<p>... with an optional user trait</p>
26+
<label for="trait-key">Trait Key: </label>
27+
<input name="trait-key" id="trait-key"/><br />
28+
29+
<label for="trait-value">Trait Value: </label>
30+
<input name="trait-value" id="trait-value"/><br />
31+
32+
<button type="submit">Identify!</button>
33+
</form>
34+
</p>
35+
</body>
36+
</html>

google_checks_v8.19.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,6 @@
254254
<property name="exceptionVariableName" value="expected"/>
255255
</module>
256256
<module name="CommentsIndentation"/>
257+
<module name="SuppressionCommentFilter"/>
257258
</module>
258259
</module>

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.flagsmith</groupId>
88
<artifactId>flagsmith-java-client</artifactId>
9-
<version>4.0.2</version>
9+
<version>5.0.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Flagsmith Java Client</name>
@@ -90,6 +90,11 @@
9090
<artifactId>slf4j-api</artifactId>
9191
<version>${slf4j.version}</version>
9292
</dependency>
93+
<dependency>
94+
<groupId>org.apache.maven</groupId>
95+
<artifactId>maven-artifact</artifactId>
96+
<version>3.6.3</version>
97+
</dependency>
9398

9499
<!-- Test Dependencies-->
95100
<dependency>

0 commit comments

Comments
 (0)