Skip to content

Commit

Permalink
Reworking URIUtils for improve robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Dec 31, 2024
1 parent 656ae60 commit 51d48e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/
package org.apache.syncope.core.provisioning.api.utils;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

public final class URIUtils {

Expand All @@ -33,15 +33,13 @@ private URIUtils() {
/**
* Build a valid URI out of the given location.
* Only "file", "connid" and "connids" schemes are allowed.
* For "file", invalid characters are handled via intermediate transformation into URL.
*
* @param location the candidate location for URI
* @return valid URI for the given location
* @throws MalformedURLException if the intermediate URL is not valid
* @throws URISyntaxException if the given location does not correspond to a valid URI
*/
public static URI buildForConnId(final String location) throws MalformedURLException, URISyntaxException {
final String candidate = location.trim();
public static URI buildForConnId(final String location) throws URISyntaxException {
String candidate = location.trim();

if (!candidate.startsWith("file:")
&& !candidate.startsWith("connid:") && !candidate.startsWith("connids:")) {
Expand All @@ -51,7 +49,12 @@ public static URI buildForConnId(final String location) throws MalformedURLExcep

URI uri;
if (candidate.startsWith("file:")) {
uri = Path.of(new URL(candidate).getFile()).toFile().getAbsoluteFile().toURI();
candidate = StringUtils.substringAfter(candidate, "file:");
if (SystemUtils.IS_OS_WINDOWS) {
candidate = StringUtils.stripStart(candidate, "/");
}

uri = Path.of(candidate).toFile().getAbsoluteFile().toURI();
} else {
uri = new URI(candidate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.syncope.core.provisioning.api.utils;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand All @@ -41,5 +42,8 @@ public void buildForConnId() throws URISyntaxException, MalformedURLException {
location.set("connid:test/location");
URI expectedURI = new URI(location.get().trim());
assertEquals(expectedURI, URIUtils.buildForConnId(location.get()));

assertDoesNotThrow(() -> URIUtils.buildForConnId("file:Z:\\syncope\\fit\\core-reference\\target/bundles/"));
assertDoesNotThrow(() -> URIUtils.buildForConnId("file:/Z:\\syncope\\fit\\core-reference\\target/bundles/"));
}
}

0 comments on commit 51d48e5

Please sign in to comment.