View Javadoc
1   /**
2    * This file Copyright (c) 2010-2016 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.cms.util;
35  
36  import info.magnolia.cms.core.AbstractContent;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.DefaultNodeData;
39  import info.magnolia.cms.core.NodeData;
40  
41  import java.util.Collection;
42  import java.util.Comparator;
43  import java.util.LinkedHashMap;
44  import java.util.SortedMap;
45  import java.util.TreeMap;
46  
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * This wrapper allows extending other nodes (mainly useful to extend configurations). A content node can define
56   * a nodeData with the name 'extends'. Its value is either an absolute or relative path. The merge is then performed as follows:
57   *
58   * <ul>
59   * <li>nodeDatas are merged and values are overwritten
60   * <li>sub content nodes are merged, the original order is guaranteed, new nodes are added at the
61   * end of the list
62   * </ul>
63   *
64   * The mechanism supports multiple inheritances as such:
65   * <ul>
66   * <li>the node the current node inherits from can again extend a node
67   * <li>nodes laying deeper in the hierarchy can extend an other node
68   * </ul>
69   *
70   * @see InheritanceContentWrapper a class supporting content inheritance.
71   * @deprecated since Magnolia 5.0 - use {@link info.magnolia.jcr.wrapper.ExtendingNodeWrapper} instead.
72   */
73  public class ExtendingContentWrapper extends ContentWrapper {
74  
75      protected static final String EXTENDING_NODE_DATA = "extends";
76      protected static final String EXTENDING_NODE_DATA_OVERRIDE = "override";
77  
78      private boolean extending;
79  
80      private Content extendedContent;
81  
82      private static final Logger log = LoggerFactory.getLogger(ExtendingContentWrapper.class);
83  
84      //This is just for test purposes. Use ExtendingContentWrapper(Content wrappedContent) as was here before.
85      ExtendingContentWrapper(Content wrappedContent, boolean failOnError) {
86  
87          super(wrappedContent);
88          try {
89              extending = getWrappedContent().hasNodeData(EXTENDING_NODE_DATA);
90              if (extending) {
91                  NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
92  
93                  // check if override is not forced
94                  String extendedNode = extendingNodeData.getString();
95  
96                  Content parent = extendingNodeData.getParent();
97  
98                  if (StringUtils.isBlank(extendedNode)) {
99                      // there is nothing to do, extending node is not defined ... probably caught in middle of config
100                     extending = false;
101                 } else if (EXTENDING_NODE_DATA_OVERRIDE.equals(extendedNode)) {
102                     extending = false;
103                 }
104                 if (extending) {
105                     if (isExists(extendedNode, parent)) {
106                         // support multiple inheritance
107                         extendedContent = wrapIfNeeded(extendingNodeData.getReferencedContent());
108                     } else {
109                         String message = "Can't find referenced node for value: " + wrappedContent;
110                         log.error(message);
111                         extending = false;
112                         if (failOnError) {
113                             throw new RuntimeException(message);
114                         }
115                     }
116 
117                 }
118 
119             }
120         } catch (RepositoryException e) {
121             throw new RuntimeException("Can't wrap node [" + wrappedContent + "]", e);
122         }
123     }
124 
125     public ExtendingContentWrapper(Content wrappedContent) {
126         this(wrappedContent, false);
127     }
128 
129     /**
130      * Does not support the extends nodedata but chains the two nodes directly. Each node is
131      * wrapped internally to ensure that each of them support the extends nodedata for themselves.
132      */
133     protected ExtendingContentWrapper(Content wrappedContent, Content extendedContent) {
134         super(wrapIfNeeded(wrappedContent));
135         extending = true;
136         try {
137             if (getWrappedContent().hasNodeData(EXTENDING_NODE_DATA)) {
138                 NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
139 
140                 // check if override is not forced
141                 extending = !EXTENDING_NODE_DATA_OVERRIDE.equals(extendingNodeData.getString());
142             }
143         } catch (RepositoryException e) {
144             throw new RuntimeException("Can't determine extends point for node [" + wrappedContent + "]", e);
145         }
146         // might extend further more
147         this.extendedContent = wrapIfNeeded(extendedContent);
148     }
149 
150     private boolean isExists(String extendedNode, Content parent) throws RepositoryException {
151         if (extendedNode.startsWith("/")) {
152             return getWrappedContent().getHierarchyManager().isExist(extendedNode);
153         }
154         return parent.hasContent(extendedNode);
155     }
156 
157     private static Content wrapIfNeeded(Content content) {
158         if (content instanceof ExtendingContentWrapper) {
159             return content;
160         }
161         return new ExtendingContentWrapper(content);
162     }
163 
164     public boolean isExtending() {
165         return this.extending;
166     }
167 
168     @Override
169     public Content getWrappedContent() {
170         Content wrapped = super.getWrappedContent();
171         if (wrapped instanceof ExtendingContentWrapper) {
172             ExtendingContentWrapper wrappedECW = (ExtendingContentWrapper) wrapped;
173             if (!wrappedECW.extending) {
174                 // wrapped but not extending ==> should not be wrapped in the first place but decision is made too late - in init<> of the ECW
175                 return ((ExtendingContentWrapper) wrapped).getWrappedContent();
176             }
177         }
178         return super.getWrappedContent();
179     }
180 
181     @Override
182     public boolean hasContent(String name) throws RepositoryException {
183         if (getWrappedContent().hasContent(name)) {
184             return true;
185         } else if (extending && extendedContent.hasContent(name)) {
186             return true;
187         }
188         return false;
189     }
190 
191     @Override
192     public Content getContent(String name) throws RepositoryException {
193         Content content;
194         if (getWrappedContent().hasContent(name)) {
195             content = getWrappedContent().getContent(name);
196         } else if (extending && extendedContent.hasContent(name)) {
197             content = extendedContent.getContent(name);
198         } else {
199             // this will throw a PathNotFoundException
200             content = getWrappedContent().getContent(name);
201         }
202         return wrap(content);
203     }
204 
205     @Override
206     public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
207         Collection<Content> directChildren = ((AbstractContent) getWrappedContent()).getChildren(filter, namePattern, orderCriteria);
208         if (extending) {
209             Collection<Content> inheritedChildren = ((AbstractContent) extendedContent).getChildren(filter, namePattern, orderCriteria);
210             // keep order, add new elements at the end of the collection
211             LinkedHashMap<String, Content> merged = new LinkedHashMap<String, Content>();
212             for (Content content : inheritedChildren) {
213                 merged.put(content.getName(), content);
214             }
215             for (Content content : directChildren) {
216                 merged.put(content.getName(), content);
217             }
218             return wrapContentNodes(merged.values());
219         }
220         return wrapContentNodes(directChildren);
221     }
222 
223     @Override
224     public NodeData getNodeData(String name) {
225         if (EXTENDING_NODE_DATA.equals(name)) {
226             return new DefaultNodeData(extendedContent, name) {
227                 @Override
228                 public boolean isExist() {
229                     return false;
230                 }
231 
232                 @Override
233                 public void save() throws RepositoryException {
234                     // do nothing
235                 }
236             };
237         }
238         try {
239             if (getWrappedContent().hasNodeData(name)) {
240                 return wrap(getWrappedContent().getNodeData(name));
241             } else if (extending && extendedContent.hasNodeData(name)) {
242                 return wrap(extendedContent.getNodeData(name));
243             } else {
244                 return wrap(getWrappedContent().getNodeData(name));
245             }
246         } catch (RepositoryException e) {
247             throw new RuntimeException("Can't read nodedata from extended node [" + extendedContent + "]", e);
248         }
249     }
250 
251     @Override
252     public boolean hasNodeData(String name) throws RepositoryException {
253         if (EXTENDING_NODE_DATA.equals(name)) {
254             return false;
255         }
256         return super.hasNodeData(name);
257     }
258 
259     @Override
260     public Collection<NodeData> getNodeDataCollection() {
261         final Content wrapped = getWrappedContent();
262         Collection<NodeData> directChildren = wrapped.getNodeDataCollection();
263         try {
264             if (wrapped.hasNodeData(EXTENDING_NODE_DATA)) {
265                 for (NodeData child : directChildren) {
266                     if (EXTENDING_NODE_DATA.equals(child.getName())) {
267                         directChildren.remove(child);
268                         break;
269                     }
270                 }
271             }
272         } catch (RepositoryException e) {
273             throw new RuntimeException("Can't read nodedata collection from node [" + wrapped.getHandle() + "]", e);
274         }
275         if (extending) {
276             Collection<NodeData> inheritedChildren = extendedContent.getNodeDataCollection();
277             // sort by name
278             SortedMap<String, NodeData> merged = new TreeMap<String, NodeData>();
279             for (NodeData nodeData : inheritedChildren) {
280                 merged.put(nodeData.getName(), nodeData);
281             }
282             for (NodeData nodeData : directChildren) {
283                 merged.put(nodeData.getName(), nodeData);
284             }
285             return wrapNodeDatas(merged.values());
286         }
287         return wrapNodeDatas(directChildren);
288     }
289 
290     @Override
291     protected Content wrap(Content node) {
292         // get the same subnode of the extended content
293         try {
294             if (extending && extendedContent.hasContent(node.getName())) {
295                 // FIXME we have to calculate the relative path
296                 Content extendedSubContent = extendedContent.getContent(node.getName());
297                 return new ExtendingContentWrapper(node, extendedSubContent);
298             }
299         } catch (RepositoryException e) {
300             throw new RuntimeException("Can't wrap " + node, e);
301         }
302         return wrapIfNeeded(node);
303     }
304 
305 }