From 8c49368d158b7acf8c7724cf29d87348afdedf91 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Mon, 22 Jan 2024 13:15:19 +0100 Subject: [PATCH] Eliminate SonarQube warnings --- .../aem/crypto/impl/AesCryptoSupport.java | 1 + .../ContentPackagePostProcessor.java | 3 +- .../aem/util/ContentElementHandler.java | 8 ++-- .../aem/util/ContentPackageBinaryFile.java | 3 +- .../plugins/aem/util/ContentPackageUtil.java | 38 ++++++++++--------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/crypto/impl/AesCryptoSupport.java b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/crypto/impl/AesCryptoSupport.java index 38936041..17d84c3d 100644 --- a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/crypto/impl/AesCryptoSupport.java +++ b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/crypto/impl/AesCryptoSupport.java @@ -37,6 +37,7 @@ /** * AES crypto support implementation with the same algorithms as used by AEM 6.3 and up. */ +@SuppressWarnings("java:S5542") // cannot use a more secure cipher mode, we have to be compatible with AEM public class AesCryptoSupport extends CryptoSupport { private static final String AES_KEY_ALGORITHM = "AES"; diff --git a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/postprocessor/ContentPackagePostProcessor.java b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/postprocessor/ContentPackagePostProcessor.java index 07619e87..9a9eff93 100644 --- a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/postprocessor/ContentPackagePostProcessor.java +++ b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/postprocessor/ContentPackagePostProcessor.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -106,7 +107,7 @@ public List apply(FileContext fileContext, PostProcessorContext con } // delete provisioning file after transformation - file.delete(); + Files.delete(file.toPath()); // set force to true by default for CONGA-generated packages (but allow override from role definition) Map modelOptions = new HashMap<>(); diff --git a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentElementHandler.java b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentElementHandler.java index 0985ddfc..110d8447 100644 --- a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentElementHandler.java +++ b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentElementHandler.java @@ -35,7 +35,7 @@ final class ContentElementHandler implements ContentHandler { private ContentElement root; - private static final Pattern PATH_PATTERN = Pattern.compile("^((/[^/]+)*)(/([^/]+))$"); + private static final Pattern PATH_PATTERN = Pattern.compile("^((/[^/]+)*+)(/([^/]+))$"); @Override public void resource(String path, Map properties) { @@ -44,11 +44,11 @@ public void resource(String path, Map properties) { } else { if (root == null) { - throw new RuntimeException("Root resource not set."); + throw new IllegalArgumentException("Root resource not set."); } Matcher matcher = PATH_PATTERN.matcher(path); if (!matcher.matches()) { - throw new RuntimeException("Unexpected path:" + path); + throw new IllegalArgumentException("Unexpected path:" + path); } String relativeParentPath = StringUtils.stripStart(matcher.group(1), "/"); String name = matcher.group(4); @@ -60,7 +60,7 @@ public void resource(String path, Map properties) { parent = root.getChild(relativeParentPath); } if (parent == null) { - throw new RuntimeException("Parent '" + relativeParentPath + "' does not exist."); + throw new IllegalArgumentException("Parent '" + relativeParentPath + "' does not exist."); } parent.getChildren().put(name, new ContentElementImpl(name, properties)); } diff --git a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageBinaryFile.java b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageBinaryFile.java index 980ea67a..4a29d4da 100644 --- a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageBinaryFile.java +++ b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageBinaryFile.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import org.apache.commons.lang3.StringUtils; @@ -101,7 +102,7 @@ public void deleteIfRequired(UrlFileManager urlFileManager, File targetDir) thro } File file = getFile(targetDir); if (file != null) { - file.delete(); + Files.delete(file.toPath()); } } } diff --git a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageUtil.java b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageUtil.java index 233ac6ef..c0fc5496 100644 --- a/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageUtil.java +++ b/conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/util/ContentPackageUtil.java @@ -47,6 +47,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; @@ -395,24 +396,25 @@ private static List getMatchingFiles(File targetDir, S Pattern pattern = Pattern.compile(fileMatch); // collect all files below the target dir - return Files.walk(Paths.get(fileTargetDir.toURI())) - .filter(Files::isRegularFile) - // strip off the target dir paths, keep only the relative path/file name - .map(ContentPackageUtil::normalizedAbsolutePath) - .map(file -> StringUtils.removeStart(file, targetPathPrefix)) - // check if file matches with the regex, apply matching input groups to path - .map(file -> { - Matcher matcher = pattern.matcher(file); - if (matcher.matches()) { - String adaptedPath = matcher.replaceAll(path); - return new ContentPackageBinaryFile(file, dir, null, adaptedPath, delete); - } - else { - return null; - } - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + try (Stream paths = Files.walk(Paths.get(fileTargetDir.toURI()))) { + return paths.filter(Files::isRegularFile) + // strip off the target dir paths, keep only the relative path/file name + .map(ContentPackageUtil::normalizedAbsolutePath) + .map(file -> StringUtils.removeStart(file, targetPathPrefix)) + // check if file matches with the regex, apply matching input groups to path + .map(file -> { + Matcher matcher = pattern.matcher(file); + if (matcher.matches()) { + String adaptedPath = matcher.replaceAll(path); + return new ContentPackageBinaryFile(file, dir, null, adaptedPath, delete); + } + else { + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } } private static String normalizedAbsolutePath(Path path) {