From 80523192844c2106f13c7b7374ae10c4e7544868 Mon Sep 17 00:00:00 2001 From: Carlos Afonso Date: Thu, 25 Jul 2024 15:59:41 +0100 Subject: [PATCH] Added suport for paths to allow ./ --- .../AmdSecValidator.java | 25 ++++++----- .../DmdSecValidator.java | 44 +++++++++++-------- .../FileSecValidator.java | 25 ++++++----- .../StructMapValidator.java | 3 +- .../validator/utils/DecoderUtils.java | 14 ++++++ 5 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 src/main/java/org/roda_project/commons_ip2/validator/utils/DecoderUtils.java diff --git a/src/main/java/org/roda_project/commons_ip2/validator/components/administritiveMetadataComponent/AmdSecValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/components/administritiveMetadataComponent/AmdSecValidator.java index 6e4487ad..97c7c1f0 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/components/administritiveMetadataComponent/AmdSecValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/components/administritiveMetadataComponent/AmdSecValidator.java @@ -21,6 +21,7 @@ import org.roda_project.commons_ip2.validator.state.MetsValidatorState; import org.roda_project.commons_ip2.validator.state.StructureValidatorState; import org.roda_project.commons_ip2.validator.utils.CHECKSUMTYPE; +import org.roda_project.commons_ip2.validator.utils.DecoderUtils; import org.roda_project.commons_ip2.validator.utils.Message; import org.roda_project.commons_ip2.validator.utils.MetadataType; @@ -88,7 +89,8 @@ protected ReporterDetails validateCSIP31(final StructureValidatorState structure for (MdSecType md : allMdSecTypes) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (metsValidatorState.isRootMets()) { if (metadataFiles.containsKey(metsValidatorState.getMets().getOBJID() + "/" + hrefDecoded)) { metadataFiles.replace(metsValidatorState.getMets().getOBJID() + "/" + hrefDecoded, true); @@ -105,7 +107,8 @@ protected ReporterDetails validateCSIP31(final StructureValidatorState structure for (MdSecType md : metsValidatorState.getMets().getDmdSec()) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (metsValidatorState.isRootMets()) { if (metadataFiles.containsKey(metsValidatorState.getMets().getOBJID() + "/" + hrefDecoded)) { metadataFiles.replace(metsValidatorState.getMets().getOBJID() + "/" + hrefDecoded, true); @@ -168,7 +171,8 @@ protected ReporterDetails validateCSIP31(final StructureValidatorState structure for (MdSecType md : amd.getDigiprovMD()) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (hrefDecoded != null) { final String path = Paths.get(metsValidatorState.getMetsPath()).resolve(hrefDecoded).toString(); if (metadataFiles.containsKey(path)) { @@ -182,7 +186,8 @@ protected ReporterDetails validateCSIP31(final StructureValidatorState structure for (MdSecType md : metsValidatorState.getMets().getDmdSec()) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (hrefDecoded != null) { final String path = Paths.get(metsValidatorState.getMetsPath()).resolve(hrefDecoded).toString(); if (metadataFiles.containsKey(path)) { @@ -416,7 +421,7 @@ protected ReporterDetails validateCSIP38(final StructureValidatorState structure final MdSecType.MdRef mdRef = md.getMdRef(); final StringBuilder message = new StringBuilder(); if (mdRef != null && mdRef.getHref() != null) { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); if (structureValidatorState.isZipFileFlag()) { final StringBuilder path = new StringBuilder(); if (metsValidatorState.isRootMets()) { @@ -527,7 +532,7 @@ protected ReporterDetails validateCSIP41(final StructureValidatorState structure for (MdSecType md : digiprov) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final Long size = mdRef.getSIZE(); if (size == null) { return new ReporterDetails(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION, @@ -641,7 +646,7 @@ protected ReporterDetails validateCSIP43(final StructureValidatorState structure metsValidatorState.getMetsName(), metsValidatorState.isRootMets()), false, false); } else { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final StringBuilder message = new StringBuilder(); if (structureValidatorState.isZipFileFlag()) { final StringBuilder file = new StringBuilder(); @@ -950,7 +955,7 @@ protected ReporterDetails validateCSIP51(final StructureValidatorState structure for (MdSecType rmd : rigthsMD) { final MdSecType.MdRef mdRef = rmd.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final StringBuilder message = new StringBuilder(); if (structureValidatorState.isZipFileFlag()) { final StringBuilder filePath = new StringBuilder(); @@ -1064,7 +1069,7 @@ protected ReporterDetails validateCSIP54(final StructureValidatorState structure for (MdSecType rmd : rigthsMD) { final MdSecType.MdRef mdRef = rmd.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final Long size = mdRef.getSIZE(); if (size == null) { return new ReporterDetails(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION, @@ -1177,7 +1182,7 @@ protected ReporterDetails validateCSIP56(final StructureValidatorState structure metsValidatorState.getMetsName(), metsValidatorState.isRootMets()), false, false); } else { - final String href = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final StringBuilder message = new StringBuilder(); if (structureValidatorState.isZipFileFlag()) { final StringBuilder filePath = new StringBuilder(); diff --git a/src/main/java/org/roda_project/commons_ip2/validator/components/descriptiveMetadataComponent/DmdSecValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/components/descriptiveMetadataComponent/DmdSecValidator.java index 3dfb0b42..fdd8cfd9 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/components/descriptiveMetadataComponent/DmdSecValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/components/descriptiveMetadataComponent/DmdSecValidator.java @@ -1,5 +1,17 @@ package org.roda_project.commons_ip2.validator.components.descriptiveMetadataComponent; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLDecoder; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.roda_project.commons_ip2.mets_v1_12.beans.AmdSecType; import org.roda_project.commons_ip2.mets_v1_12.beans.MdSecType; @@ -14,20 +26,10 @@ import org.roda_project.commons_ip2.validator.state.MetsValidatorState; import org.roda_project.commons_ip2.validator.state.StructureValidatorState; import org.roda_project.commons_ip2.validator.utils.CHECKSUMTYPE; +import org.roda_project.commons_ip2.validator.utils.DecoderUtils; import org.roda_project.commons_ip2.validator.utils.Message; import org.roda_project.commons_ip2.validator.utils.MetadataType; -import java.io.IOException; -import java.io.InputStream; -import java.net.URLDecoder; -import java.nio.file.Paths; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - /** * @author Carlos Afonso */ @@ -88,7 +90,10 @@ protected ReporterDetails validateCSIP17(final StructureValidatorState structure for (MdSecType md : dmdSec) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + + String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); + Path path = Paths.get(hrefDecoded); + hrefDecoded = path.normalize().toString(); if (metsValidatorState.isRootMets()) { if (metadataFiles.containsKey(mets.getOBJID() + Constants.SEPARATOR + hrefDecoded)) { metadataFiles.replace(mets.getOBJID() + Constants.SEPARATOR + hrefDecoded, true); @@ -112,7 +117,8 @@ protected ReporterDetails validateCSIP17(final StructureValidatorState structure for (MdSecType md : allMdSecTypes) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null && mdRef.getHref() != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (metsValidatorState.isRootMets()) { if (metadataFiles.containsKey(mets.getOBJID() + Constants.SEPARATOR + hrefDecoded)) { metadataFiles.replace(mets.getOBJID() + Constants.SEPARATOR + hrefDecoded, true); @@ -174,7 +180,8 @@ protected ReporterDetails validateCSIP17(final StructureValidatorState structure for (MdSecType md : dmdSec) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (hrefDecoded != null) { final String path = Paths.get(metsValidatorState.getMetsPath()).resolve(hrefDecoded).toString(); if (metadataFiles.containsKey(path)) { @@ -188,7 +195,8 @@ protected ReporterDetails validateCSIP17(final StructureValidatorState structure for (MdSecType md : amd.getDigiprovMD()) { final MdSecType.MdRef mdRef = md.getMdRef(); if (mdRef != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), + Constants.UTF_8); if (hrefDecoded != null) { final String path = Paths.get(metsValidatorState.getMetsPath()).resolve(hrefDecoded).toString(); if (metadataFiles.containsKey(path)) { @@ -414,7 +422,7 @@ protected ReporterDetails validateCSIP24(final StructureValidatorState structure final MdSecType.MdRef mdRef = mdSec.getMdRef(); final String href = mdRef.getHref(); if (href != null) { - final String hrefDecoded = URLDecoder.decode(href, Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(href), Constants.UTF_8); if (structureValidatorState.isZipFileFlag()) { final StringBuilder path = new StringBuilder(); if (metsValidatorState.isRootMets()) { @@ -531,7 +539,7 @@ protected ReporterDetails validateCSIP27(final StructureValidatorState structure final MdSecType.MdRef mdRef = mdSec.getMdRef(); final String href = mdRef.getHref(); if (href != null) { - final String hrefDecoded = URLDecoder.decode(mdRef.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(mdRef.getHref()), Constants.UTF_8); final Long size = mdRef.getSIZE(); if (size != null) { final StringBuilder message = new StringBuilder(); @@ -627,7 +635,7 @@ protected ReporterDetails validateCSIP29(final StructureValidatorState structure if (checksum != null) { final String href = mdRef.getHref(); if (href != null) { - final String file = URLDecoder.decode(href, Constants.UTF_8); + final String file = URLDecoder.decode(DecoderUtils.normalizePath(href), Constants.UTF_8); final StringBuilder filePath = new StringBuilder(); final StringBuilder message = new StringBuilder(); if (structureValidatorState.isZipFileFlag()) { diff --git a/src/main/java/org/roda_project/commons_ip2/validator/components/fileSectionComponent/FileSecValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/components/fileSectionComponent/FileSecValidator.java index 92d83dd7..bf77ebf8 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/components/fileSectionComponent/FileSecValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/components/fileSectionComponent/FileSecValidator.java @@ -25,6 +25,7 @@ import org.roda_project.commons_ip2.validator.state.MetsValidatorState; import org.roda_project.commons_ip2.validator.state.StructureValidatorState; import org.roda_project.commons_ip2.validator.utils.CHECKSUMTYPE; +import org.roda_project.commons_ip2.validator.utils.DecoderUtils; import org.roda_project.commons_ip2.validator.utils.Message; /** @@ -97,7 +98,7 @@ protected ReporterDetails validateCSIP60(final StructureValidatorState structure final List fLocats = file.getFLocat(); if (structureValidatorState.isZipFileFlag()) { for (FileType.FLocat flocat : fLocats) { - final String href = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), Constants.UTF_8); final StringBuilder filePath = new StringBuilder(); if (metsValidatorState.isRootMets()) { filePath.append(metsValidatorState.getMets().getOBJID()).append(Constants.SEPARATOR).append(href); @@ -114,7 +115,8 @@ protected ReporterDetails validateCSIP60(final StructureValidatorState structure } } else { for (FileType.FLocat flocat : fLocats) { - final String filePath = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String filePath = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), + Constants.UTF_8); if (!structureValidatorState.getFolderManager() .checkPathExists(Paths.get(metsValidatorState.getMetsPath()).resolve(filePath))) { message.append("mets/fileSec/fileGrp[@USE=’Documentation’] ") @@ -150,7 +152,7 @@ protected ReporterDetails validateCSIP113(final StructureValidatorState structur final List fLocats = file.getFLocat(); if (structureValidatorState.isZipFileFlag()) { for (FileType.FLocat flocat : fLocats) { - final String href = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), Constants.UTF_8); final StringBuilder filePath = new StringBuilder(); if (metsValidatorState.isRootMets()) { filePath.append(metsValidatorState.getMets().getOBJID()).append(Constants.SEPARATOR).append(href); @@ -167,7 +169,7 @@ protected ReporterDetails validateCSIP113(final StructureValidatorState structur } } else { for (FileType.FLocat flocat : fLocats) { - final String filePath = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String filePath = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), Constants.UTF_8); if (!structureValidatorState.getFolderManager() .checkPathExists(Paths.get(metsValidatorState.getMetsPath()).resolve(filePath))) { message.append("mets/fileSec/fileGrp[@USE=’Schemas’] ") @@ -201,7 +203,7 @@ protected ReporterDetails validateCSIP114(final StructureValidatorState structur final List fLocats = file.getFLocat(); if (structureValidatorState.isZipFileFlag()) { for (FileType.FLocat flocat : fLocats) { - final String href = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), Constants.UTF_8); final StringBuilder filePath = new StringBuilder(); if (metsValidatorState.isRootMets()) { filePath.append(metsValidatorState.getMets().getOBJID()).append(Constants.SEPARATOR).append(href); @@ -218,7 +220,7 @@ protected ReporterDetails validateCSIP114(final StructureValidatorState structur } } else { for (FileType.FLocat flocat : fLocats) { - final String filePath = URLDecoder.decode(flocat.getHref(), Constants.UTF_8); + final String filePath = URLDecoder.decode(DecoderUtils.normalizePath(flocat.getHref()), Constants.UTF_8); if (!structureValidatorState.getFolderManager() .checkPathExists(Paths.get(metsValidatorState.getMetsPath()).resolve(filePath))) { message.append("mets/fileSec/fileGrp[@USE=’Representations’] ") @@ -448,7 +450,8 @@ protected ReporterDetails validateCSIP66(final StructureValidatorState structure final List fLocats = file.getFLocat(); if (!fLocats.isEmpty()) { for (FileType.FLocat fLocat : fLocats) { - final String hrefDecoded = URLDecoder.decode(fLocat.getHref(), Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(fLocat.getHref()), + Constants.UTF_8); final StringBuilder filePath = new StringBuilder(); if (structureValidatorState.isZipFileFlag()) { if (metsValidatorState.isRootMets()) { @@ -470,7 +473,7 @@ protected ReporterDetails validateCSIP66(final StructureValidatorState structure } if (metsValidatorState.getMetsFiles().containsValue(false) && metsValidatorState.isRootMets()) { return new ReporterDetails(getCSIPVersion(), - Message.createErrorMessage("You have files in SIP does not referenced in %1$s", + Message.createErrorMessage("You have files in SIP that are not referenced in %1$s", metsValidatorState.getMetsName(), metsValidatorState.isRootMets()), false, false); } @@ -553,7 +556,7 @@ protected ReporterDetails validateCSIP69(final StructureValidatorState structure final List flocat = file.getFLocat(); if (flocat != null) { if (flocat.size() == 1) { - final String href = URLDecoder.decode(flocat.get(0).getHref(), Constants.UTF_8); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(flocat.get(0).getHref()), Constants.UTF_8); if (href != null) { final Long size = file.getSIZE(); if (size != null) { @@ -685,7 +688,7 @@ protected ReporterDetails validateCSIP71(final StructureValidatorState structure metsValidatorState.getMetsName(), metsValidatorState.isRootMets()), false, false); } else { - final String filePath = URLDecoder.decode(href, Constants.UTF_8); + final String filePath = URLDecoder.decode(DecoderUtils.normalizePath(href), Constants.UTF_8); if (structureValidatorState.isZipFileFlag()) { final StringBuilder finalPath = new StringBuilder(); if (!metsValidatorState.isRootMets()) { @@ -1017,7 +1020,7 @@ protected ReporterDetails validateCSIP79(final StructureValidatorState structure for (FileType.FLocat floc : flocat) { final String href = floc.getHref(); if (href != null) { - final String hrefDecoded = URLDecoder.decode(href, Constants.UTF_8); + final String hrefDecoded = URLDecoder.decode(DecoderUtils.normalizePath(href), Constants.UTF_8); if (structureValidatorState.isZipFileFlag()) { final StringBuilder finalPath = new StringBuilder(); if (!metsValidatorState.isRootMets()) { diff --git a/src/main/java/org/roda_project/commons_ip2/validator/components/structuralMapComponent/StructMapValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/components/structuralMapComponent/StructMapValidator.java index db6c9abe..43cbedca 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/components/structuralMapComponent/StructMapValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/components/structuralMapComponent/StructMapValidator.java @@ -19,6 +19,7 @@ import org.roda_project.commons_ip2.validator.reporter.ReporterDetails; import org.roda_project.commons_ip2.validator.state.MetsValidatorState; import org.roda_project.commons_ip2.validator.state.StructureValidatorState; +import org.roda_project.commons_ip2.validator.utils.DecoderUtils; import org.roda_project.commons_ip2.validator.utils.Message; /** @@ -1426,7 +1427,7 @@ protected ReporterDetails validateCSIP110(final StructureValidatorState structur final List mptrs = d.getMptr(); if (!mptrs.isEmpty()) { for (DivType.Mptr mptr : mptrs) { - final String href = URLDecoder.decode(mptr.getHref(), "UTF-8"); + final String href = URLDecoder.decode(DecoderUtils.normalizePath(mptr.getHref()), "UTF-8"); if (structureValidatorState.isZipFileFlag()) { final StringBuilder filePath = new StringBuilder(); if (metsValidatorState.isRootMets()) { diff --git a/src/main/java/org/roda_project/commons_ip2/validator/utils/DecoderUtils.java b/src/main/java/org/roda_project/commons_ip2/validator/utils/DecoderUtils.java new file mode 100644 index 00000000..e7fdffc6 --- /dev/null +++ b/src/main/java/org/roda_project/commons_ip2/validator/utils/DecoderUtils.java @@ -0,0 +1,14 @@ +package org.roda_project.commons_ip2.validator.utils; + +import java.nio.file.Paths; + +/** + * @author Carlos Afonso + */ +public class DecoderUtils { + + public static String normalizePath(String href) { + return Paths.get(href).normalize().toString(); + } + +}