|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * AEM FileVault Content Package Namespace Validators |
| 4 | + * %% |
| 5 | + * Copyright (C) 2024 Cognizant Netcentric |
| 6 | + * %% |
| 7 | + * All rights reserved. This program and the accompanying materials are made available under the terms of the |
| 8 | + * Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 9 | + * https://www.eclipse.org/legal/epl-v20.html |
| 10 | + * SPDX-License-Identifier: EPL-2.0 |
| 11 | + * #L% |
| 12 | + */ |
| 13 | +package biz.netcentric.filevault.validator.aem.namespace; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.Spliterators; |
| 23 | +import java.util.jar.JarInputStream; |
| 24 | +import java.util.jar.Manifest; |
| 25 | +import java.util.regex.Pattern; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import java.util.stream.StreamSupport; |
| 28 | + |
| 29 | +import org.apache.jackrabbit.vault.validation.spi.GenericJcrDataValidator; |
| 30 | +import org.apache.jackrabbit.vault.validation.spi.ValidationMessage; |
| 31 | +import org.apache.jackrabbit.vault.validation.spi.ValidationMessageSeverity; |
| 32 | +import org.jetbrains.annotations.NotNull; |
| 33 | +import org.jetbrains.annotations.Nullable; |
| 34 | + |
| 35 | +public class EmbeddedNamespaceValidator implements GenericJcrDataValidator { |
| 36 | + |
| 37 | + private final ValidationMessageSeverity severity; |
| 38 | + private final Set<Pattern> allowedBundleSymbolicNamePatterns; |
| 39 | + |
| 40 | + public EmbeddedNamespaceValidator( |
| 41 | + ValidationMessageSeverity severity, Set<Pattern> allowedBundleSymbolicNamePatterns) { |
| 42 | + super(); |
| 43 | + this.severity = severity; |
| 44 | + this.allowedBundleSymbolicNamePatterns = allowedBundleSymbolicNamePatterns; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public @Nullable Collection<ValidationMessage> validateJcrData( |
| 49 | + @NotNull InputStream input, |
| 50 | + @NotNull Path filePath, |
| 51 | + @NotNull Path basePath, |
| 52 | + @NotNull Map<String, Integer> nodePathsAndLineNumbers) |
| 53 | + throws IOException { |
| 54 | + try (JarInputStream jarInputStream = new JarInputStream(input)) { |
| 55 | + String bundleSymbolicName = getBundleSymbolicName(jarInputStream.getManifest()); |
| 56 | + if (bundleSymbolicName == null) { |
| 57 | + return Collections.singleton(new ValidationMessage( |
| 58 | + ValidationMessageSeverity.WARN, |
| 59 | + "Either no manifest or no Bundle-SymbolicName header found in manifest. Skip evaluation!")); |
| 60 | + } |
| 61 | + if (allowedBundleSymbolicNamePatterns.stream() |
| 62 | + .noneMatch(pattern -> pattern.matcher(bundleSymbolicName).matches())) { |
| 63 | + return Collections.singleton(new ValidationMessage( |
| 64 | + severity, |
| 65 | + String.format( |
| 66 | + "Bundle-SymbolicName '%s' does not match any of the allowed patterns [%s]", |
| 67 | + bundleSymbolicName, |
| 68 | + allowedBundleSymbolicNamePatterns.stream() |
| 69 | + .map(Pattern::pattern) |
| 70 | + .collect(Collectors.joining(","))))); |
| 71 | + } |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + String getBundleSymbolicName(Manifest manifest) { |
| 77 | + if (manifest == null) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + return manifest.getMainAttributes().getValue("Bundle-SymbolicName"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean shouldValidateJcrData(@NotNull Path filePath, @NotNull Path basePath) { |
| 85 | + return isEmbeddedBundle(filePath); |
| 86 | + } |
| 87 | + |
| 88 | + static boolean isEmbeddedBundle(@NotNull Path filePath) { |
| 89 | + if (!filePath.getName(0).toString().equals("apps")) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + if (!filePath.getFileName().toString().endsWith(".jar")) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + return StreamSupport.stream(Spliterators.spliterator(filePath.iterator(), filePath.getNameCount(), 0), false) |
| 96 | + .limit(5) // max depth |
| 97 | + .map(Path::getFileName) |
| 98 | + .map(Path::toString) |
| 99 | + .anyMatch(name -> name.startsWith("install.") || name.equals("install")); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public @Nullable Collection<ValidationMessage> done() { |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
0 commit comments