forked from eclipse-rdf4j/rdf4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-rdf4jGH-5058: added metadatafinder code (WIP)
- Loading branch information
1 parent
8c3942d
commit 67e1965
Showing
12 changed files
with
241 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
core/rio/csvw/src/main/java/org/eclipse/rdf4j/rio/csvw/CSVWMetadataFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Eclipse RDF4J contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.rio.csvw; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URI; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Find metadata info for a given CSV file, using various methods | ||
* | ||
* @author Bart Hanssens | ||
*/ | ||
public class CSVWMetadataFinder { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CSVWMetadataFinder.class); | ||
private static final String WELL_KNOWN = "/.well-known/csvm"; | ||
private static final String METADATA_JSON = "-metadata.json"; | ||
private static final String CSV = ".csv"; | ||
|
||
/** | ||
* Find by adding metadata.json as file extension | ||
* | ||
* @param csvFile | ||
* @return inputstream or null | ||
*/ | ||
public static InputStream findByExtension(URI csvFile) { | ||
String s = csvFile.toString(); | ||
if (s.endsWith(CSV)) { | ||
s = s.substring(0, s.length() - CSV.length()); | ||
} | ||
URI metaURI = URI.create(s + METADATA_JSON); | ||
try (InputStream meta = metaURI.toURL().openStream()) { | ||
return new ByteArrayInputStream(meta.readAllBytes()); | ||
} catch (IOException ioe) { | ||
LOGGER.debug("Could not open {}", metaURI); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Try reading the well-known location | ||
* | ||
* @param csvFile | ||
* @return URI or null | ||
*/ | ||
public static InputStream findByWellKnown(URI csvFile) { | ||
URI wellKnown = csvFile.resolve(WELL_KNOWN); | ||
|
||
try (InputStream is = wellKnown.toURL().openStream(); | ||
BufferedReader r = new BufferedReader(new InputStreamReader(is))) { | ||
URI metaURI; | ||
String line = r.readLine(); | ||
|
||
while (line != null) { | ||
String s = line.replaceFirst("\\{\\+?url\\}", csvFile.toString()); | ||
if (s.isBlank()) { | ||
continue; | ||
} | ||
switch (line.charAt(0)) { | ||
case '?': | ||
metaURI = URI.create(line + s); | ||
break; | ||
case '/': | ||
metaURI = csvFile.resolve(s); | ||
break; | ||
default: | ||
metaURI = URI.create(s); | ||
} | ||
try (InputStream meta = metaURI.toURL().openStream()) { | ||
return new ByteArrayInputStream(meta.readAllBytes()); | ||
} catch (IOException ioe) { | ||
LOGGER.debug("Could not open {}", metaURI); | ||
} | ||
line = r.readLine(); | ||
} | ||
} catch (IOException ioe) { | ||
LOGGER.info("Could not open {}", wellKnown); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/rio/csvw/src/test/java/org/eclipse/rdf4j/rio/csvw/CSVWMetadataFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Eclipse RDF4J contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.rdf4j.rio.csvw; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.junit.jupiter.MockServerExtension; | ||
|
||
/** | ||
* | ||
* @author Bart.Hanssens | ||
*/ | ||
@ExtendWith(MockServerExtension.class) | ||
public class CSVWMetadataFinderTest { | ||
private MockServerClient client; | ||
|
||
private String getFile(String file) throws IOException { | ||
return new String(CSVWMetadataFinderTest.class.getResourceAsStream("/" + file).readAllBytes()); | ||
} | ||
|
||
@BeforeEach | ||
public void init(MockServerClient client) throws IOException { | ||
this.client = client; | ||
client.when( | ||
request().withMethod("GET").withPath("/downloads/painters.csv")) | ||
.respond(response().withBody(getFile("painters.csv"))); | ||
client.when( | ||
request().withMethod("GET").withPath("/.well-known/csvm")) | ||
.respond(response().withBody(getFile("well-known-csvm"))); | ||
client.when( | ||
request().withMethod("GET").withPath("/downloads/painters.csvm")) | ||
.respond(response().withBody(getFile("painters-metadata.json"))); | ||
} | ||
|
||
@Test | ||
public void testWellKnownLocation() throws IOException { | ||
String base = "http://localhost:" + client.getPort() + "/"; | ||
URI uri = URI.create(base + "downloads/painters.csv"); | ||
|
||
String expected = getFile("painters-metadata.json"); | ||
String got = new String(CSVWMetadataFinder.findByWellKnown(uri).readAllBytes()); | ||
assertEquals(expected, got); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{+url}m |