View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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   */
70  public class ContentUtil {
71      private final static Logger log = LoggerFactory.getLogger(ContentUtil.class);
72  
73      /**
74       * Content filter accepting everything.
75       */
76      public static ContentFilter ALL_NODES_CONTENT_FILTER = new ContentFilter() {
77  
78          public boolean accept(Content content) {
79              return true;
80          }
81      };
82  
83      /**
84       * Content filter accepting everything exept nodes with namespace jcr (version and system store).
85       */
86      public static ContentFilter ALL_NODES_EXCEPT_JCR_CONTENT_FILTER = new ContentFilter() {
87  
88          public boolean accept(Content content) {
89              return !content.getName().startsWith("jcr:");
90          }
91      };
92  
93      /**
94       * Content filter accepting everything except meta data and jcr types.
95       */
96      public static ContentFilter EXCLUDE_META_DATA_CONTENT_FILTER = new ContentFilter() {
97          public boolean accept(Content content) {
98              return !content.getName().startsWith("jcr:") && !content.isNodeType(ItemType.NT_METADATA);
99          }
100     };
101 
102     /**
103      * Content filter accepting all nodes with a nodetype of namespace mgnl.
104      */
105     public static ContentFilter MAGNOLIA_FILTER = new ContentFilter() {
106         public boolean accept(Content content) {
107 
108             try {
109                 String nodetype = content.getNodeType().getName();
110                 // export only "magnolia" nodes
111                 return nodetype.startsWith("mgnl:");
112             }
113             catch (RepositoryException e) {
114                 log.error("Unable to read nodetype for node {}", content.getHandle());
115             }
116             return false;
117         }
118     };
119 
120     /**
121      * Used in {@link #visit(Content)} to visit the hierarchy.
122      * @version $Id: ContentUtil.java 41137 2011-01-06 18:19:25Z gjoseph $
123      */
124     // TODO : throws RepositoryException or none, but not Exception !?
125     public static interface Visitor {
126         void visit(Content node) throws Exception;
127     }
128 
129     /**
130      * Used in {@link #visit(Content)} if the visitor wants to use post order.
131      */
132     public static interface PostVisitor extends Visitor {
133         void postVisit(Content node) throws Exception;
134     }
135 
136 
137     /**
138      * Returns a Content object of the named repository or null if not existing.
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      */
245     private static List<Content> collectAllChildren(List<Content> nodes, Content node, ContentFilter filter) {
246         // get filtered sub nodes first
247         Collection<Content> children = node.getChildren(filter);
248         for (Content child : children) {
249             nodes.add(child);
250         }
251 
252         // get all children to find recursively
253         Collection<Content> allChildren = node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER);
254 
255         // recursion
256         for (Content child : allChildren) {
257             collectAllChildren(nodes, child, filter);
258         }
259 
260         return nodes;
261     }
262 
263     /**
264      * Get all children of a particular type.
265      */
266     public static List<Content> collectAllChildren(Content node, ItemType type) {
267         List<Content> nodes = new ArrayList<Content>();
268         return collectAllChildren(nodes, node, new ItemType[]{type});
269     }
270 
271     /**
272      * Returns all children (not recursively) independent of there type.
273      */
274     public static Collection<Content> getAllChildren(Content node){
275         return node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER);
276     }
277 
278     /**
279      * Returns all children (not recursively) independent of there type.
280      */
281     public static Collection<Content> getAllChildren(Content node, Comparator<Content> comp){
282         return node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER, comp);
283     }
284 
285     /**
286      * Get all children of a particular type.
287      */
288     public static List<Content> collectAllChildren(Content node, ItemType[] types) {
289         List<Content> nodes = new ArrayList<Content>();
290         return collectAllChildren(nodes, node, types);
291     }
292 
293     /**
294      * Get all subnodes recursively and add them to the nodes collection.
295      */
296     private static List<Content> collectAllChildren(List<Content> nodes, Content node, ItemType[] types) {
297         for (int i = 0; i < types.length; i++) {
298             ItemType type = types[i];
299 
300             Collection<Content> children = node.getChildren(type);
301             for (Content child : children) {
302                 nodes.add(child);
303                 collectAllChildren(nodes, child, types);
304             }
305         }
306         return nodes;
307     }
308 
309     /**
310      * Convenient method to order a node before a target node.
311      */
312     public static void orderBefore(Content nodeToMove, String targetNodeName) throws RepositoryException{
313         nodeToMove.getParent().orderBefore(nodeToMove.getName(), targetNodeName);
314     }
315 
316     /**
317      * Convenient method for ordering a node after a specific target node. This is not that simple as jcr only supports ordering before a node.
318      */
319     public static void orderAfter(Content nodeToMove, String targetNodeName) throws RepositoryException{
320         Content parent = nodeToMove.getParent();
321         Boolean readyToMove = false;
322 
323         Collection<Content> children = new ArrayList<Content>(ContentUtil.getAllChildren(parent));
324         for (Content child : children) {
325             if(readyToMove){
326                 parent.orderBefore(nodeToMove.getName(), child.getName());
327                 readyToMove = false;
328                 break;
329             }
330 
331             if(child.getName().equals(targetNodeName)){
332                 readyToMove = true;
333             }
334         }
335 
336         if(readyToMove){
337             for (Content child : children){
338                 if(!nodeToMove.getName().equals(child.getName())){
339                     parent.orderBefore(child.getName(), nodeToMove.getName());
340                 }
341             }
342         }
343     }
344 
345     public static void orderNodes(Content node, String[] nodes) throws RepositoryException{
346         for (int i = nodes.length - 1; i > 0; i--) {
347             node.orderBefore(nodes[i-1], nodes[i]);
348         }
349     }
350 
351     /**
352      * Uses the passed comparator to create the jcr ordering of the children.
353      */
354     public static void orderNodes(Content node, Comparator<Content> comparator) throws RepositoryException {
355         Collection<Content> children = ContentUtil.getAllChildren(node, comparator);
356         String[] names = new String[children.size()];
357 
358         int i = 0;
359         for (Content childNode : children) {
360             names[i] = childNode.getName();
361             i++;
362         }
363         orderNodes(node, names);
364     }
365 
366     public static void visit(Content node, Visitor visitor) throws Exception{
367         visit(node, visitor, EXCLUDE_META_DATA_CONTENT_FILTER);
368     }
369 
370     public static void visit(Content node, Visitor visitor, ContentFilter filter) throws Exception{
371         visitor.visit(node);
372         for (Content content : node.getChildren(filter)) {
373             visit(content, visitor, filter);
374         }
375         if(visitor instanceof PostVisitor){
376             ((PostVisitor)visitor).postVisit(node);
377         }
378     }
379 
380     public static Content createPath(HierarchyManager hm, String path) throws AccessDeniedException,
381         PathNotFoundException, RepositoryException {
382         return createPath(hm, path, false);
383     }
384 
385     public static Content createPath(HierarchyManager hm, String path, boolean save) throws AccessDeniedException,
386         PathNotFoundException, RepositoryException {
387         return createPath(hm, path, ItemType.CONTENT, save);
388     }
389 
390     public static Content createPath(HierarchyManager hm, String path, ItemType type) throws AccessDeniedException,
391         PathNotFoundException, RepositoryException {
392         return createPath(hm, path, type, false);
393     }
394 
395     public static Content createPath(HierarchyManager hm, String path, ItemType type, boolean save) throws AccessDeniedException,
396         PathNotFoundException, RepositoryException {
397         Content root = hm.getRoot();
398         return createPath(root, path, type, save);
399     }
400 
401     public static Content createPath(Content parent, String path, ItemType type) throws RepositoryException,
402         PathNotFoundException, AccessDeniedException {
403         return createPath(parent, path, type, false);
404     }
405 
406     public static Content createPath(Content parent, String path, ItemType type, boolean save) throws RepositoryException,
407         PathNotFoundException, AccessDeniedException {
408         // remove leading /
409         path = StringUtils.removeStart(path, "/");
410 
411         if (StringUtils.isEmpty(path)) {
412             return parent;
413         }
414 
415         String[] names = path.split("/"); //$NON-NLS-1$
416 
417         for (int i = 0; i < names.length; i++) {
418             String name = names[i];
419             if (parent.hasContent(name)) {
420                 parent = parent.getContent(name);
421             }
422             else {
423                 final Content newNode = parent.createContent(name, type);
424                 if(save){
425                     parent.save();
426                 }
427                 parent = newNode;
428             }
429         }
430         return parent;
431     }
432 
433     public static String uuid2path(String repository, String uuid){
434         if(StringUtils.isNotEmpty(uuid)){
435             HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
436             try {
437                 Content node = hm.getContentByUUID(uuid);
438                 return node.getHandle();
439             }
440             catch (Exception e) {
441                 // return the uuid
442             }
443 
444         }
445         return uuid;
446     }
447 
448     public static String path2uuid(String repository, String path) {
449         if(StringUtils.isNotEmpty(path)){
450             HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
451             try {
452                 Content node = hm.getContent(path);
453                 return node.getUUID();
454             }
455             catch (Exception e) {
456                 // return the uuid
457             }
458 
459         }
460         return path;
461     }
462 
463     public static void deleteAndRemoveEmptyParents(Content node) throws PathNotFoundException, RepositoryException,
464     AccessDeniedException {
465         deleteAndRemoveEmptyParents(node, 0);
466     }
467 
468     public static void deleteAndRemoveEmptyParents(Content node, int level) throws PathNotFoundException, RepositoryException,
469         AccessDeniedException {
470         Content parent = null;
471         if(node.getLevel() != 0){
472              parent = node.getParent();
473         }
474         node.delete();
475         if(parent != null && parent.getLevel()>level && parent.getChildren(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER).size()==0){
476             deleteAndRemoveEmptyParents(parent, level);
477         }
478     }
479 
480     /**
481      * Session based copy operation. As JCR only supports workspace based copies this operation is performed
482      * by using export import operations.
483      */
484     public static void copyInSession(Content src, String dest) throws RepositoryException {
485         final String destParentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(dest, "/"), "/");
486         final String destNodeName = StringUtils.substringAfterLast(dest, "/");
487         final Session session = src.getWorkspace().getSession();
488         try{
489             final File file = File.createTempFile("mgnl", null, Path.getTempDirectory());
490             final FileOutputStream outStream = new FileOutputStream(file);
491             session.exportSystemView(src.getHandle(), outStream, false, false);
492             outStream.flush();
493             IOUtils.closeQuietly(outStream);
494             FileInputStream inStream = new FileInputStream(file);
495             session.importXML(
496                 destParentPath,
497                 inStream,
498                 ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
499             IOUtils.closeQuietly(inStream);
500             file.delete();
501             if(!StringUtils.equals(src.getName(), destNodeName)){
502                 String currentPath;
503                 if(destParentPath.equals("/")){
504                     currentPath = "/" + src.getName();
505                 }
506                 else{
507                     currentPath = destParentPath + "/" + src.getName();
508                 }
509                 session.move(currentPath, dest);
510             }
511         }
512         catch (IOException e) {
513             throw new RepositoryException("Can't copy node " + src + " to " + dest, e);
514         }
515     }
516 
517     /**
518      * Magnolia uses by default workspace move operation to move nodes. This is a util method to move a node inside a session.
519      */
520     public static void moveInSession(Content src, String dest) throws RepositoryException {
521         src.getWorkspace().getSession().move(src.getHandle(), dest);
522     }
523 
524     public static void rename(Content node, String newName) throws RepositoryException{
525         Content parent = node.getParent();
526         String placedBefore = null;
527         for (Iterator<Content> iter = parent.getChildren(node.getNodeTypeName()).iterator(); iter.hasNext();) {
528             Content child = iter.next();
529             if (child.getUUID().equals(node.getUUID())) {
530                 if (iter.hasNext()) {
531                     child = iter.next();
532                     placedBefore = child.getName();
533                 }
534             }
535         }
536 
537         moveInSession(node, PathUtil.createPath(node.getParent().getHandle(), newName));
538 
539         // now set at the same place as before
540         if (placedBefore != null) {
541             parent.orderBefore(newName, placedBefore);
542             parent.save();
543         }
544     }
545 
546     /**
547      * Utility method to change the <code>jcr:primaryType</code> value of a node.
548      * @param node - {@link Content} the node whose type has to be changed
549      * @param newType - {@link ItemType} the new node type to be assigned
550      * @param replaceAll - boolean when <code>true</code> replaces all occurrences
551      * of the old node type. When <code>false</code> replaces only the first occurrence.
552      * @throws RepositoryException
553      */
554     public static void changeNodeType(Content node, ItemType newType, boolean replaceAll) throws RepositoryException{
555         if(node == null){
556             throw new IllegalArgumentException("Content can't be null");
557         }
558         if(newType == null){
559             throw new IllegalArgumentException("ItemType can't be null");
560         }
561         final String oldTypeName = node.getNodeTypeName();
562         final String newTypeName = newType.getSystemName();
563         if(newTypeName.equals(oldTypeName)){
564             log.info("Old node type and new one are the same {}. Nothing to change.", newTypeName);
565             return;
566         }
567         final Pattern nodeTypePattern = Pattern.compile("(<sv:property\\s+sv:name=\"jcr:primaryType\"\\s+sv:type=\"Name\"><sv:value>)("+oldTypeName+")(</sv:value>)");
568         final String replacement = "$1"+newTypeName+"$3";
569 
570         log.debug("pattern is {}", nodeTypePattern.pattern());
571         log.debug("replacement string is {}", replacement);
572         log.debug("replaceAll? {}", replaceAll);
573 
574         final String destParentPath = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(node.getHandle(), "/"), "/");
575         final Session session = node.getWorkspace().getSession();
576         FileOutputStream outStream = null;
577         FileInputStream inStream = null;
578         File file = null;
579 
580         try {
581             file = File.createTempFile("mgnl", null, Path.getTempDirectory());
582             outStream = new FileOutputStream(file);
583             session.exportSystemView(node.getHandle(), outStream, false, false);
584             outStream.flush();
585             final String fileContents = FileUtils.readFileToString(file);
586             log.debug("content string is {}", fileContents);
587             final Matcher matcher = nodeTypePattern.matcher(fileContents);
588             String replaced = null;
589 
590             log.debug("starting find&replace...");
591             long start = System.currentTimeMillis();
592             if(matcher.find()) {
593                 log.debug("{} will be replaced", node.getHandle());
594                 if(replaceAll){
595                     replaced = matcher.replaceAll(replacement);
596                 } else {
597                     replaced = matcher.replaceFirst(replacement);
598                 }
599                 log.debug("replaced string is {}", replaced);
600             } else {
601                 log.debug("{} won't be replaced", node.getHandle());
602                 return;
603             }
604             log.debug("find&replace operations took {}ms" + (System.currentTimeMillis() - start) / 1000);
605 
606             FileUtils.writeStringToFile(file, replaced);
607             inStream = new FileInputStream(file);
608             session.importXML(
609                 destParentPath,
610                 inStream,
611                 ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
612 
613         } catch (IOException e) {
614             throw new RepositoryException("Can't replace node " + node.getHandle(), e);
615         } finally {
616             IOUtils.closeQuietly(outStream);
617             IOUtils.closeQuietly(inStream);
618             FileUtils.deleteQuietly(file);
619         }
620     }
621 }