1111import java .util .Collections ;
1212import java .util .List ;
1313import java .util .Map ;
14+ import java .util .regex .Pattern ;
1415
1516public class MetadataHelper {
1617 private static final Logger LOG = LogFactory .getLogger (MetadataHelper .class );
18+ // dash separated hex char sequences
19+ private static final Pattern METADATA_ID_VALIDATOR = Pattern .compile ("^[0-9A-Fa-f]+(?:-[0-9A-Fa-f]+)*$" );
1720 /**
1821 * Helper for parsing metadata uuid from url.
1922 * @param url
@@ -40,7 +43,7 @@ public static String getIdFromMetadataUrl(String url) {
4043 .orElse (null );
4144 if (idParam == null ) {
4245 // param not in url
43- return null ;
46+ return tryParsingIdFromPath ( url ) ;
4447 }
4548 List <String > values = params .getOrDefault (idParam , Collections .emptyList ());
4649 if (values .isEmpty ()) {
@@ -56,6 +59,40 @@ public static String getIdFromMetadataUrl(String url) {
5659 return null ;
5760 }
5861
62+ private static String tryParsingIdFromPath (String url ) {
63+ if (url == null || !url .startsWith ("http" ) || url .length () < 11 ) {
64+ return null ;
65+ }
66+ // Parse with URI and getPath() to remove params etc
67+ // the url-encoded curly braces will be decoded and saved to db as decoded
68+ try {
69+ String [] pathParts = new URI (url ).getPath ().split ("/" );
70+ for (String possibleId : pathParts ) {
71+ if (couldBeMetadataId (possibleId )) {
72+ return possibleId ;
73+ }
74+ }
75+
76+ } catch (Exception e ) {
77+ LOG .debug ("Unexpected error converting url-string to uri:" , e );
78+ }
79+
80+ return null ;
81+ }
82+
83+ private static boolean couldBeMetadataId (String possibleId ) {
84+ if (possibleId == null || possibleId .length () < 20 ) {
85+ // usually 30+ chars
86+ return false ;
87+ }
88+ // URI decodes any URL-encoded bits so we can assume decoded chars here
89+ if (possibleId .startsWith ("{" ) && possibleId .endsWith ("}" )) {
90+ possibleId = possibleId .substring (1 , possibleId .length () - 1 );
91+ }
92+
93+ return METADATA_ID_VALIDATOR .matcher (possibleId ).matches ();
94+ }
95+
5996 public static ArrayList <String > getAllowedDomainsList () {
6097 ArrayList <String > allowedDomains = new ArrayList <String >(Arrays .asList (PropertyUtil .getCommaSeparatedList ("service.metadata.domains" )));
6198 //add property url if allowedDomains list is empty
0 commit comments