View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.rendering.template.registry.validator;
35  
36  import static info.magnolia.config.registry.DefinitionProvider.Problem.DefaultTypes.RESOLUTION;
37  import static info.magnolia.config.registry.DefinitionProvider.Problem.*;
38  import static info.magnolia.util.DeprecationUtil.getDeprecationMessage;
39  
40  import info.magnolia.config.registry.DefinitionMetadata;
41  import info.magnolia.config.registry.DefinitionProvider;
42  import info.magnolia.config.registry.validator.DefinitionValidator;
43  import info.magnolia.rendering.DefinitionTypes;
44  import info.magnolia.rendering.model.RenderingModel;
45  import info.magnolia.rendering.renderer.registry.RendererRegistry;
46  import info.magnolia.rendering.template.AreaDefinition;
47  import info.magnolia.rendering.template.TemplateDefinition;
48  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
49  import info.magnolia.resourceloader.ResourceOrigin;
50  
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Optional;
54  
55  import javax.inject.Inject;
56  import javax.inject.Provider;
57  
58  
59  /**
60   * Validates {@link info.magnolia.rendering.template.TemplateDefinition}.
61   * Validates references to {@link info.magnolia.rendering.renderer.Renderer}s, another {@link info.magnolia.rendering.template.TemplateDefinition}s and template scripts.
62   */
63  public class TemplateDefinitionValidator implements DefinitionValidator<TemplateDefinition> {
64  
65      private final Provider<TemplateDefinitionRegistry> templateDefinitionRegistryProvider;
66      private final RendererRegistry rendererRegistry;
67      private final ResourceOrigin resourceOrigin;
68  
69      @Inject
70      public TemplateDefinitionValidator(Provider<TemplateDefinitionRegistry> templateDefinitionRegistryProvider, RendererRegistry rendererRegistry, ResourceOrigin resourceOrigin) {
71          this.templateDefinitionRegistryProvider = templateDefinitionRegistryProvider;
72          this.rendererRegistry = rendererRegistry;
73          this.resourceOrigin = resourceOrigin;
74      }
75  
76      @Override
77      public Collection<DefinitionProvider.Problem> validate(DefinitionProvider<TemplateDefinition> provider) {
78          final Collection<DefinitionProvider.Problem> problems = new ArrayList<>();
79          if (provider.isValid()) {
80              validateDefinition(problems, provider);
81          }
82          return problems;
83      }
84  
85      protected void validateDefinition(Collection<DefinitionProvider.Problem> problems, DefinitionProvider<TemplateDefinition> definitionProvider) {
86          TemplateDefinition definition = definitionProvider.get();
87          validateRenderType(problems, "", definition.getRenderType());
88          validateTemplateScript(problems, "", definition.getTemplateScript());
89          validateModelClass(problems, "", definition.getModelClass());
90          definition.getAreas().forEach((areaName, area) -> validateArea(definitionProvider, problems, "", area));
91      }
92  
93      protected void validateArea(DefinitionProvider<TemplateDefinition> definitionProvider, Collection<DefinitionProvider.Problem> problems, String rootPath, AreaDefinition areaDefinition) {
94          final String areaPath = rootPath + "areas/" + areaDefinition.getName() + "/";
95  
96          areaDefinition.getAvailableComponents().forEach((componentName, componentAvailability) -> {
97                      Optional<DefinitionMetadata> componentMetadataOptional = templateDefinitionRegistryProvider.get().getAllMetadata().stream()
98                              .filter(metadata -> metadata.getReferenceId().equals(componentAvailability.getId()))
99                              .findAny();
100 
101                     if (!componentMetadataOptional.isPresent()) {
102                         problems.add(major().withType(DefinitionProvider.Problem.DefaultTypes.REFERENCES)
103                                 .withTitle("Template availability problem")
104                                 .withDetails("Template {" + componentAvailability.getId() + "} is not registered.")
105                                 .withLocation(areaPath + "availableComponents/" + componentName)
106                                 .build());
107                     }
108                     // We want to only report if the definition at hand is not deprecated itself.
109                     else if (!definitionProvider.getMetadata().getDeprecation().isPresent() && componentMetadataOptional.get().getDeprecation().isPresent()) {
110                         DefinitionMetadata componentMetadata = componentMetadataOptional.get();
111                         componentMetadata.getDeprecation().ifPresent(deprecation -> {
112                             String deprecationMessage = getDeprecationMessage(DefinitionTypes.TEMPLATE.getName(), componentMetadata.getName(), deprecation.since(), deprecation.description());
113 
114                             problems.add(minor().withTitle("Deprecated definition usage")
115                                     .withDetails(deprecationMessage)
116                                     .withLocation(areaPath + "availableComponents/" + componentName)
117                                     .withType(RESOLUTION)
118                                     .build());
119                         });
120                     }
121                 }
122         );
123         validateTemplateScript(problems, areaPath, areaDefinition.getTemplateScript());
124         validateRenderType(problems, areaPath, areaDefinition.getRenderType());
125         validateModelClass(problems, areaPath, areaDefinition.getModelClass());
126         areaDefinition.getAreas().forEach(((areaName, area) -> validateArea(definitionProvider, problems, areaPath, area)));
127     }
128 
129     private void validateTemplateScript(Collection<DefinitionProvider.Problem> problems, String rootPath, String scriptPath) {
130         if (scriptPath != null && (!scriptPath.startsWith("/") || !resourceOrigin.hasPath(scriptPath))) {
131             problems.add(major()
132                     .withType(DefinitionProvider.Problem.DefaultTypes.REFERENCES)
133                     .withTitle("Template script definition problem")
134                     .withDetails("Resource {" + scriptPath + "} does not exist.")
135                     .withLocation(rootPath + "templateScript")
136                     .build());
137         }
138     }
139 
140     private void validateRenderType(Collection<DefinitionProvider.Problem> problems, String rootPath, String renderType) {
141         if (renderType != null && rendererRegistry.getAllMetadata().stream().noneMatch(metadata -> metadata.getReferenceId().equals(renderType))) {
142             problems.add(major()
143                     .withType(DefinitionProvider.Problem.DefaultTypes.REFERENCES)
144                     .withTitle("Template renderer definition problem")
145                     .withDetails("Renderer {" + renderType + "} is not registered.")
146                     .withLocation(rootPath + "renderType")
147                     .build());
148         }
149     }
150 
151     private void validateModelClass(Collection<DefinitionProvider.Problem> problems, String rootPath, Class<?> modelClass) {
152         if (modelClass != null && !RenderingModel.class.isAssignableFrom(modelClass)) {
153             problems.add(major()
154                     .withType(DefinitionProvider.Problem.DefaultTypes.REFERENCES)
155                     .withTitle("Model class definition problem")
156                     .withDetails("Model " + modelClass + " is not an instance of " + RenderingModel.class)
157                     .withLocation(rootPath + "modelClass")
158                     .build());
159         }
160     }
161 }