+ * The provided String is expected to be a valid {@link java.nio.file.Path}, otherwise an + * {@link InvalidPathException} is thrown. + *
+ * Format iterates through every part of the path and checks them agains the configured filters. + *
+ * The filter matching works the same as {@link Pattern#matches(String, CharSequence)}. + *
+ * The resulting String representation of a path will be removed of any matching filter terms. + * + *
+ * List+ *filters = Arrays.asList("removeme"); + * var formatter = PathFormatter.with(filters); + * + * System.out.println(formatter("/example/removeme/path")); + * + * // Prints out "/example/path" + *
+ * Format has an implicit check for {@link Path#isAbsolute()} and preserves an absolute or
+ * relative path.
+ *
+ * @param s the String value to format
+ * @return the formatted new String, after the filter has been applied
+ * @throws InvalidPathException in case the String is not a valid path
+ * @since 1.1.0
+ */
+ public String format(String s) throws InvalidPathException {
+ return apply(Path.of(s), filter);
+ }
+}
diff --git a/measurement-provider/src/test/groovy/life/qbic/data_download/measurement/api/PathFormatterSpec.groovy b/measurement-provider/src/test/groovy/life/qbic/data_download/measurement/api/PathFormatterSpec.groovy
new file mode 100644
index 0000000..1b53af9
--- /dev/null
+++ b/measurement-provider/src/test/groovy/life/qbic/data_download/measurement/api/PathFormatterSpec.groovy
@@ -0,0 +1,45 @@
+package life.qbic.data_download.measurement.api
+
+import life.qbic.data_download.measurements.api.PathFormatter
+import spock.lang.Specification
+
+class PathFormatterSpec extends Specification {
+
+ def "Given a String with a to filtered part, the filtered part past not pe part of the resulting String"() {
+ given:
+ var filter = Arrays.asList("awesome", "wehaa")
+
+ and:
+ var f = PathFormatter.with(filter)
+
+ when:
+ var result = f.format(input)
+
+ then:
+ result == expected
+
+ where:
+ input | expected
+ "/my/awesome/path/wehaa" | "/my/path"
+ "a/relative/wehaa/awesome/path" | "a/relative/path"
+ }
+
+ def "Given a String that contains a UUID-4 in its path, the formatter shall filter it out"() {
+ given:
+ var filter = Arrays.asList("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\$")
+
+ and:
+ var f = PathFormatter.with(filter)
+
+ when:
+ var result = f.format(input)
+
+ then:
+ result == expected
+
+ where:
+ input | expected
+ "/my/f47ac10b-58cc-4372-a567-0e02b2c3d479/path/" | "/my/path"
+ "a/relative/f47ac10b-58cc-4372-a567-0e02b2c3d479/path" | "a/relative/path"
+ }
+}
diff --git a/openbis-connector/src/main/java/life/qbic/data_download/openbis/DatasetFileStreamReaderImpl.java b/openbis-connector/src/main/java/life/qbic/data_download/openbis/DatasetFileStreamReaderImpl.java
index 936c26a..2dc1307 100644
--- a/openbis-connector/src/main/java/life/qbic/data_download/openbis/DatasetFileStreamReaderImpl.java
+++ b/openbis-connector/src/main/java/life/qbic/data_download/openbis/DatasetFileStreamReaderImpl.java
@@ -6,10 +6,12 @@
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download.DataSetFileDownload;
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download.DataSetFileDownloadReader;
import java.io.InputStream;
-import java.util.Optional;
+import java.util.List;
+import java.util.Objects;
import life.qbic.data_download.measurements.api.DataFile;
import life.qbic.data_download.measurements.api.FileInfo;
import life.qbic.data_download.measurements.api.MeasurementDataReader;
+import life.qbic.data_download.measurements.api.PathFormatter;
/**
* Reads openbis data streams
@@ -17,11 +19,16 @@
public class DatasetFileStreamReaderImpl implements MeasurementDataReader {
private DataSetFileDownloadReader dataSetFileDownloadReader;
- private final String ignoredPrefix;
+
+ private final PathFormatter formatter;
+
+ private static final String UUID_REGEX = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
public DatasetFileStreamReaderImpl(String ignoredPrefix) {
+ Objects.requireNonNull(ignoredPrefix);
dataSetFileDownloadReader = null;
- this.ignoredPrefix = Optional.ofNullable(ignoredPrefix).orElse("");
+ var filter = List.of(ignoredPrefix, UUID_REGEX);
+ this.formatter = PathFormatter.with(filter);
}
@@ -55,7 +62,10 @@ public DataFile nextDataFile() {
.toEpochMilli() : -1;
long lastModifiedMillis = nonNull(dataStore) ? dataStore.getModificationDate().toInstant()
.toEpochMilli() : -1;
- FileInfo fileInfo = new FileInfo(fileDownload.getDataSetFile().getPath().replaceFirst(ignoredPrefix, ""),
+
+ var cleanedPath = formatter.format(fileDownload.getDataSetFile().getPath());
+
+ FileInfo fileInfo = new FileInfo(cleanedPath,
fileDownload.getDataSetFile().getFileLength(),
Integer.toUnsignedLong(fileDownload.getDataSetFile().getChecksumCRC32()),
creationMillis,
From f1a2231917cb8391461d38a45fa98c657bb00e8b Mon Sep 17 00:00:00 2001
From: "Sven F." <9976560+sven1103@users.noreply.github.com>
Date: Wed, 5 Feb 2025 09:26:59 +0100
Subject: [PATCH 3/3] Update
measurement-provider/src/test/groovy/life/qbic/data_download/measurement/api/PathFormatterSpec.groovy
Co-authored-by: Tobias Koch