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     * @param handlebarsManager Handlebars Manager
47     */
48    public RoleTemplateFileValidator(HandlebarsManager handlebarsManager) {
49      this.handlebarsManager = handlebarsManager;
50    }
51  
52    @Override
53    @SuppressWarnings("PMD.PreserveStackTrace")
54    public Void validate(Resource resource, String pathForLog) throws MojoFailureException {
55      try {
56        Role role = modelReader.read(resource);
57        for (RoleFile roleFile : role.getFiles()) {
58  
59          // validate template file
60          String templateFile = FileUtil.getTemplatePath(role, roleFile);
61          if (StringUtils.isNotEmpty(templateFile)) {
62            Handlebars handlebars = handlebarsManager.get(NoneEscapingStrategy.NAME, roleFile.getCharset());
63            handlebars.compile(templateFile);
64          }
65  
66          // ensure only one of the parameters template, url, symlinkTarget is defined for a file
67          boolean hasTemplate = StringUtils.isNotEmpty(roleFile.getTemplate());
68          boolean hasUrl = StringUtils.isNotEmpty(roleFile.getUrl());
69          boolean hasSymlinkTarget = StringUtils.isNotEmpty(roleFile.getSymlinkTarget());
70          if ((hasTemplate && hasUrl) || (hasTemplate && hasSymlinkTarget) || (hasUrl && hasSymlinkTarget)) {
71            throw new IllegalArgumentException("Only one of the attributes 'template', 'url', 'symlinkTarget' is allowed for a file definition.");
72          }
73  
74        }
75      }
76      /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
77        throw new MojoFailureException("Role definition " + pathForLog + " is invalid:\n" + ex.getMessage());
78      }
79      return null;
80    }
81  
82  }