Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EA-3703 add urn pattern #285

Merged
merged 1 commit into from
Feb 19, 2024
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 @@ -18,7 +18,10 @@ public final class EuropeanaUriUtils {

private static final Pattern RELATIVEURLPATTERN = Pattern.compile("^(?!www\\.|(?:http|ftp|session)s?://|[A-Za-z]:\\|//)(?:#|\\./|\\.\\./|/)\\S+$");
private static final Pattern ABSOLUTEURLPATTERN = Pattern.compile("^([a-zA-Z][a-zA-Z+-.]*)://[^/$.?#].*$");
// private static final Pattern ABSOLUTEURLPATTERN = Pattern.compile("^(https?|ftp|session)://[^\\s/$.?#].[^\\s]*$");

// EA-3703 urn pattern added. Should be treated differently
private static final Pattern URN_PATTERN = Pattern.compile("^urn:[a-z0-9][a-z0-9-]{0,31}:([a-z0-9()+,\\-.:=@;$_!*']|%[0-9a-f]{2})++$", Pattern.CASE_INSENSITIVE);

private static final Set<String> schemes= new HashSet<>();

static {
Expand Down Expand Up @@ -246,7 +249,7 @@ public final class EuropeanaUriUtils {
schemes.add("tv");
schemes.add("udp");
schemes.add("unreal");
schemes.add("urn");
//schemes.add("urn"); EA-3703 urn will be treated separately
schemes.add("ut2004");
schemes.add("v-event");
schemes.add("vemmi");
Expand Down Expand Up @@ -312,14 +315,14 @@ private static String sanitizeCollectionId(String collectionId) {
}

/**
* Check if the provided string is a valid (absolute or relative) URI
* Check if the provided string is a valid (absolute or relative or urn) URI
*
* @param str URI to check
* @return true if the provided string is not empty and a valid URI, otherwise false
*/
public static boolean isUri(String str) {
if (StringUtils.isNotEmpty(str)) {
return (isAbsoluteUri(str) || isRelativeUri(str));
return (isAbsoluteUri(str) || isRelativeUri(str) || isUrn(str));
}
return false;
}
Expand Down Expand Up @@ -350,4 +353,15 @@ public static boolean isRelativeUri(String uri) {
return m.find();
}

/**
* Checks if a uri is a valid urn
* See - EA-3703
* @param uri
* @return
*/
public static boolean isUrn(String uri) {
Matcher m = URN_PATTERN.matcher(uri);
return m.find();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public void isURITest() {
assertTrue(EuropeanaUriUtils.isUri("http://www.budaivigado.hu/ftp/rmne//Mozgokep/Tanc/Magyarpalatka.1991.11.16/Magyarpalatkai_sűrű magyarMoldovan_Istvan91.mp4"));
assertFalse(EuropeanaUriUtils.isUri("Europeana space test"));
assertFalse(EuropeanaUriUtils.isAbsoluteUri("Europeana space test"));

// check urn values
assertTrue(EuropeanaUriUtils.isUri("urn:rijksmuseum:thesaurus:RM0001.THESAU.22403"));
// should be a absolute url or relative url
assertFalse(EuropeanaUriUtils.isAbsoluteUri("urn:rijksmuseum:thesaurus:RM0001.THESAU.22403"));
assertFalse(EuropeanaUriUtils.isRelativeUri("urn:rijksmuseum:thesaurus:RM0001.THESAU.22403"));

assertFalse(EuropeanaUriUtils.isUrn("Europeana space test"));
assertTrue(EuropeanaUriUtils.isUrn("urn:rijksmuseum:thesaurus:RM0001.THESAU.19"));

}

@Test
Expand Down