Skip to content

Commit

Permalink
Merge pull request #54 from Flagsmith/release/5.0.0
Browse files Browse the repository at this point in the history
Release version 5.0.0
  • Loading branch information
matthewelwell authored Jun 7, 2022
2 parents 40bf983 + 16635f3 commit 5919af8
Show file tree
Hide file tree
Showing 97 changed files with 5,106 additions and 3,518 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v2.3.1
with:
submodules: recursive

- name: Set up JDK (${{ matrix.java }} ${{ matrix.distribution }})
uses: actions/setup-java@v2
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
path = src/test/java/com/flagsmith/flagengine/enginetestdata
url = git@github.com:Flagsmith/engine-test-data.git
24 changes: 22 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Example Spring Boot Application
# Flagsmith Java Example

This is just a really simple Spring Boot application that demonstrates a super basic implementation of Flagsmith.
This directory contains a basic Spring Boot application which utilises Flagsmith. To run the example application, you'll
need to go through the following steps:

1. Create an account, organisation and project on [Flagsmith](https://flagsmith.com)
2. Create a feature in the project called "secret_button"
3. Give the feature a value using the json editor as follows:

```json
{"colour": "#ababab"}
```


4. Set environment variable `FLAGSMITH_API_KEY` with your Flagsmith API key.
5. If you are facing issues with maven dependency for flagsmith, you may run
`mvn clean install` on the flagsmith-java-client root directory (also ../).
6. Run this project using mvn spring-boot:run.
7. The project is accessible at http://localhost:8080/

Now you can play around with the 'secret_button' feature in flagsmith, turn it on to show it and edit the colour in the
json value to edit the colour of the button. You can also identify as a given user and then update the settings for the
secret button feature for that user in the flagsmith interface to see the affect that has too.
6 changes: 5 additions & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -30,7 +34,7 @@
<dependency>
<groupId>com.flagsmith</groupId>
<artifactId>flagsmith-java-client</artifactId>
<version>2.3</version>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
1 change: 1 addition & 0 deletions example/src/main/java/com/flagsmith/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
16 changes: 16 additions & 0 deletions example/src/main/java/com/flagsmith/FontColour.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.flagsmith;

import com.fasterxml.jackson.annotation.JsonProperty;

public class FontColour {
@JsonProperty("colour")
private String colour;

public String getColour() {
return colour;
}

public void setColour(String colour) {
this.colour = colour;
}
}
97 changes: 88 additions & 9 deletions example/src/main/java/com/flagsmith/HelloController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,98 @@
package com.flagsmith;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.flagsmith.exceptions.FlagsmithApiError;
import com.flagsmith.exceptions.FlagsmithClientError;
import com.flagsmith.models.DefaultFlag;
import com.flagsmith.models.Flags;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@RestController
@Controller
public class HelloController {
private static volatile ObjectMapper mapper = null;

private static FlagsmithClient flagsmith = FlagsmithClient.newBuilder()
.setApiKey("NowEDzKzNJXZVTVanLVdMQ")
private static FlagsmithClient flagsmith = FlagsmithClient
.newBuilder()
.setDefaultFlagValueFunction(HelloController::defaultFlagHandler)
.setApiKey(System.getenv("FLAGSMITH_API_KEY"))
.build();

@RequestMapping("/")
public String index() {
String fontSize = flagsmith.getFeatureFlagValue("font_size");
@GetMapping("")
@ResponseBody
public ModelAndView index(
@RequestParam(name = "identifier", defaultValue = "") String identifier,
@RequestParam(name = "trait-key", defaultValue = "") String traitKey,
@RequestParam(name = "trait-value", defaultValue = "") String traitValue
) throws FlagsmithApiError, FlagsmithClientError {
String featureName = "secret_button";
Flags flags;

return "Greetings from Spring Boot! Font size is " + fontSize;
if (!StringUtils.isBlank(identifier)) {
Map<String, Object> traits = new HashMap<String, Object>();
if (!StringUtils.isBlank(traitKey) && !StringUtils.isBlank(traitValue)) {
traits.put(traitKey, traitValue);
}
flags = flagsmith.getIdentityFlags(identifier, traits);
} else {
flags = flagsmith.getEnvironmentFlags();
}

Boolean showButton = flags.isFeatureEnabled(featureName);
Object value = flags.getFeatureValue(featureName);
String buttonValue = value instanceof String ? (String) value : ((TextNode) value).textValue();

FontColour fontColor = parse(buttonValue, FontColour.class);

ModelAndView view = new ModelAndView();
view.setViewName("index");
view.addObject("show_button", showButton);
view.addObject("font_colour", fontColor.getColour());
return view;
}

private static DefaultFlag defaultFlagHandler(String featureName) {
DefaultFlag flag = new DefaultFlag();
flag.setEnabled(Boolean.FALSE);

if (featureName.equals("secret_button")) {
flag.setValue("{\"colour\": \"#ababab\"}");
} else {
flag.setValue(null);
}

return flag;
}

private static ObjectMapper getMapper() {
if (null == mapper) {
mapper = new ObjectMapper();
mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
}

return mapper;
}

private <T> T parse(String data, Class<T> clazz) {
try {
JsonNode json = getMapper().readTree(data);
return getMapper().treeToValue(json, clazz);
} catch (JsonProcessingException e) {
return null;
}
}
}
36 changes: 36 additions & 0 deletions example/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Flagsmith Test</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
</head>
<body>
<p>
Hello <span id="identifier-name" th:text="${identifier} ? identifier : 'World'">World!</span>,
</p>
<button
id="secret-button"
th:if="${show_button}"
th:style="'color: ' + @{${font_colour}} + ';'"
>Secret Button</button>
<p>
<form id="form" method="get">
<h3>Identify as a user</h3>
<label for="identifier">Identifier: </label>
<input name="identifier" id="identifier"/>
<br />
<p>... with an optional user trait</p>
<label for="trait-key">Trait Key: </label>
<input name="trait-key" id="trait-key"/><br />

<label for="trait-value">Trait Value: </label>
<input name="trait-value" id="trait-value"/><br />

<button type="submit">Identify!</button>
</form>
</p>
</body>
</html>
1 change: 1 addition & 0 deletions google_checks_v8.19.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,6 @@
<property name="exceptionVariableName" value="expected"/>
</module>
<module name="CommentsIndentation"/>
<module name="SuppressionCommentFilter"/>
</module>
</module>
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.flagsmith</groupId>
<artifactId>flagsmith-java-client</artifactId>
<version>4.0.2</version>
<version>5.0.0</version>
<packaging>jar</packaging>

<name>Flagsmith Java Client</name>
Expand Down Expand Up @@ -90,6 +90,11 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.6.3</version>
</dependency>

<!-- Test Dependencies-->
<dependency>
Expand Down
54 changes: 0 additions & 54 deletions src/main/java/com/flagsmith/Feature.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/main/java/com/flagsmith/FeatureUser.java

This file was deleted.

64 changes: 0 additions & 64 deletions src/main/java/com/flagsmith/Flag.java

This file was deleted.

Loading

0 comments on commit 5919af8

Please sign in to comment.