25
25
import java .net .Inet4Address ;
26
26
import java .net .Inet6Address ;
27
27
import java .net .InetAddress ;
28
- import java .net .MalformedURLException ;
28
+ import java .net .URI ;
29
29
import java .net .URISyntaxException ;
30
- import java .net .URL ;
31
30
import java .util .HashSet ;
32
31
import java .util .Set ;
33
32
import org .projectnessie .cel .common .types .BoolT ;
@@ -357,11 +356,7 @@ private static Overload isUri() {
357
356
if (addr .isEmpty ()) {
358
357
return BoolT .False ;
359
358
}
360
- try {
361
- return Types .boolOf (new URL (addr ).toURI ().isAbsolute ());
362
- } catch (MalformedURLException | URISyntaxException e ) {
363
- return BoolT .False ;
364
- }
359
+ return Types .boolOf (validateURI (addr , true ));
365
360
});
366
361
}
367
362
@@ -381,14 +376,7 @@ private static Overload isUriRef() {
381
376
if (addr .isEmpty ()) {
382
377
return BoolT .False ;
383
378
}
384
- try {
385
- // TODO: The URL api requires a host or it always fails.
386
- String host = "http://protovalidate.buf.build" ;
387
- URL url = new URL (host + addr );
388
- return Types .boolOf (url .getPath () != null && !url .getPath ().isEmpty ());
389
- } catch (MalformedURLException e ) {
390
- return BoolT .False ;
391
- }
379
+ return Types .boolOf (validateURI (addr , false ));
392
380
});
393
381
}
394
382
@@ -609,6 +597,25 @@ private static boolean validateIP(String addr, long ver) {
609
597
return false ;
610
598
}
611
599
600
+ /**
601
+ * Validates if the input string is a valid URI, which can be a URL or a URN.
602
+ *
603
+ * @param val The input string to validate as a URI.
604
+ * @param checkAbsolute Whether to check if this URI is absolute (i.e. has a scheme component)
605
+ * @return {@code true} if the input string is a valid URI, {@code false} otherwise.
606
+ */
607
+ private static boolean validateURI (String val , boolean checkAbsolute ) {
608
+ try {
609
+ URI uri = new URI (val );
610
+ if (checkAbsolute ) {
611
+ return uri .isAbsolute ();
612
+ }
613
+ return true ;
614
+ } catch (URISyntaxException e ) {
615
+ return false ;
616
+ }
617
+ }
618
+
612
619
/**
613
620
* Validates if the input string is a valid IP prefix.
614
621
*
0 commit comments