| 
 | 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 (allowedBundleSymbolicNamePatterns.stream()  | 
 | 57 | +                    .noneMatch(pattern -> pattern.matcher(bundleSymbolicName).matches())) {  | 
 | 58 | +                return Collections.singleton(new ValidationMessage(  | 
 | 59 | +                        severity,  | 
 | 60 | +                        String.format(  | 
 | 61 | +                                "Bundle-SymbolicName '%s' does not match any of the allowed patterns [%s]",  | 
 | 62 | +                                bundleSymbolicName,  | 
 | 63 | +                                allowedBundleSymbolicNamePatterns.stream()  | 
 | 64 | +                                        .map(Pattern::pattern)  | 
 | 65 | +                                        .collect(Collectors.joining(",")))));  | 
 | 66 | +            }  | 
 | 67 | +        }  | 
 | 68 | + | 
 | 69 | +        return GenericJcrDataValidator.super.validateJcrData(input, filePath, basePath, nodePathsAndLineNumbers);  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    String getBundleSymbolicName(Manifest manifest) {  | 
 | 73 | +        return manifest.getMainAttributes().getValue("Bundle-SymbolicName");  | 
 | 74 | +    }  | 
 | 75 | + | 
 | 76 | +    @Override  | 
 | 77 | +    public boolean shouldValidateJcrData(@NotNull Path filePath, @NotNull Path basePath) {  | 
 | 78 | +        return isEmbeddedBundle(filePath);  | 
 | 79 | +    }  | 
 | 80 | + | 
 | 81 | +    static boolean isEmbeddedBundle(@NotNull Path filePath) {  | 
 | 82 | +        if (!filePath.getName(0).toString().equals("apps")) {  | 
 | 83 | +            return false;  | 
 | 84 | +        }  | 
 | 85 | +        if (!filePath.getFileName().toString().endsWith(".jar")) {  | 
 | 86 | +            return false;  | 
 | 87 | +        }  | 
 | 88 | +        return StreamSupport.stream(Spliterators.spliterator(filePath.iterator(), filePath.getNameCount(), 0), false)  | 
 | 89 | +                .limit(5) // max depth  | 
 | 90 | +                .map(Path::getFileName)  | 
 | 91 | +                .map(Path::toString)  | 
 | 92 | +                .anyMatch(name -> name.startsWith("install.") || name.equals("install"));  | 
 | 93 | +    }  | 
 | 94 | + | 
 | 95 | +    @Override  | 
 | 96 | +    public @Nullable Collection<ValidationMessage> done() {  | 
 | 97 | +        return null;  | 
 | 98 | +    }  | 
 | 99 | +}  | 
0 commit comments