View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.config.source.yaml.construct;
35  
36  import static info.magnolia.config.source.yaml.dependency.YamlDependencyResoutionProblemType.DEPENDENCY_RESOLUTION;
37  
38  import info.magnolia.config.maputil.ConfigurationMapOverlay;
39  import info.magnolia.config.maputil.ToMap;
40  import info.magnolia.config.registry.DefinitionProvider.Problem;
41  import info.magnolia.config.source.yaml.YamlReader;
42  import info.magnolia.config.source.yaml.dependency.YamlFileDependency;
43  import info.magnolia.resourceloader.ResourceOrigin;
44  
45  import java.util.Map;
46  import java.util.function.Consumer;
47  import java.util.regex.Matcher;
48  import java.util.regex.Pattern;
49  
50  import org.apache.commons.lang3.StringUtils;
51  import org.yaml.snakeyaml.nodes.Node;
52  import org.yaml.snakeyaml.nodes.ScalarNode;
53  
54  /**
55   * Adds support of the yaml resource inclusion with the following syntax: <b>!include:&lt;resource_path.yaml&gt;</b>. Unlike the
56   * {@link IncludeYamlFile} construct this one allows us to also override the nodes and properties of the included file
57   * making the inclusion process significantly more flexible.
58   */
59  public final class IncludeFileYamlWithModificationPossibility extends MgnlYamlConstruct {
60  
61      public static final String TAG_PREFIX = "!include:";
62  
63      private static final Pattern INCLUSION_MATCHER = Pattern.compile("^!include:(?<path>[0-9a-zA-Z_/-]+.yaml)$");
64  
65      private final YamlReader yamlReader;
66      private final ResourceOrigin<?> resourceOrigin;
67  
68      public IncludeFileYamlWithModificationPossibility(YamlReader yamlReader, ResourceOrigin<?> resourceOrigin, Consumer<Problem> problemCollector) {
69          super(problemCollector);
70          this.yamlReader = yamlReader;
71          this.resourceOrigin = resourceOrigin;
72      }
73  
74      @Override
75      public Object construct(Node node) {
76          final Map<String, Object> baseData = ToMap.toMap(getConstructor().getConstructByNodeType(node).construct(node));
77  
78          if (node instanceof ScalarNode) {
79              final String scalarValue = ((ScalarNode) node).getValue();
80              if (StringUtils.isNotBlank(scalarValue)) {
81                  reportProblem(
82                          Problem.minor()
83                                  .withType(DEPENDENCY_RESOLUTION)
84                                  .withTitle("Redundant inclusion syntax used")
85                                  .withDetails(String.format("Scalar value [%s] of node [%s] will be ignored due to !include:<resource_path> semantics",
86                                          scalarValue, node.getNodeId()))
87                                  .build());
88              }
89          }
90  
91          final Matcher resourcePathMatcher = INCLUSION_MATCHER.matcher(node.getTag().getValue());
92          if (!resourcePathMatcher.matches()) {
93              reportProblem(
94                      Problem.severe()
95                              .withType(DEPENDENCY_RESOLUTION)
96                              .withTitle("Mis-configured YAML resource dependency")
97                              .withDetails(String.format("Tag [%s] does not match the inclusion pattern !include:<yaml_resource_path>",
98                                      node.getTag().getValue()))
99                              .build());
100             return baseData;
101         }
102 
103         final String path = resourcePathMatcher.group("path");
104         final YamlFileDependency dependency = new YamlFileDependency(resourceOrigin, path, yamlReader, getConstructor().getDependencyAggregator(), this::reportProblem);
105 
106         if (!dependency.exists()) {
107             reportProblem(
108                     Problem.major()
109                             .withType(DEPENDENCY_RESOLUTION)
110                             .withTitle("Missing YAML resource dependency")
111                             .withDetails(String.format("Resource dependency at [%s] does not exist", path))
112                             .build());
113         }
114 
115         getConstructor().getDependencyAggregator().addDependency(dependency);
116 
117         return ConfigurationMapOverlay
118                 .of(ToMap.toMap(dependency.readData()))
119                 .by(baseData)
120                 .at("/")
121                 .overlay();
122     }
123 }