Skip to content

Commit 164f6bc

Browse files
cdpreteSteKoe
andauthored
fix(#5251): expose the colors configured in the settings directly rather then calling a method which is not allowed from Thymeleaf 3.1.4 (#5285)
Co-authored-by: Stephan Köninger <stephan.koeninger@codecentric.de>
1 parent 54a0846 commit 164f6bc

9 files changed

Lines changed: 118 additions & 28 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
boot:
3+
admin:
4+
ui:
5+
theme:
6+
color: "#4A1420"
7+
palette:
8+
50: "#F8EBE4"
9+
100: "#F2D7CC"
10+
200: "#E5AC9C"
11+
300: "#D87B6C"
12+
400: "#CB463B"
13+
500: "#9F2A2A"
14+
600: "#83232A"
15+
700: "#661B26"
16+
800: "#4A1420"
17+
900: "#2E0C16"

spring-boot-admin-server-ui/src/main/frontend/public/variables.css

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
:root {
2-
--main-50: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade50())})]*/ #e8fbef;
3-
--main-100: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade100())})]*/ #d0f7df;
4-
--main-200: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade200())})]*/ #a1efbd;
5-
--main-300: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade300())})]*/ #71e69c;
6-
--main-400: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade400())})]*/ #41de7b;
7-
--main-500: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade500())})]*/ #22c55e;
8-
--main-600: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade600())})]*/ #1a9547;
9-
--main-700: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade700())})]*/ #116530;
10-
--main-800: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade800())})]*/ #09351a;
11-
--main-900: /*[(${@cssColorUtils.hexToRgb(uiSettings.theme.palette.getShade900())})]*/ #010603;
2+
--main-50: /*[(${palette.rgbColor50})]*/ #e8fbef;
3+
--main-100: /*[(${palette.rgbColor100})]*/ #d0f7df;
4+
--main-200: /*[(${palette.rgbColor200})]*/ #a1efbd;
5+
--main-300: /*[(${palette.rgbColor300})]*/ #71e69c;
6+
--main-400: /*[(${palette.rgbColor400})]*/ #41de7b;
7+
--main-500: /*[(${palette.rgbColor500})]*/ #22c55e;
8+
--main-600: /*[(${palette.rgbColor600})]*/ #1a9547;
9+
--main-700: /*[(${palette.rgbColor700})]*/ #116530;
10+
--main-800: /*[(${palette.rgbColor800})]*/ #09351a;
11+
--main-900: /*[(${palette.rgbColor900})]*/ #010603;
1212

13-
--bg-color-start: /*[(${uiSettings.theme.palette.getShade300()})]*/ #71e69c;
14-
--bg-color-stop: /*[(${uiSettings.theme.palette.getShade700()})]*/ #09351a;
13+
--bg-color-start: /*[(${uiSettings.theme.palette.shade300})]*/ #71e69c;
14+
--bg-color-stop: /*[(${uiSettings.theme.palette.shade700})]*/ #09351a;
1515
}
1616

