Search in sources :

Example 1 with Aspect

use of mpern.sap.commerce.ccv2.model.Aspect in project commerce-gradle-plugin by SAP.

the class UseConfigValidator method validateProperties.

private List<Error> validateProperties(Manifest manifest) {
    List<Error> errors = new ArrayList<>();
    for (int i = 0; i < manifest.useConfig.properties.size(); i++) {
        Properties properties = manifest.useConfig.properties.get(i);
        Tuple2<Path, List<Error>> result = validateAndNormalizePath(this.projectRoot, String.format("useConfig.properties[%d]", i), properties.location);
        errors.addAll(result.getSecond());
        if (!properties.aspect.isEmpty() && !ALLOWED_ASPECTS.contains(properties.aspect)) {
            errors.add(new Error.Builder().setLocation("useConfig.properties[%d]", i).setMessage("Aspect `%s` not supported", properties.aspect).setCode("E-002").createError());
        }
        if (!properties.persona.isEmpty() && !ALLOWED_PERSONAS.contains(properties.persona)) {
            errors.add(new Error.Builder().setLocation("useConfig.properties[%d]", i).setMessage("Persona `%s` not supported", properties.persona).setCode("E-008").createError());
        }
        if (result.getFirst() != null) {
            try (InputStream stream = Files.newInputStream(result.getFirst())) {
                new java.util.Properties().load(stream);
            } catch (Exception e) {
                errors.add(new Error.Builder().setLocation("useConfig.properties[%d]", i).setMessage("`%s` is not a valid Java properties file", properties.location).setCode("E-010").createError());
            }
            try {
                // ref. java doc of java.util.Properties.load(InputStream)
                String defaultCharset = String.join("\n", Files.readAllLines(result.getFirst(), StandardCharsets.ISO_8859_1));
                String utf8 = String.join("\n", Files.readAllLines(result.getFirst(), StandardCharsets.UTF_8));
                if (!defaultCharset.equals(utf8)) {
                    errors.add(new Error.Builder().setLocation("useConfig.properties[%d]", i).setLevel(Level.WARNING).setMessage("`%s` seems to use a different charset than ISO 8859-1. This might lead to corrupted properties after build and deployment.", properties.location).setCode("W-002").createError());
                }
            } catch (IOException e) {
            // shouldn't happen
            }
        }
    }
    return errors;
}
Also used : ValidationUtils.validateAndNormalizePath(mpern.sap.commerce.ccv2.validation.ValidationUtils.validateAndNormalizePath) Path(java.nio.file.Path) InputStream(java.io.InputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) Error(mpern.sap.commerce.ccv2.validation.Error) IOException(java.io.IOException) Properties(mpern.sap.commerce.ccv2.model.useconfig.Properties) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Aspect

use of mpern.sap.commerce.ccv2.model.Aspect in project commerce-gradle-plugin by SAP.

the class AspectValidator method validate.

