Skip to content

Commit b9e9463

Browse files
committed
feat: add minor changes
1 parent b1e4b9c commit b9e9463

File tree

3 files changed

+56
-104
lines changed

3 files changed

+56
-104
lines changed

core/src/main/java/com/linecorp/armeria/server/docs/DocServiceBuilder.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public final class DocServiceBuilder {
4444

4545
static final DocServiceFilter NO_SERVICE = (plugin, service, method) -> false;
4646

47+
private static final String WEB_APP_TITLE_KEY = "webAppTitle";
48+
49+
private static final int WEB_APP_TITLE_MAX_SIZE = 50;
50+
4751
private DocServiceFilter includeFilter = ALL_SERVICES;
4852

4953
private DocServiceFilter excludeFilter = NO_SERVICE;
@@ -558,13 +562,11 @@ private static String[] guessAndSerializeExampleRequest(Object exampleRequest) {
558562
* @return The current {@link DocServiceBuilder} instance for method chaining.
559563
*/
560564
public DocServiceBuilder webAppTitle(String webAppTitle) {
561-
final String webAppTitleKey = "webAppTitle";
562-
final Integer webAppTitleMaxSize = 50;
563-
requireNonNull(webAppTitle, webAppTitleKey);
564-
checkArgument(!webAppTitle.trim().isEmpty(), "%s is empty.", webAppTitleKey);
565-
checkArgument(webAppTitle.length() <= webAppTitleMaxSize,
566-
"%s length exceeds %s.", webAppTitleKey, webAppTitleMaxSize);
567-
docServiceExtraInfo.putIfAbsent(webAppTitleKey, webAppTitle);
565+
requireNonNull(webAppTitle, WEB_APP_TITLE_KEY);
566+
checkArgument(!webAppTitle.trim().isEmpty(), "%s is empty.", WEB_APP_TITLE_KEY);
567+
checkArgument(webAppTitle.length() <= WEB_APP_TITLE_MAX_SIZE,
568+
"%s length exceeds %s.", WEB_APP_TITLE_KEY, WEB_APP_TITLE_MAX_SIZE);
569+
docServiceExtraInfo.putIfAbsent(WEB_APP_TITLE_KEY, webAppTitle);
568570
return this;
569571
}
570572

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,45 @@
1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static java.util.Objects.requireNonNull;
2020

21-
import java.net.URI;
22-
import java.net.URISyntaxException;
2321
import java.util.regex.Pattern;
2422

25-
import com.google.common.collect.ImmutableSet;
26-
2723
/**
28-
* Util class for DocServiceBuilder#injectedScripts method.
24+
* Provides utilities for {@link DocServiceBuilder#injectedScripts(String...)}.
2925
*/
30-
public final class DocServiceInjectedScriptsUtil {
26+
public final class DocServiceInjectableScripts {
3127

32-
private static final String HEX_COLOR_PATTERN = "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$";
28+
private static final String HEX_COLOR_PATTERN = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$";
3329
private static final int MAX_COLOR_LENGTH = 7;
3430
private static final String SAFE_DOM_HOOK = "data-js-target";
35-
private static final ImmutableSet<String> ALLOWED_FAVICON_EXTENSIONS =
36-
ImmutableSet.of(".ico", ".png", ".svg");
37-
private static final ImmutableSet<String> ALLOWED_SCHEMES = ImmutableSet.of("http", "https");
31+
private static final String TITLE_BACKGROUND_KEY = "titleBackground";
32+
private static final String GOTO_BACKGROUND_KEY = "gotoBackground";
33+
private static final String FAVICON_KEY = "favicon";
3834

3935
/**
40-
* Returns a js script to change the title background color.
36+
* Returns a js script to change the title background to the specified color in hex code format.
4137
*
4238
* @param color the color string to set
4339
* @return the js script
4440
*/
45-
public static String withTitleBackground(String color) {
46-
final String titleBackgroundKey = "titleBackground";
41+
public static String titleBackground(String color) {
4742
final String targetAttr = "main-app-bar";
48-
validateHexColor(color, titleBackgroundKey);
43+
validateHexColor(color, TITLE_BACKGROUND_KEY);
4944

50-
return buildStyleScript(color, targetAttr);
45+
return buildStyleScript(checkHashtagInHexColorCode(color), targetAttr);
5146
}
5247

5348
/**
54-
* Returns a js script to change the goto component background color.
49+
* Returns a js script to change the background of the goto component to the specified color in hex code
50+
* format.
5551
*
5652
* @param color the color string to set
5753
* @return the js script
5854
*/
59-
public static String withGotoBackground(String color) {
60-
final String gotoBackgroundKey = "gotoBackground";
55+
public static String gotoBackground(String color) {
6156
final String targetAttr = "goto-app-bar";
62-
validateHexColor(color, gotoBackgroundKey);
57+
validateHexColor(color, GOTO_BACKGROUND_KEY);
6358

64-
return buildStyleScript(color, targetAttr);
59+
return buildStyleScript(checkHashtagInHexColorCode(color), targetAttr);
6560
}
6661

6762
/**
@@ -70,14 +65,13 @@ public static String withGotoBackground(String color) {
7065
* @param uri the uri string to set
7166
* @return the js script
7267
*/
73-
public static String withFavicon(String uri) {
74-
final String faviconKey = "favicon";
75-
validateFaviconUri(uri, faviconKey);
68+
public static String favicon(String uri) {
69+
validateFaviconUri(uri, FAVICON_KEY);
7670

7771
return buildFaviconScript(escapeJavaScriptUri(uri));
7872
}
7973

80-
private DocServiceInjectedScriptsUtil() {}
74+
private DocServiceInjectableScripts() {}
8175

8276
/**
8377
* Validates that the given color is a non-null, non-empty, character hex color string.
@@ -94,6 +88,20 @@ private static void validateHexColor(String color, String key) {
9488
"%s not in hex format: %s.", key, color);
9589
}
9690

91+
/**
92+
* Check if the given color starts with a hashtag char.
93+
*
94+
* @param color the color string to validate
95+
* @return hex color string with hashtag included
96+
*/
97+
private static String checkHashtagInHexColorCode(String color) {
98+
99+
if (color.startsWith("#")) {
100+
return color;
101+
}
102+
return '#' + color;
103+
}
104+
97105
/**
98106
* Builds a JavaScript snippet that sets the background color of a DOM element.
99107
*
@@ -118,38 +126,6 @@ private static String buildStyleScript(String color, String targetAttr) {
118126
private static void validateFaviconUri(String uri, String key) {
119127
requireNonNull(uri, key);
120128
checkArgument(!uri.trim().isEmpty(), "%s is empty.", key);
121-
checkArgument(isValidUri(uri), "%s uri invalid.", key);
122-
checkArgument(hasValidFaviconExtension(uri), "%s extension not allowed.",key);
123-
}
124-
125-
/**
126-
* Check if the input is a valid URI.
127-
* @param input the uri string to validate
128-
* @return true if is valid
129-
*/
130-
public static boolean isValidUri(String input) {
131-
try {
132-
final URI uri = new URI(input);
133-
final String scheme = uri.getScheme();
134-
if (scheme == null) {
135-
return true;
136-
}
137-
return ALLOWED_SCHEMES.contains(scheme.toLowerCase());
138-
} catch (URISyntaxException e) {
139-
return false;
140-
}
141-
}
142-
143-
/**
144-
* Validates the favicon extension.
145-
*
146-
* @param uri the uri string
147-
* @return the result of validation
148-
*/
149-
private static boolean hasValidFaviconExtension(String uri) {
150-
final String lowerUrl = uri.toLowerCase();
151-
return ALLOWED_FAVICON_EXTENSIONS.stream()
152-
.anyMatch(lowerUrl::endsWith);
153129
}
154130

155131
/**
Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,49 @@
2525
import org.junit.jupiter.params.provider.MethodSource;
2626
import org.junit.jupiter.params.provider.ValueSource;
2727

28-
public class DocServiceInjectedScriptsUtilTest {
28+
public class DocServiceInjectableScriptsTest {
2929

3030
@ParameterizedTest
31-
@ValueSource(strings = { "#ff0089", "#ff9dc3", "#3a3"})
32-
void withTitleBackground_givenValidColor_returnsScriptWithColor(String color) {
31+
@ValueSource(strings = { "#ff0089", "ff9dc3", "#3a3"})
32+
void titleBackground_givenValidColor_returnsScriptWithColor(String color) {
3333

34-
final String result = DocServiceInjectedScriptsUtil.withTitleBackground(color);
34+
final String result = DocServiceInjectableScripts.titleBackground(color);
3535

3636
assertThat(result).isNotBlank().contains(color);
3737
}
3838

3939
@ParameterizedTest
4040
@ValueSource(strings = { "#1234567", "#ABCDEFA", "#7654321"})
41-
void withTitleBackground_givenTooLongColor_throwsException(String color) {
41+
void titleBackground_givenTooLongColor_throwsException(String color) {
4242

43-
assertThatThrownBy(() -> DocServiceInjectedScriptsUtil.withGotoBackground(color))
43+
assertThatThrownBy(() -> DocServiceInjectableScripts.gotoBackground(color))
4444
.isInstanceOf(IllegalArgumentException.class)
4545
.hasMessageContaining("length exceeds");
4646
}
4747

4848
@ParameterizedTest
4949
@ValueSource(strings = { "#12345Z", "#ZABCDE", "#@12345"})
50-
void withTitleBackground_givenInvalidColor_throwsException(String color) {
50+
void titleBackground_givenInvalidColor_throwsException(String color) {
5151

52-
assertThatThrownBy(() -> DocServiceInjectedScriptsUtil.withGotoBackground(color))
52+
assertThatThrownBy(() -> DocServiceInjectableScripts.gotoBackground(color))
5353
.isInstanceOf(IllegalArgumentException.class)
5454
.hasMessageContaining("not in hex format");
5555
}
5656

5757
@ParameterizedTest
58-
@ValueSource(strings = { "#ff0089", "#ff9dc3", "#3a3"})
59-
void withGotoBackground_givenValidColor_returnsScriptWithColor(String color) {
58+
@ValueSource(strings = { "#ff0089", "ff9dc3", "#3a3"})
59+
void gotoBackground_givenValidColor_returnsScriptWithColor(String color) {
6060

61-
final String result = DocServiceInjectedScriptsUtil.withGotoBackground(color);
61+
final String result = DocServiceInjectableScripts.gotoBackground(color);
6262

6363
assertThat(result).isNotBlank().contains(color);
6464
}
6565

6666
@ParameterizedTest
6767
@MethodSource("getStreamOfValidUri")
68-
void withFavicon_givenValidUri_returnsScriptWithUri(String uri, String expectedUri) {
68+
void favicon_givenValidUri_returnsScriptWithUri(String uri, String expectedUri) {
6969

70-
final String result = DocServiceInjectedScriptsUtil.withFavicon(uri);
70+
final String result = DocServiceInjectableScripts.favicon(uri);
7171

7272
assertThat(result).contains(expectedUri).doesNotContain(uri);
7373
}
@@ -79,37 +79,11 @@ private static Stream<Arguments> getStreamOfValidUri() {
7979
);
8080
}
8181

82-
@ParameterizedTest
83-
@ValueSource(strings = {
84-
"javascript://armeria.dev/static/icon.svg",
85-
"data://line.com/static/icon.svg",
86-
"https://\\evil.corp/image/icon.svg"
87-
})
88-
void withFavicon_givenInvalidUri_throwsException(String uri) {
89-
90-
assertThatThrownBy(() -> DocServiceInjectedScriptsUtil.withFavicon(uri))
91-
.isInstanceOf(IllegalArgumentException.class)
92-
.hasMessageContaining("uri invalid");
93-
}
94-
95-
@ParameterizedTest
96-
@ValueSource(strings = {
97-
"https://armeria.dev/static/icon.js",
98-
"https://line.com/static/icon.zip",
99-
"https://evil.corp/image/icon.jpeg"
100-
})
101-
void withFavicon_givenBadImageExtension_throwsException(String uri) {
102-
103-
assertThatThrownBy(() -> DocServiceInjectedScriptsUtil.withFavicon(uri))
104-
.isInstanceOf(IllegalArgumentException.class)
105-
.hasMessageContaining("extension not allowed");
106-
}
107-
10882
@ParameterizedTest
10983
@MethodSource("getStreamOfEvilUri")
110-
void withFavicon_givenEvilUri_returnsScriptWithCleanUri(String uri, String expectedUri) {
84+
void favicon_givenEvilUri_returnsScriptWithCleanUri(String uri, String expectedUri) {
11185

112-
final String result = DocServiceInjectedScriptsUtil.withFavicon(uri);
86+
final String result = DocServiceInjectableScripts.favicon(uri);
11387

11488
assertThat(result).contains(expectedUri).doesNotContain(uri);
11589
}

0 commit comments

Comments
 (0)