Skip to content

Commit 014a615

Browse files
committed
Added also the inverse util.
1 parent ade37fc commit 014a615

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

vpro-shared-util/src/main/java/nl/vpro/util/URLUtils.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,27 @@ public static String hidePassword(String url) {
3232
}
3333
}
3434

35-
}
35+
public static String addAuthentication(String url, String basicAuth) {
36+
if (url == null) {
37+
return null;
38+
}
39+
try {
40+
URI uri = URI.create(url);
41+
URI sanitized = new URI(
42+
uri.getScheme(),
43+
basicAuth,
44+
uri.getHost(),
45+
uri.getPort(),
46+
uri.getPath(),
47+
uri.getQuery(),
48+
uri.getFragment()
49+
);
50+
return sanitized.toString();
51+
} catch (Exception e) {
52+
// on any parse error, return original URL unchanged
53+
return url;
54+
}
55+
}
56+
57+
58+
}

vpro-shared-util/src/test/java/nl/vpro/util/URLUtilsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public void hidePassword() {
1111
assertThat( URLUtils.hidePassword("https://user:password@example.com/path?query=1")).isEqualTo("https://user:*****@example.com/path?query=1");
1212
}
1313

14+
@Test
15+
public void addPassword() {
16+
assertThat( URLUtils.addAuthentication("https://example.com/path?query=1", "user:password")).isEqualTo("https://user:password@example.com/path?query=1");
17+
}
18+
1419
}

0 commit comments

Comments
 (0)