1515
1616public class MetadataHelper {
1717 private static final Logger LOG = LogFactory .getLogger (MetadataHelper .class );
18- private static final Pattern HEX_UPPER_GROUPED = Pattern .compile ("^[0-9A-Fa-f]+(?:-[0-9A-Fa-f]+)*$" );
18+ // dash separated hex char sequences
19+ private static final Pattern METADATA_ID_VALIDATOR = Pattern .compile ("^[0-9A-Fa-f]+(?:-[0-9A-Fa-f]+)*$" );
1920 /**
2021 * Helper for parsing metadata uuid from url.
2122 * @param url
@@ -62,13 +63,18 @@ private static String tryParsingIdFromPath(String url) {
6263 if (url == null || !url .startsWith ("http" ) || url .length () < 11 ) {
6364 return null ;
6465 }
65- // remove possible protocol, we don't care if part of the domain is removed as well
66- String [] pathParts = url .substring (10 ).split ("/" );
67-
68- for (String possibleId : pathParts ) {
69- if (couldBeMetadataId (possibleId )) {
70- return possibleId ;
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+ }
7174 }
75+
76+ } catch (Exception e ) {
77+ LOG .debug ("Unexpected error converting url-string to uri:" , e );
7278 }
7379
7480 return null ;
@@ -79,11 +85,12 @@ private static boolean couldBeMetadataId(String possibleId) {
7985 // usually 30+ chars
8086 return false ;
8187 }
82- if (possibleId .startsWith ("%7B" ) && possibleId .endsWith ("%7D" )) {
83- possibleId = possibleId .substring (3 , possibleId .length () - 3 );
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 );
8491 }
8592
86- return HEX_UPPER_GROUPED .matcher (possibleId ).matches ();
93+ return METADATA_ID_VALIDATOR .matcher (possibleId ).matches ();
8794 }
8895
8996 public static ArrayList <String > getAllowedDomainsList () {
0 commit comments