Skip to content

Commit f506daa

Browse files
committed
Use URI to decode curly braces
1 parent ba3024c commit f506daa

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

service-capabilities/src/main/java/org/oskari/capabilities/MetadataHelper.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
public 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() {

service-capabilities/src/test/java/org/oskari/capabilities/MetadataHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testGetIdFromMetadataUrl() {
3838
expected.put("http://mydomain.org?test=test&Id=key&post=test", "key");
3939
expected.put("http://mydomain.org?test=test&uuId=Key&post=test", "Key");
4040
expected.put("http://mydomain.org/dataset/C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3", "C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3");
41-
expected.put("http://mydomain.org/dataset/%7BC73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3%7D", "%7BC73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3%7D");
41+
expected.put("http://mydomain.org/dataset/%7BC73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3%7D", "{C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3}");
4242
// java.net.URI breaks at {}
4343
expected.put("http://mydomain.org/dataset/{C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3}", null);
4444

0 commit comments

Comments
 (0)