View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Ensures that all template files references in role definitions point to an existing template.
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     * Constructor.
47     * @param handlebarsManager Handlebars Manager
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          // validate template file
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          // ensure only one of the parameters template, url, symlinkTarget is defined for a file
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      /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
80        throw new MojoFailureException("Role definition " + pathForLog + " is invalid:\n" + ex.getMessage());
81      }
82      return null;
83    }
84  
85  }