Skip to content

Commit 80da944

Browse files
authored
Merge pull request #1226 from ZakarFin/parse-metadataid-from-path
Parse metadataid from path
2 parents 7a5262c + f506daa commit 80da944

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

control-userlayer/src/main/java/org/oskari/control/userlayer/CreateUserLayerHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ protected static boolean isFileIgnored(String name) {
281281
if (name == null || name.trim().isEmpty()) {
282282
return true;
283283
}
284+
// https://stackoverflow.com/questions/13846000/file-separators-of-path-name-of-zipentry
284285
String[] parts = name.split("/");
285286
for (int i = 0; i < parts.length; i++) {
286287
if (parts[i].indexOf('.') == 0) {

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import java.util.Collections;
1212
import java.util.List;
1313
import java.util.Map;
14+
import java.util.regex.Pattern;
1415

1516
public 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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public void testGetIdFromMetadataUrl() {
3737
expected.put("http://mydomain.org?test=test&uuid=key2&post=test", "key2");
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");
40+
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", "{C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3}");
42+
// java.net.URI breaks at {}
43+
expected.put("http://mydomain.org/dataset/{C73BC2C1-C2CA-4FCA-9171-B8074F2BC2A3}", null);
44+
4045
for (String url : expected.keySet()) {
4146
Assertions.assertEquals(expected.get(url), MetadataHelper.getIdFromMetadataUrl(url), "Url with id returns id value");
4247
}

0 commit comments

Comments
 (0)