View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.importexport;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.Path;
38  import info.magnolia.cms.util.StringLengthComparator;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.NodeUtil;
42  
43  import java.io.File;
44  import java.io.FileOutputStream;
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.util.ArrayList;
48  import java.util.Arrays;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  import javax.jcr.Session;
56  
57  import org.apache.commons.io.IOUtils;
58  import org.apache.commons.lang3.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Utilities to bootstrap set of files and/or to export content into a specified directory.
64   */
65  public class BootstrapUtil {
66      private static final Logger log = LoggerFactory.getLogger(BootstrapUtil.class);
67  
68      public static void bootstrap(String[] resourceNames, int importUUIDBehavior) throws IOException, RepositoryException {
69          // sort by length --> import parent node first
70          List<String> list = new ArrayList<String>(Arrays.asList(resourceNames));
71  
72          Collections.sort(list, new StringLengthComparator());
73  
74          for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
75              String resourceName = iter.next();
76  
77              String name = getFilenameFromResource(resourceName, ".xml");
78              String workspace = getWorkspaceNameFromResource(resourceName);
79              String pathName = getPathnameFromResource(resourceName);
80              String fullPath = getFullpathFromResource(resourceName);
81  
82              log.debug("Will bootstrap {}", resourceName);
83  
84              final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
85              if (stream == null) {
86                  throw new IOException("Can't find resource to bootstrap at " + resourceName);
87              }
88  
89              // if the node already exists we will keep the order
90              String nameOfNodeAfterTheImportedNode = null;
91  
92              final Session session = MgnlContext.getJCRSession(workspace);
93              // if the path already exists --> delete it
94              try {
95  
96                  // session can be null if module is not properly registered and the repository has not been created
97                  if (session != null && StringUtils.isNotEmpty(fullPath) && session.nodeExists(fullPath)) {
98                      // but keep the order
99                      Node node = session.getNode(fullPath);
100                     if (!NodeUtil.isLastSibling(node)) {
101                         nameOfNodeAfterTheImportedNode = NodeUtil.getSiblingAfter(node).getName();
102                     }
103                     if (node.getDepth() == 1) { // deletion on the first level doesn't sometimes refresh cache and leads into ItemExistsException on import, see JCR-3279/MAGNOLIA-4544
104                         Node parent = node.getParent();
105                         Node tmp = NodeUtil.createPath(parent, Path.getUniqueLabel(parent, "tmp"), NodeTypes.Content.NAME);
106                         NodeUtil.moveNode(node, tmp);
107                         tmp.remove();
108                     } else {
109                         node.remove();
110                     }
111                     log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
112                 }
113             } catch (RepositoryException e) {
114                 IOUtils.closeQuietly(stream);
115                 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
116             }
117             DataTransporter.importXmlStream(stream, workspace, pathName, name, false, importUUIDBehavior, false, true);
118 
119             if (nameOfNodeAfterTheImportedNode != null) {
120                 Node newNode = session.getNode(fullPath);
121                 NodeUtil.orderBefore(newNode, nameOfNodeAfterTheImportedNode);
122             }
123 
124         }
125     }
126 
127     /**
128      * @deprecated since 5.3.3, use {@link #export(Node, File)} instead.
129      */
130     public static void export(Content content, File directory) throws IOException, RepositoryException {
131         BootstrapUtil.export(content.getJCRNode(), directory);
132     }
133 
134     public static void export(Node content, File directory) throws IOException, RepositoryException {
135 
136         Session session = content.getSession();
137         String workspace = session.getWorkspace().getName();
138         String path = content.getPath();
139         String fileName = workspace + DataTransporter.createExportPath(path) + ".xml";
140         File file = new File(directory, fileName);
141         FileOutputStream out = new FileOutputStream(file);
142         try {
143             DataTransporter.executeExport(out, false, true, session, path, workspace, DataTransporter.XML);
144         } finally {
145             IOUtils.closeQuietly(out);
146         }
147     }
148 
149     /**
150      * I.e. given a resource path like <code>/mgnl-bootstrap/foo/config.server.i18n.xml</code> it will return <code>config</code>.
151      */
152     public static String getWorkspaceNameFromResource(final String resourcePath) {
153         String resourceName = StringUtils.replace(resourcePath, "\\", "/");
154 
155         String name = getFilenameFromResource(resourceName, ".xml");
156         String fullPath = DataTransporter.revertExportPath(name);
157         return StringUtils.substringBefore(fullPath, "/");
158     }
159 
160     /**
161      * I.e. given a resource path like <code>/mgnl-bootstrap/foo/config.server.i18n.xml</code> it will return <code>/server/i18n</code>.
162      */
163     public static String getFullpathFromResource(final String resourcePath) {
164         String resourceName = StringUtils.replace(resourcePath, "\\", "/");
165 
166         String name = getFilenameFromResource(resourceName, ".xml");
167         String fullPath = DataTransporter.revertExportPath(name);
168         String repository = StringUtils.substringBefore(fullPath, "/");
169 
170         return StringUtils.removeStart(fullPath, repository);
171     }
172 
173     /**
174      * I.e. given a resource path like <code>/mgnl-bootstrap/foo/config.server.i18n.xml</code> it will return <code>/server</code>.
175      */
176     public static String getPathnameFromResource(final String resourcePath) {
177         String resourceName = StringUtils.replace(resourcePath, "\\", "/");
178 
179         String name = getFilenameFromResource(resourceName, ".xml");
180         String fullPath = DataTransporter.revertExportPath(name);
181         String pathName = StringUtils.substringAfter(StringUtils.substringBeforeLast(fullPath, "/"), "/");
182 
183         if (!pathName.startsWith("/")) {
184             pathName = "/" + pathName;
185         }
186         return pathName;
187     }
188 
189     /**
190      * I.e. given a resource path like <code>/mgnl-bootstrap/foo/config.server.i18n.xml</code> and <code>.xml</code> extension it will return <code>config.server.i18n</code> (no trailing dot).
191      * If extension is <code>null</code>, it defaults to <code>.xml</code>.
192      */
193     public static String getFilenameFromResource(final String resourcePath, final String extension) {
194         String ext = StringUtils.defaultIfEmpty(extension, ".xml");
195         String tmpResourcePath = resourcePath;
196         if (resourcePath.contains("/")) {
197             tmpResourcePath = StringUtils.substringAfterLast(resourcePath, "/");
198         }
199         return StringUtils.removeEnd(tmpResourcePath, ext.startsWith(".") ? ext : "." + ext);
200     }
201 
202 }