|
1 | 1 | package com.flagsmith; |
2 | 2 |
|
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; |
5 | 23 |
|
6 | | -@RestController |
| 24 | +@Controller |
7 | 25 | public class HelloController { |
| 26 | + private static volatile ObjectMapper mapper = null; |
8 | 27 |
|
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")) |
11 | 32 | .build(); |
12 | 33 |
|
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; |
16 | 43 |
|
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 | + } |
18 | 97 | } |
19 | 98 | } |
0 commit comments