View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.core.Path;
40  import info.magnolia.cms.core.Content.ContentFilter;
41  import info.magnolia.cms.security.AccessDeniedException;
42  import info.magnolia.context.MgnlContext;
43  
44  import java.io.File;
45  import java.io.FileInputStream;
46  import java.io.FileOutputStream;
47  import java.io.IOException;
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.Comparator;
51  import java.util.Iterator;
52  import java.util.List;
53  import java.util.regex.Matcher;
54  import java.util.regex.Pattern;
55  
56  import javax.jcr.ImportUUIDBehavior;
57  import javax.jcr.PathNotFoundException;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.Session;
60  
61  import org.apache.commons.io.FileUtils;
62  import org.apache.commons.io.IOUtils;
63  import org.apache.commons.lang.StringUtils;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * Some easy to use methods to handle with Content objects.
69   * @author philipp
70   */
71  public class ContentUtil {
72      private final static Logger log = LoggerFactory.getLogger(ContentUtil.class);
73  
74      /**
75       * Content filter accepting everything
76       */
77      public static ContentFilter ALL_NODES_CONTENT_FILTER = new ContentFilter() {
78  
79          public boolean accept(Content content) {
80              return true;
81          }
82      };
83  
84      /**
85       * Content filter accepting everything exept nodes with namespace jcr (version and system store)
86       */
87      public static ContentFilter ALL_NODES_EXCEPT_JCR_CONTENT_FILTER = new ContentFilter() {
88  
89          public boolean accept(Content content) {
90              return !content.getName().startsWith("jcr:");
91          }
92      };
93  
94      /**
95       * Content filter accepting everything except meta data and jcr:
96       */
97      public static ContentFilter EXCLUDE_META_DATA_CONTENT_FILTER = new ContentFilter() {
98          public boolean accept(Content content) {
99              return !content.getName().startsWith("jcr:") && !content.isNodeType(ItemType.NT_METADATA);
100         }
101     };
102 
103     /**
104      * Content filter accepting all nodes with a nodetype of namespace mgnl
105      */
106     public static ContentFilter MAGNOLIA_FILTER = new ContentFilter() {
107         public boolean accept(Content content) {
108 
109             try {
110                 String nodetype = content.getNodeType().getName();
111                 // export only "magnolia" nodes
112                 return nodetype.startsWith("mgnl:");
113             }
114             catch (RepositoryException e) {
115                 log.error("Unable to read nodetype for node {}", content.getHandle());
116             }
117             return false;
118         }
119     };
120 
121     /**
122      * @author Philipp Bracher
123      * @version $Id: ContentUtil.java 34893 2010-06-08 06:09:24Z dlipp $
124      *
125      */ // TODO : throws RepositoryException or none, but not Exception !?
126     public static interface Visitor {
127         void visit(Content node) throws Exception;
128     }
129 
130     public static interface PostVisitor extends Visitor {
131         void postVisit(Content node) throws Exception;
132     }
133 
134 
135     /**
136      * Returns a Content object of the named repository or null if not existing.
137      * @param repository
138      * @param path
139      * @return null if not found
140      */
141     public static Content getContent(String repository, String path) {
142         try {
143             return MgnlContext.getHierarchyManager(repository).getContent(path);
144         }
145         catch (RepositoryException e) {
146             return null;
147         }
148     }
149 
150     /**
151      * @return null if not found
152      */
153     public static Content getContentByUUID(String repository, String uuid) {
154         try {
155             return MgnlContext.getHierarchyManager(repository).getContentByUUID(uuid);
156         }
157         catch (RepositoryException e) {
158             return null;
159         }
160     }
161 
162 
163     /**
164      * Get the node or null if not exists
165      * @param node
166      * @param name
167      * @return the sub node
168      */
169     public static Content getContent(Content node, String name) {
170         try {
171             return node.getContent(name);
172         }
173         catch (RepositoryException e) {
174             return null;
175         }
176     }
177 
178     /**
179      * If the node doesn't exist just create it. Attention the method does not save the newly created node.
180      */
181     public static Content getOrCreateContent(Content node, String name, ItemType contentType) throws AccessDeniedException, RepositoryException{
182         return getOrCreateContent(node, name, contentType, false);
183     }
184 
185     /**
186      * If the node doesn't exist just create it. If the parameter save is true the parent node is saved.
187      */
188     public static Content getOrCreateContent(Content node, String name, ItemType contentType, boolean save)
189         throws AccessDeniedException, RepositoryException {
190         Content res = null;
191         try {
192             res = node.getContent(name);
193         }
194         catch (PathNotFoundException e) {
195             res = node.createContent(name, contentType);
196             if(save){
197                 res.getParent().save();
198             }
199         }
200         return res;
201     }
202 
203     /**
204      * Get a subnode case insensitive.
205      * @param node
206      * @param name
207      * @return
208      */
209     public static Content getCaseInsensitive(Content node, String name) {
210         if (name == null || node == null) {
211             return null;
212         }
213         name = name.toLowerCase();
214         for (Content child : node.getChildren(ALL_NODES_CONTENT_FILTER)) {
215             if (child.getName().toLowerCase().equals(name)) {
216                 return child;
217             }
218         }
219         return null;
220     }
221 
222     /**
223      * Get all children recursively (content and contentnode)
224      */
225     public static List<Content> collectAllChildren(Content node) {
226         List<Content> nodes = new ArrayList<Content>();
227         return collectAllChildren(nodes, node, new ItemType[]{ItemType.CONTENT, ItemType.CONTENTNODE});
228     }
229 
230     /**
231      * Get all children using a filter
232      * @param node
233      * @param filter
234      * @return list of all found nodes
235      */
236     public static List<Content> collectAllChildren(Content node, ContentFilter filter) {
237         List<Content> nodes = new ArrayList<Content>();
238         return collectAllChildren(nodes, node, filter);
239     }
240 
241     /**
242      * Get the children using a filter
243      * @param nodes collection of already found nodes
244      * @param node
245      * @param filter the filter to use
246      * @return
247      */
248     private static List<Content> collectAllChildren(List<Content> nodes, Content node, ContentFilter filter) {
249         // get filtered sub nodes first
250         Collection<Content> children = node.getChildren(filter);
251         for (Content child : children) {
252             nodes.add(child);
253         }
254 
255         // get all children to find recursively
256         Collection<Content> allChildren = node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER);
257 
258         // recursion
259         for (Content child : allChildren) {
260             collectAllChildren(nodes, child, filter);
261         }
262 
263         return nodes;
264     }
265 
266     /**
267      * Get all children of a particular type
268      * @param node
269      * @param type
270      * @return
271      */
272     public static List<Content> collectAllChildren(Content node, ItemType type) {
273         List<Content> nodes = new ArrayList<Content>();
274         return collectAllChildren(nodes, node, new ItemType[]{type});
275     }
276 
277     /**
278      * Returns all children (not recursively) independent of there type
279      */
280     public static Collection<Content> getAllChildren(Content node){
281         return node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER);
282     }
283 
284     /**
285      * Returns all children (not recursively) independent of there type
286      */
287     public static Collection<Content> getAllChildren(Content node, Comparator<Content> comp){
288         return node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER, comp);
289     }
290 
291     /**
292      * Get all children of a particular type
293      * @param node
294      * @param types
295      * @return
296      */
297     public static List<Content> collectAllChildren(Content node, ItemType[] types) {
298         List<Content> nodes = new ArrayList<Content>();
299         return collectAllChildren(nodes, node, types);
300     }
301 
302     /**
303      * Get all subnodes recursively and add them to the nodes collection.
304      * @param nodes
305      * @param node
306      * @param types
307      * @return the list
308      */
309     private static List<Content> collectAllChildren(List<Content> nodes, Content node, ItemType[] types) {
310         for (int i = 0; i < types.length; i++) {
311             ItemType type = types[i];
312 
313             Collection<Content> children = node.getChildren(type);
314             for (Content child : children) {
315                 nodes.add(child);
316                 collectAllChildren(nodes, child, types);
317             }
318         }
319         return nodes;
320     }
321 
322     /**
323      * Convenient method to order a node before a target node.
324      */
325     public static void orderBefore(Content nodeToMove, String targetNodeName) throws RepositoryException{
326         nodeToMove.getParent().orderBefore(nodeToMove.getName(), targetNodeName);
327     }
328 
329     /**
330      * Convenient method for ordering a node after a specific target node. This is not that simple as jcr only supports ordering before a node.
331      */
332     public static void orderAfter(Content nodeToMove, String targetNodeName) throws RepositoryException{
333         Content parent = nodeToMove.getParent();
334 
335         Collection<Content> children = new ArrayList<Content>(ContentUtil.getAllChildren(parent));
336         // move all nodes before nodeToMove until the target node has been moved to
337         // this will keep the original order
338         for (Content child : children) {
339             // don't reorder the node we are moving. we are moving the other nodes in front of it
340             if(child.getName().equals(nodeToMove.getName())){
341                 continue;
342             }
343             // order the current child in front of the target node -->
344             parent.orderBefore(child.getName(), nodeToMove.getName());
345             if(child.getName().equals(targetNodeName)){
346                 break;
347             }
348         }
349     }
350 
351     public static void orderNodes(Content node, String[] nodes) throws RepositoryException{
352         for (int i = nodes.length - 1; i > 0; i--) {
353             node.orderBefore(nodes[i-1], nodes[i]);
354         }
355     }
356 
357     /**
358      * Uses the passed comparator to create the jcr ordering of the children
359      * @throws RepositoryException
360      */
361     public static void orderNodes(Content node, Comparator<Content> comparator) throws RepositoryException {
362         Collection<Content> children = ContentUtil.getAllChildren(node, comparator);
363         String[] names = new String[children.size()];
364 
365         int i = 0;
366         for (Content childNode : children) {
367             names[i] = childNode.getName();
368             i++;
369         }
370         orderNodes(node, names);
371     }
372 
373     public static void visit(Content node, Visitor visitor) throws Exception{
374         visit(node, visitor, EXCLUDE_META_DATA_CONTENT_FILTER);
375     }
376 
377     public static void visit(Content node, Visitor visitor, ContentFilter filter) throws Exception{
378         visitor.visit(node);
379         for (Content content : node.getChildren(filter)) {
380             visit(content, visitor, filter);
381         }
382         if(visitor instanceof PostVisitor){
383             ((PostVisitor)visitor).postVisit(node);
384         }
385     }
386 
387     public static Content createPath(HierarchyManager hm, String path) throws AccessDeniedException,
388         PathNotFoundException, RepositoryException {
389         return createPath(hm, path, false);
390     }
391 
392     public static Content createPath(HierarchyManager hm, String path, boolean save) throws AccessDeniedException,
393         PathNotFoundException, RepositoryException {
394         return createPath(hm, path, ItemType.CONTENT, save);
395     }
396 
397     public static Content createPath(HierarchyManager hm, String path, ItemType type) throws AccessDeniedException,
398         PathNotFoundException, RepositoryException {
399         return createPath(hm, path, type, false);
400     }
401 
402     public static Content createPath(HierarchyManager hm, String path, ItemType type, boolean save) throws AccessDeniedException,
403         PathNotFoundException, RepositoryException {
404         Content root = hm.getRoot();
405         return createPath(root, path, type, save);
406     }
407 
408     public static Content createPath(Content parent, String path, ItemType type) throws RepositoryException,
409         PathNotFoundException, AccessDeniedException {
410         return createPath(parent, path, type, false);
411     }
412 
413     public static Content createPath(Content parent, String path, ItemType type, boolean save) throws RepositoryException,
414         PathNotFoundException, AccessDeniedException {
415         // remove leading /
416         path = StringUtils.removeStart(path, "/");
417 
418         if (StringUtils.isEmpty(path)) {
419             return parent;
420         }
421 
422         String[] names = path.split("/"); //$NON-NLS-1$
423 
424         for (int i = 0; i < names.length; i++) {
425             String name = names[i];
426             if (parent.hasContent(name)) {
427                 parent = parent.getContent(name);
428             }
429             else {
430                 final Content newNode = parent.createContent(name, type);
431                 if(save){
432                     parent.save();
433                 }
434                 parent = newNode;
435             }
436         }
437         return parent;
438     }
439 
440     public static String uuid2path(String repository, String uuid){
441         if(StringUtils.isNotEmpty(uuid)){
442             HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
443             try {
444                 Content node = hm.getContentByUUID(uuid);
445                 return node.getHandle();
446             }
447             catch (Exception e) {
448                 // return the uuid
449             }
450 
451         }
452         return uuid;
453     }
454 
455     public static String path2uuid(String repository, String path) {
456         if(StringUtils.isNotEmpty(path)){
457             HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
458             try {
459                 Content node = hm.getContent(path);
460                 return node.getUUID();
461             }
462             catch (Exception e) {
463                 // return the uuid
464             }
465 
466         }
467         return path;
468     }
469 
470     public static void deleteAndRemoveEmptyParents(Content node) throws PathNotFoundException, RepositoryException,
471     AccessDeniedException {
472         deleteAndRemoveEmptyParents(node, 0);
473     }
474 
475     public static void deleteAndRemoveEmptyParents(Content node, int level) throws PathNotFoundException, RepositoryException,
476         AccessDeniedException {
477         Content parent = null;
478         if(node.getLevel() != 0){
479              parent = node.getParent();
480         }
481         node.delete();
482         if(parent != null && parent.getLevel()>level && parent.getChildren(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER).size()==0){
483             deleteAndRemoveEmptyParents(parent, level);
484         }
485     }
486 
487     /**
488      * Session based copy operation. As JCR only supports workspace based copies this operation is performed
489      * by using export import operations.
490      */
491     public static void copyInSession(Content src, String dest) throws RepositoryException {
492         final String destParentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(dest, "/"), "/");
493         final String destNodeName = StringUtils.substringAfterLast(dest, "/");
494         final Session session = src.getWorkspace().getSession();
495         try{
496             final File file = File.createTempFile("mgnl", null, Path.getTempDirectory());
497             final FileOutputStream outStream = new FileOutputStream(file);
498             session.exportSystemView(src.getHandle(), outStream, false, false);
499             outStream.flush();
500             IOUtils.closeQuietly(outStream);
501             FileInputStream inStream = new FileInputStream(file);
502             session.importXML(
503                 destParentPath,
504                 inStream,
505                 ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
506             IOUtils.closeQuietly(inStream);
507             file.delete();
508             if(!StringUtils.equals(src.getName(), destNodeName)){
509                 String currentPath;
510                 if(destParentPath.equals("/")){
511                     currentPath = "/" + src.getName();
512                 }
513                 else{
514                     currentPath = destParentPath + "/" + src.getName();
515                 }
516                 session.move(currentPath, dest);
517             }
518         }
519         catch (IOException e) {
520             throw new RepositoryException("Can't copy node " + src + " to " + dest, e);
521         }
522     }
523 
524     /**
525      * Magnolia uses by default workspace move operation to move nodes. This is a util method to move a node inside a session.
526      */
527     public static void moveInSession(Content src, String dest) throws RepositoryException {
528         src.getWorkspace().getSession().move(src.getHandle(), dest);
529     }
530 
531     public static void rename(Content node, String newName) throws RepositoryException{
532         Content parent = node.getParent();
533         String placedBefore = null;
534         for (Iterator<Content> iter = parent.getChildren(node.getNodeTypeName()).iterator(); iter.hasNext();) {
535             Content child = iter.next();
536             if (child.getUUID().equals(node.getUUID())) {
537                 if (iter.hasNext()) {
538                     child = iter.next();
539                     placedBefore = child.getName();
540                 }
541             }
542         }
543 
544         moveInSession(node, PathUtil.createPath(node.getParent().getHandle(), newName));
545 
546         // now set at the same place as before
547         if (placedBefore != null) {
548             parent.orderBefore(newName, placedBefore);
549             parent.save();
550         }
551     }
552 
553     /**
554      * Utility method to change the <code>jcr:primaryType</code> value of a node.
555      * @param node - {@link Content} the node whose type has to be changed
556      * @param newType - {@link ItemType} the new node type to be assigned
557      * @param replaceAll - boolean when <code>true</code> replaces all occurrences
558      * of the old node type. When <code>false</code> replaces only the first occurrence.
559      * @throws RepositoryException
560      */
561     public static void changeNodeType(Content node, ItemType newType, boolean replaceAll) throws RepositoryException{
562         if(node == null){
563             throw new IllegalArgumentException("Content can't be null");
564         }
565         if(newType == null){
566             throw new IllegalArgumentException("ItemType can't be null");
567         }
568         final String oldTypeName = node.getNodeTypeName();
569         final String newTypeName = newType.getSystemName();
570         if(newTypeName.equals(oldTypeName)){
571             log.info("Old node type and new one are the same {}. Nothing to change.", newTypeName);
572             return;
573         }
574         final Pattern nodeTypePattern = Pattern.compile("(<sv:property\\s+sv:name=\"jcr:primaryType\"\\s+sv:type=\"Name\"><sv:value>)("+oldTypeName+")(</sv:value>)");
575         final String replacement = "$1"+newTypeName+"$3";
576 
577         log.debug("pattern is {}", nodeTypePattern.pattern());
578         log.debug("replacement string is {}", replacement);
579         log.debug("replaceAll? {}", replaceAll);
580 
581         final String destParentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(node.getHandle(), "/"), "/");
582         final Session session = node.getWorkspace().getSession();
583         FileOutputStream outStream = null;
584         FileInputStream inStream = null;
585         File file = null;
586 
587         try {
588             file = File.createTempFile("mgnl", null, Path.getTempDirectory());
589             outStream = new FileOutputStream(file);
590             session.exportSystemView(node.getHandle(), outStream, false, false);
591             outStream.flush();
592             final String fileContents = FileUtils.readFileToString(file);
593             log.debug("content string is {}", fileContents);
594             final Matcher matcher = nodeTypePattern.matcher(fileContents);
595             String replaced = null;
596 
597             log.debug("starting find&replace...");
598             long start = System.currentTimeMillis();
599             if(matcher.find()) {
600                 log.debug("{} will be replaced", node.getHandle());
601                 if(replaceAll){
602                     replaced = matcher.replaceAll(replacement);
603                 } else {
604                     replaced = matcher.replaceFirst(replacement);
605                 }
606                 log.debug("replaced string is {}", replaced);
607             } else {
608                 log.debug("{} won't be replaced", node.getHandle());
609                 return;
610             }
611             log.debug("find&replace operations took {}ms" + (System.currentTimeMillis() - start) / 1000);
612 
613             FileUtils.writeStringToFile(file, replaced);
614             inStream = new FileInputStream(file);
615             session.importXML(
616                 destParentPath,
617                 inStream,
618                 ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
619 
620         } catch (IOException e) {
621             throw new RepositoryException("Can't replace node " + node.getHandle(), e);
622         } finally {
623             IOUtils.closeQuietly(outStream);
624             IOUtils.closeQuietly(inStream);
625             FileUtils.deleteQuietly(file);
626         }
627     }
628 }