1717
.bg-color-start {

spring-boot-admin-server-ui/src/main/java/de/codecentric/boot/admin/server/ui/config/AdminServerUiAutoConfiguration.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ public AdminServerUiAutoConfiguration(AdminServerUiProperties adminUi, AdminServ
9090
this.applicationContext = applicationContext;
9191
}
9292

93-
@Bean
94-
public CssColorUtils cssColorUtils() {
95-
return new CssColorUtils();
96-
}
97-
9893
@Bean
9994
@ConditionalOnMissingBean
10095
public UiController homeUiController(UiExtensions uiExtensions) throws IOException {

spring-boot-admin-server-ui/src/main/java/de/codecentric/boot/admin/server/ui/config/CssColorUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import java.util.regex.Pattern;
2020

21-
public class CssColorUtils {
21+
public final class CssColorUtils {
22+
23+
private CssColorUtils() {
24+
}
2225

2326
private static final Pattern HEX_RGB_PATTERN = Pattern.compile("^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
2427

25-
public String hexToRgb(String color) {
28+
public static String hexToRgb(String color) {
2629
if (!HEX_RGB_PATTERN.matcher(color).matches()) {
2730
throw new IllegalArgumentException(String.format("Invalid hex rgb format %s", color));
2831
}

spring-boot-admin-server-ui/src/main/java/de/codecentric/boot/admin/server/ui/config/ServerRuntimeHints.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ private static void registerReflectionHints(org.springframework.aot.hint.Runtime
124124
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
125125
.registerType(AdminServerUiProperties.PollTimer.class, MemberCategory.INVOKE_PUBLIC_METHODS,
126126
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
127-
.registerType(CssColorUtils.class, MemberCategory.INVOKE_PUBLIC_METHODS,
128-
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
129127
.registerType(InstanceDeregisteredEvent.class, MemberCategory.INVOKE_PUBLIC_METHODS,
130128
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
131129
.registerType(InstanceEndpointsDetectedEvent.class, MemberCategory.INVOKE_PUBLIC_METHODS,

spring-boot-admin-server-ui/src/main/java/de/codecentric/boot/admin/server/ui/web/UiController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.security.Principal;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.function.Function;
2223

2324
import com.fasterxml.jackson.annotation.JsonInclude;
2425
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -31,8 +32,10 @@
3132
import org.springframework.web.util.UriComponents;
3233
import org.springframework.web.util.UriComponentsBuilder;
3334

35+
import de.codecentric.boot.admin.server.ui.config.AdminServerUiProperties.Palette;
3436
import de.codecentric.boot.admin.server.ui.config.AdminServerUiProperties.PollTimer;
3537
import de.codecentric.boot.admin.server.ui.config.AdminServerUiProperties.UiTheme;
38+
import de.codecentric.boot.admin.server.ui.config.CssColorUtils;
3639
import de.codecentric.boot.admin.server.ui.extensions.UiExtension;
3740
import de.codecentric.boot.admin.server.ui.extensions.UiExtensions;
3841
import de.codecentric.boot.admin.server.web.AdminController;
@@ -97,6 +100,15 @@ public Map<String, Object> getUser(@Nullable Principal principal) {
97100
return emptyMap();
98101
}
99102

103+
@ModelAttribute(value = "palette", binding = false)
104+
public Map<String, String> getPalette() {
105+
return Map.ofEntries(hexToRgbColor(50, Palette::getShade50), hexToRgbColor(100, Palette::getShade100),
106+
hexToRgbColor(200, Palette::getShade200), hexToRgbColor(300, Palette::getShade300),
107+
hexToRgbColor(400, Palette::getShade400), hexToRgbColor(500, Palette::getShade500),
108+
hexToRgbColor(600, Palette::getShade600), hexToRgbColor(700, Palette::getShade700),
109+
hexToRgbColor(800, Palette::getShade800), hexToRgbColor(900, Palette::getShade900));
110+
}
111+
100112
@GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
101113
@RegisterReflectionForBinding(String.class)
102114
public String index() {
@@ -113,6 +125,11 @@ public String variablesCss() {
113125
return "variables.css";
114126
}
115127

128+
private Map.Entry<String, String> hexToRgbColor(int grade, Function<Palette, String> hexColorGetter) {
129+
return Map.entry(("rgbColor" + grade).intern(),
130+
CssColorUtils.hexToRgb(hexColorGetter.apply(uiSettings.getTheme().getPalette())));
131+
}
132+
116133
@GetMapping(path = "/login", produces = MediaType.TEXT_HTML_VALUE)
117134
public String login() {
118135
return "login";

spring-boot-admin-server-ui/src/test/java/de/codecentric/boot/admin/server/ui/AbstractAdminUiApplicationTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@ public void should_return_defaults_for_pollTimers() {
150150
//@formatter:on
151151
}
152152

153+
@Test
154+
public void should_render_palette_colors_in_variables_css() {
155+
//@formatter:off
156+
this.webClient.get()
157+
.uri("/variables.css")
158+
.accept(MediaType.parseMediaType("text/css"), MediaType.ALL)
159+
.exchange()
160+
.expectStatus().isOk()
161+
.expectHeader().contentTypeCompatibleWith(MediaType.parseMediaType("text/css"))
162+
.expectBody(String.class)
163+
.value((body) -> assertThat(body).contains("--main-50: rgb(238, 252, 250)"))
164+
.value((body) -> assertThat(body).contains("--main-100: rgb(217, 247, 244)"))
165+
.value((body) -> assertThat(body).contains("--main-200: rgb(183, 240, 234)"))
166+
.value((body) -> assertThat(body).contains("--main-300: rgb(145, 232, 224)"))
167+
.value((body) -> assertThat(body).contains("--main-400: rgb(107, 224, 213)"))
168+
.value((body) -> assertThat(body).contains("--main-500: rgb(71, 217, 203)"))
169+
.value((body) -> assertThat(body).contains("--main-600: rgb(39, 190, 175)"))
170+
.value((body) -> assertThat(body).contains("--main-700: rgb(30, 144, 132)"))
171+
.value((body) -> assertThat(body).contains("--main-800: rgb(20, 97, 90)"))
172+
.value((body) -> assertThat(body).contains("--main-900: rgb(10, 47, 43)"))
173+
.value((body) -> assertThat(body).contains("--bg-color-start: #91E8E0"))
174+
.value((body) -> assertThat(body).contains("--bg-color-stop: #1E9084"));
175+
//@formatter:on
176+
}
177+
153178
protected WebTestClient createWebClient(int port) {
154179
return WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
155180
}

spring-boot-admin-server-ui/src/test/java/de/codecentric/boot/admin/server/ui/config/CssColorUtilsTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ class CssColorUtilsTest implements WithAssertions {
2323

2424
@Test
2525
void hexToRgb() {
26-
CssColorUtils cssColorUtils = new CssColorUtils();
27-
28-
assertThat(cssColorUtils.hexToRgb(new AdminServerUiProperties.Palette().getShade50()))
26+
assertThat(CssColorUtils.hexToRgb(new AdminServerUiProperties.Palette().getShade50()))
2927
.isEqualTo("rgb(238, 252, 250)");
3028
}
3129

3230
@Test
3331
void hexToRgb_throws_exception_on_invalid_format() {
34-
CssColorUtils cssColorUtils = new CssColorUtils();
35-
36-
assertThatThrownBy(() -> cssColorUtils.hexToRgb("EEFCFA")).isInstanceOf(IllegalArgumentException.class);
32+
assertThatThrownBy(() -> CssColorUtils.hexToRgb("EEFCFA")).isInstanceOf(IllegalArgumentException.class);
3733
}
3834

3935
}

spring-boot-admin-server-ui/src/test/java/de/codecentric/boot/admin/server/ui/web/UiControllerTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
package de.codecentric.boot.admin.server.ui.web;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.params.ParameterizedTest;
2324
import org.junit.jupiter.params.provider.CsvSource;
2425
import org.springframework.test.web.servlet.MockMvc;
2526
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2627

28+
import de.codecentric.boot.admin.server.ui.config.AdminServerUiProperties;
29+
import de.codecentric.boot.admin.server.ui.config.CssColorUtils;
2730
import de.codecentric.boot.admin.server.ui.extensions.UiExtensions;
2831
import de.codecentric.boot.admin.server.web.servlet.AdminControllerHandlerMapping;
2932

33+
import static org.assertj.core.api.Assertions.assertThat;
3034
import static org.junit.jupiter.api.Assertions.fail;
3135
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3236
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
@@ -98,8 +102,43 @@ void should_validate_external_views(String label, String url, boolean hasChildre
98102
}
99103
}
100104

105+
@Test
106+
void should_populate_palette_with_matching_rgb_colors_for_all_grades() {
107+
AdminServerUiProperties.Palette palette = new AdminServerUiProperties.Palette();
108+
palette.set50("#010203");
109+
palette.set100("#111213");
110+
palette.set200("#212223");
111+
palette.set300("#313233");
112+
palette.set400("#414243");
113+
palette.set500("#515253");
114+
palette.set600("#616263");
115+
palette.set700("#717273");
116+
palette.set800("#818283");
117+
palette.set900("#919293");
118+
119+
AdminServerUiProperties.UiTheme theme = new AdminServerUiProperties.UiTheme();
120+
theme.setPalette(palette);
121+
122+
UiController.Settings settings = UiController.Settings.builder().theme(theme).build();
123+
UiController controller = new UiController("", UiExtensions.EMPTY, settings);
124+
125+
Map<String, String> actualPalette = controller.getPalette();
126+
127+
assertThat(actualPalette)
128+
.containsExactlyInAnyOrderEntriesOf(Map.of("rgbColor50", CssColorUtils.hexToRgb(palette.getShade50()),
129+
"rgbColor100", CssColorUtils.hexToRgb(palette.getShade100()), "rgbColor200",
130+
CssColorUtils.hexToRgb(palette.getShade200()), "rgbColor300",
131+
CssColorUtils.hexToRgb(palette.getShade300()), "rgbColor400",
132+
CssColorUtils.hexToRgb(palette.getShade400()), "rgbColor500",
133+
CssColorUtils.hexToRgb(palette.getShade500()), "rgbColor600",
134+
CssColorUtils.hexToRgb(palette.getShade600()), "rgbColor700",
135+
CssColorUtils.hexToRgb(palette.getShade700()), "rgbColor800",
136+
CssColorUtils.hexToRgb(palette.getShade800()), "rgbColor900",
137+
CssColorUtils.hexToRgb(palette.getShade900())));
138+
}
139+
101140
private MockMvc setupController(String publicUrl, List<UiController.ExternalView> externalViews) {
102-
var uiControllerSettings = UiController.Settings.builder();
141+
var uiControllerSettings = UiController.Settings.builder().theme(new AdminServerUiProperties.UiTheme());
103142
if (!isEmpty(externalViews)) {
104143
uiControllerSettings.externalViews(externalViews);
105144
}

0 commit comments

Comments
 (0)