1
2
3
4 package de.powerstat.validation;
5
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Objects;
10
11
12
13
14
15
16
17
18 @SuppressWarnings("java:S1696")
19 public final class ValidationUtils
20 {
21
22
23
24
25
26
27
28
29 private static final char URI_SEPARATOR = '/';
30
31
32
33
34
35 private ValidationUtils()
36 {
37 super();
38 }
39
40
41
42
43
44
45
46
47
48 public static String sanitizeUrlPath(final String urlPath)
49 {
50 Objects.requireNonNull(urlPath, "urlPath");
51 final var result = new StringBuilder(urlPath.length() + 1);
52 if (urlPath.isEmpty())
53 {
54 result.append(ValidationUtils.URI_SEPARATOR);
55 }
56 else
57 {
58 if (urlPath.charAt(0) != ValidationUtils.URI_SEPARATOR)
59 {
60 result.append(ValidationUtils.URI_SEPARATOR);
61 }
62
63
64
65
66
67
68 result.append(urlPath);
69 }
70 return result.toString();
71 }
72
73
74
75
76
77
78
79
80
81
82 public static List<String> splitHostnamePort(final String hostnamePort)
83 {
84 Objects.requireNonNull(hostnamePort, "hostnamePort");
85 final int pos = hostnamePort.lastIndexOf(':');
86 if (pos == -1)
87 {
88 throw new IllegalArgumentException("Not a host:port combination!");
89 }
90 final List<String> result = new ArrayList<>();
91 if (hostnamePort.contains("["))
92 {
93 result.add(hostnamePort.substring(1, pos - 1));
94 }
95 else
96 {
97 result.add(hostnamePort.substring(0, pos));
98 }
99 result.add(hostnamePort.substring(pos + 1));
100 return result;
101 }
102
103 }