Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,11 @@ public static Link createLink(final String value, final String name,
final String url, final String type) {
final String resolvedName = firstNonEmpty(value).orElse(name);
final String resolvedUrl = firstNonEmpty(url)
.orElseGet(() -> getLinkUrl(resolvedName, type));
.orElseGet(
() -> isHttpOrHttpsUrl(resolvedName)
? resolvedName
: getLinkUrl(resolvedName, type)
);
return new Link()
.setName(resolvedName)
.setUrl(resolvedUrl)
Expand Down Expand Up @@ -847,6 +851,21 @@ private static String getLinkUrl(final String name, final String type) {
return pattern.replaceAll("\\{}", Objects.isNull(name) ? "" : name);
}

private static boolean isHttpOrHttpsUrl(final String value) {
if (Objects.isNull(value)) {
return false;
}

try {
final URI uri = URI.create(value);
final String scheme = uri.getScheme();
return nonNull(uri.getHost())
&& ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme));
} catch (IllegalArgumentException ignored) {
return false;
}
}

private static String getRealHostName() {
if (Objects.isNull(cachedHost)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,44 @@ public static Stream<Arguments> data() {
return Stream.of(
Arguments.of("a", "b", "c", "d", "e", link("a", "c", "d")),
Arguments.of("a", "b", "c", "d", null, link("a", "c", "d")),
Arguments.of(
"https://example.org/path?query=value#fragment",
null,
null,
"d",
"pattern/{}/some",
link(
"https://example.org/path?query=value#fragment",
"https://example.org/path?query=value#fragment",
"d"
)
),
Arguments.of(
"http://example.org/path",
null,
null,
"d",
null,
link("http://example.org/path", "http://example.org/path", "d")
),
Arguments.of("a", "b", null, "d", "invalid-pattern", link("a", "invalid-pattern", "d")),
Arguments.of("a", "b", null, "d", "pattern/{}/some", link("a", "pattern/a/some", "d")),
Arguments.of(
"ftp://example.org/path",
null,
null,
"d",
"pattern/{}/some",
link("ftp://example.org/path", "pattern/ftp://example.org/path/some", "d")
),
Arguments.of(
"https:path-without-host",
null,
null,
"d",
"pattern/{}/some",
link("https:path-without-host", "pattern/https:path-without-host/some", "d")
),
Arguments.of(null, null, null, "d", "pattern/{}/some", link(null, "pattern//some", "d")),
Arguments.of(null, null, null, null, "pattern/{}/some", link(null, null, null)),
Arguments.of(null, "b", null, "d", "pattern/{}/some/{}/and-more", link("b", "pattern/b/some/b/and-more", "d")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ void shouldSupportCustomMultiLabelAnnotations() {
name = "LINK-2",
url = "https://example.org/link/2"
),
@Link(url = "https://example.org/some-custom-link")
@Link(url = "https://example.org/some-custom-link"),
@Link("https://example.org/direct-link"),
@Link("http://example.org/direct-link"),
@Link("ftp://example.org/pattern-link")
}
)
@TmsLink("TMS-1")
Expand Down Expand Up @@ -275,6 +278,13 @@ void shouldExtractLinks() {
tuple("LINK-1", "custom", "https://example.org/custom/LINK-1"),
tuple("LINK-2", "custom", "https://example.org/link/2"),
tuple("", "custom", "https://example.org/some-custom-link"),
tuple("https://example.org/direct-link", "custom", "https://example.org/direct-link"),
tuple("http://example.org/direct-link", "custom", "http://example.org/direct-link"),
tuple(
"ftp://example.org/pattern-link",
"custom",
"https://example.org/custom/ftp://example.org/pattern-link"
),
tuple("ISSUE-1", "issue", "https://example.org/issue/ISSUE-1"),
tuple("ISSUE-2", "issue", "https://example.org/issue/ISSUE-2"),
tuple("ISSUE-3", "issue", "https://example.org/issue/ISSUE-3"),
Expand Down
Loading