@Override
public List<Error> validate(Manifest manifest) throws Exception {
    List<Error> errors = new ArrayList<>();
    Map<String, Integer> seenAspects = new HashMap<>();
    for (int aspectIndex = 0; aspectIndex < manifest.aspects.size(); aspectIndex++) {
        Aspect aspect = manifest.aspects.get(aspectIndex);
        if (!Aspect.ALLOWED_ASPECTS.contains(aspect.name)) {
            errors.add(new Error.Builder().setLocation("aspects[?name == '%s']", aspect.name).setMessage("Aspect `%s` not supported", aspect.name).setCode("E-002").createError());
        } else {
            Integer previous = seenAspects.put(aspect.name, aspectIndex);
            if (previous != null) {
                errors.add(new Error.Builder().setLocation("aspects[%d]", aspectIndex).setMessage("Aspect `%s` configured more than once. Previous location: `aspects[%d]", aspect.name, previous).setCode("E-003").createError());
            }
            errors.addAll(new SharedPropertyValidator(String.format("aspects[?name == '%s'].", aspect.name)).validateProperties(aspect.properties));
            if (ADMIN_ASPECT.equals(aspect.name)) {
                if (!aspect.webapps.isEmpty()) {
                    errors.add(new Error.Builder().setLocation("aspects[?name == '%s']", aspect.name).setMessage("Webapps not allowed for aspect `admin`").setCode("E-007").createError());
                }
            } else {
                Map<String, Integer> loadedExtensions = new HashMap<>();
                Map<String, Integer> webroots = new HashMap<>();
                for (int j = 0; j < aspect.webapps.size(); j++) {
                    Webapp w = aspect.webapps.get(j);
                    previous = loadedExtensions.put(w.name, j);
                    if (previous != null) {
                        errors.add(new Error.Builder().setLocation("aspects[?name == '%s'].webapps[%d]", aspect.name, j).setMessage("Extension `%s` configured more than once. Previous location: `aspects[?name == '%s'].webapps[%d]`", w.name, aspect.name, previous).setCode("E-004").createError());
                    }
                    previous = webroots.put(w.contextPath, j);
                    if (previous != null) {
                        errors.add(new Error.Builder().setLocation("aspects[?name == '%s'].webapps[%d]", aspect.name, j).setMessage("Context path `%s` configured more than once! Previous location: `aspects[?name == '%s'].webapps[%d]`", w.contextPath, aspect.name, previous).setCode("E-005").createError());
                    }
                    if (!w.contextPath.isEmpty() && !w.contextPath.startsWith("/")) {
                        errors.add(new Error.Builder().setLocation("aspects[?name == '%s'].webapps[%d]", aspect.name, j).setMessage("contextPath `%s` must start with `/`", w.contextPath).setCode("E-006").createError());
                    }
                }
            }
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Error(mpern.sap.commerce.ccv2.validation.Error) Aspect(mpern.sap.commerce.ccv2.model.Aspect) Webapp(mpern.sap.commerce.ccv2.model.Webapp)

Example 3 with Aspect

use of mpern.sap.commerce.ccv2.model.Aspect in project commerce-gradle-plugin by SAP.

the class WebrootValidator method validate.

@Override
public List<Error> validate(Manifest manifest) throws Exception {
    Map<String, Set<String>> occurences = new HashMap<>();
    for (int i = 0; i < manifest.properties.size(); i++) {
        Property p = manifest.properties.get(i);
        String location = String.format("properties[%d]", i);
        checkProperty(p.key, location, occurences);
    }
    for (int i = 0; i < manifest.useConfig.properties.size(); i++) {
        Properties properties = manifest.useConfig.properties.get(i);
        Tuple2<Path, List<Error>> result = ValidationUtils.validateAndNormalizePath(this.projectRoot, "", properties.location);
        if (result.getFirst() != null) {
            try (InputStream stream = Files.newInputStream(result.getFirst())) {
                java.util.Properties props = new java.util.Properties();
                props.load(stream);
                String location = String.format("useConfig.properties[%d].location (%s)", i, properties.location);
                for (Map.Entry<Object, Object> entry : props.entrySet()) {
                    checkProperty((String) entry.getKey(), location, occurences);
                }
            } catch (IOException e) {
            // ignore
            }
        }
    }
    for (Aspect aspect : manifest.aspects) {
        for (int i = 0; i < aspect.properties.size(); i++) {
            Property p = aspect.properties.get(i);
            String location = String.format("aspects[?name == '%s'].properties[%d]", aspect.name, i);
            checkProperty(p.key, location, occurences);
        }
    }
    if (occurences.isEmpty()) {
        return Collections.emptyList();
    } else {
        List<Error> errors = new ArrayList<>();
        for (Map.Entry<String, Set<String>> errorEntry : occurences.entrySet()) {
            errors.add(new Error.Builder().setLocation(errorEntry.getKey()).setCode("E-017").setMessage("Do not configure webroots in properties.\nFaulty properties:\n- " + String.join("\n -", errorEntry.getValue())).createError());
        }
        return errors;
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) Error(mpern.sap.commerce.ccv2.validation.Error) IOException(java.io.IOException) Properties(mpern.sap.commerce.ccv2.model.useconfig.Properties) Aspect(mpern.sap.commerce.ccv2.model.Aspect) java.util(java.util) Property(mpern.sap.commerce.ccv2.model.Property)

Example 4 with Aspect

use of mpern.sap.commerce.ccv2.model.Aspect in project commerce-gradle-plugin by SAP.

the class AspectWebappValidator method validateWithExtensions.

@Override
protected List<Error> validateWithExtensions(Manifest manifest, ExtensionsResolver.Result effectiveExtensions) {
    List<Error> errors = new ArrayList<>();
    Set<String> extensionNames = effectiveExtensions.extensions.stream().map(e -> e.name).collect(Collectors.toSet());
    for (int i = 0; i < manifest.aspects.size(); i++) {
        Aspect aspect = manifest.aspects.get(i);
        if (ADMIN_ASPECT.equals(aspect.name)) {
            continue;
        }
        for (int j = 0; j < aspect.webapps.size(); j++) {
            Webapp w = aspect.webapps.get(j);
            // extension does not exist / not loaded
            if (!extensionNames.contains(w.name)) {
                errors.add(new Error.Builder().setLocation("aspects[?name == '%s'].webapps[%d]", aspect.name, i).setMessage("Extension `%s` not available.\n%s", w.name, formatLocations(effectiveExtensions.locations)).setCode("E-001").createError());
            }
        }
    }
    return errors;
}
Also used : Error(mpern.sap.commerce.ccv2.validation.Error) List(java.util.List) ExtensionsResolver(mpern.sap.commerce.ccv2.validation.ExtensionsResolver) Manifest(mpern.sap.commerce.ccv2.model.Manifest) Set(java.util.Set) ADMIN_ASPECT(mpern.sap.commerce.ccv2.model.Aspect.ADMIN_ASPECT) Aspect(mpern.sap.commerce.ccv2.model.Aspect) Collectors(java.util.stream.Collectors) Webapp(mpern.sap.commerce.ccv2.model.Webapp) ExtensionValidator(mpern.sap.commerce.ccv2.validation.ExtensionValidator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Error(mpern.sap.commerce.ccv2.validation.Error) Aspect(mpern.sap.commerce.ccv2.model.Aspect) Webapp(mpern.sap.commerce.ccv2.model.Webapp)

Aggregations

Error (mpern.sap.commerce.ccv2.validation.Error)4 ArrayList (java.util.ArrayList)3 Aspect (mpern.sap.commerce.ccv2.model.Aspect)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 List (java.util.List)2 Webapp (mpern.sap.commerce.ccv2.model.Webapp)2 Properties (mpern.sap.commerce.ccv2.model.useconfig.Properties)2 java.util (java.util)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ADMIN_ASPECT (mpern.sap.commerce.ccv2.model.Aspect.ADMIN_ASPECT)1 Manifest (mpern.sap.commerce.ccv2.model.Manifest)1 Property (mpern.sap.commerce.ccv2.model.Property)1 ExtensionValidator (mpern.sap.commerce.ccv2.validation.ExtensionValidator)1 ExtensionsResolver (mpern.sap.commerce.ccv2.validation.ExtensionsResolver)1