1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.tooling.maven.plugin.validation;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.maven.plugin.MojoFailureException;
24
25 import com.github.jknack.handlebars.Handlebars;
26
27 import io.wcm.devops.conga.generator.handlebars.HandlebarsManager;
28 import io.wcm.devops.conga.generator.plugins.handlebars.escaping.NoneEscapingStrategy;
29 import io.wcm.devops.conga.generator.util.FileUtil;
30 import io.wcm.devops.conga.model.reader.ModelReader;
31 import io.wcm.devops.conga.model.reader.RoleReader;
32 import io.wcm.devops.conga.model.role.Role;
33 import io.wcm.devops.conga.model.role.RoleFile;
34 import io.wcm.devops.conga.resource.Resource;
35
36
37
38
39 public final class RoleTemplateFileValidator implements DefinitionValidator<Void> {
40
41 private final ModelReader<Role> modelReader = new RoleReader();
42
43 private final HandlebarsManager handlebarsManager;
44
45
46
47
48
49 public RoleTemplateFileValidator(HandlebarsManager handlebarsManager) {
50 this.handlebarsManager = handlebarsManager;
51 }
52
53 @Override
54 @SuppressWarnings({
55 "PMD.PreserveStackTrace", "PMD.ExceptionAsFlowControl"
56 })
57 public Void validate(Resource resource, String pathForLog) throws MojoFailureException {
58 try {
59 Role role = modelReader.read(resource);
60 for (RoleFile roleFile : role.getFiles()) {
61
62
63 String templateFile = FileUtil.getTemplatePath(role, roleFile);
64 if (StringUtils.isNotEmpty(templateFile)) {
65 Handlebars handlebars = handlebarsManager.get(NoneEscapingStrategy.NAME, roleFile.getCharset());
66 handlebars.compile(templateFile);
67 }
68
69
70 boolean hasTemplate = StringUtils.isNotEmpty(roleFile.getTemplate());
71 boolean hasUrl = StringUtils.isNotEmpty(roleFile.getUrl());
72 boolean hasSymlinkTarget = StringUtils.isNotEmpty(roleFile.getSymlinkTarget());
73 if ((hasTemplate && hasUrl) || (hasTemplate && hasSymlinkTarget) || (hasUrl && hasSymlinkTarget)) {
74 throw new IllegalArgumentException("Only one of the attributes 'template', 'url', 'symlinkTarget' is allowed for a file definition.");
75 }
76
77 }
78 }
79 catch (Exception ex) {
80 throw new MojoFailureException("Role definition " + pathForLog + " is invalid:\n" + ex.getMessage());
81 }
82 return null;
83 }
84
85 }