Skip to content

Commit ceb7fc4

Browse files
committed
refactor: extract URL validation into util class
1 parent 42ab610 commit ceb7fc4

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ category: Administration
1818
* refactor: use parameterized log messages
1919
* refactor: remove unused or obsolete imports
2020
* feat: ensure compatibility with Jira 11.2.0
21+
* refactor: extract URL validation into util class
2122

2223
### [25.10.0] - 2025-10-03
2324

src/main/java/de/codescape/jira/plugins/multiplesubtasks/service/SubtasksCreationService.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
import de.codescape.jira.plugins.multiplesubtasks.service.syntax.DateTimeStringService;
3737
import de.codescape.jira.plugins.multiplesubtasks.service.syntax.EstimateStringService;
3838
import de.codescape.jira.plugins.multiplesubtasks.service.syntax.SubtasksSyntaxService;
39+
import de.codescape.jira.plugins.multiplesubtasks.util.URLUtil;
3940
import jakarta.inject.Inject;
4041
import org.springframework.stereotype.Component;
4142

42-
import java.net.MalformedURLException;
43-
import java.net.URISyntaxException;
44-
import java.net.URL;
4543
import java.sql.Timestamp;
4644
import java.util.*;
4745
import java.util.stream.Collectors;
@@ -607,7 +605,7 @@ private void applyValuesToCustomField(MutableIssue parent, MutableIssue newSubta
607605
newSubtask.setCustomFieldValue(customField, parent.getCustomFieldValue(customField));
608606
}
609607
} else {
610-
if (isValidURL(value)) {
608+
if (URLUtil.isValidURL(value)) {
611609
newSubtask.setCustomFieldValue(customField, value);
612610
} else {
613611
warnings.add("Invalid url value for custom field: " + customFieldName);
@@ -877,16 +875,4 @@ private void applyValuesToCustomField(MutableIssue parent, MutableIssue newSubta
877875
}
878876
}
879877

880-
/**
881-
* Verify the provided URL string is a valid URL to be persisted to a custom field of type URL.
882-
*/
883-
private boolean isValidURL(String url) {
884-
try {
885-
new URL(url).toURI();
886-
return true;
887-
} catch (MalformedURLException | URISyntaxException e) {
888-
return false;
889-
}
890-
}
891-
892878
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.codescape.jira.plugins.multiplesubtasks.util;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
7+
/**
8+
* Utility class for URL related stuff.
9+
*/
10+
public class URLUtil {
11+
12+
/**
13+
* Verify the provided URL string is a valid URL to be persisted to a custom field of type URL.
14+
*/
15+
public static boolean isValidURL(String url) {
16+
try {
17+
//noinspection ResultOfMethodCallIgnored
18+
new URI(url).toURL();
19+
return true;
20+
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
21+
return false;
22+
}
23+
}
24+
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package de.codescape.jira.plugins.multiplesubtasks.util;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.is;
8+
9+
public class URLUtilTest {
10+
11+
@Test
12+
public void shouldAcceptFullQualifiedURLs() {
13+
assertThat(URLUtil.isValidURL("https://www.codescape.de"), is(equalTo(true)));
14+
}
15+
16+
@Test
17+
public void shouldRejectNonValidURLs() {
18+
assertThat(URLUtil.isValidURL("stefan@codescape.de"), is(equalTo(false)));
19+
}
20+
21+
}

0 commit comments

Comments
 (0)