View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.commands.impl;
35  
36  import info.magnolia.cms.security.AccessDeniedException;
37  import info.magnolia.cms.security.AccessManager;
38  import info.magnolia.cms.security.Permission;
39  import info.magnolia.context.Context;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.importexport.DataTransporter;
42  import info.magnolia.repository.RepositoryConstants;
43  
44  import java.io.OutputStream;
45  
46  import org.apache.commons.io.IOUtils;
47  import org.apache.commons.lang3.StringEscapeUtils;
48  import org.apache.commons.lang3.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Generic Export Command.<br>
54   * <br>
55   *
56   * Get as Input parameter: <br>
57   * Workspace and node path to export <br>
58   * <br>
59   * Set as output: <br>
60   * Fill the OutputStream with an XML representation of the Node structure<br>
61   * Define the MimeType and File name.
62   */
63  public class ExportCommand extends BaseRepositoryCommand {
64  
65      private static final Logger log = LoggerFactory.getLogger(ExportCommand.class);
66  
67      public static final String MIME_TEXT_XML = "application/octet-stream";
68      public static final String MIME_GZIP = "application/x-gzip";
69      public static final String MIME_APPLICATION_ZIP = "application/zip";
70  
71      private String ext = ".xml";
72      public static final String EXPORT_EXTENSION = "ext";
73      private boolean format = true;
74      public static final String EXPORT_FORMAT = "format";
75      private boolean keepHistory = true;
76      public static final String EXPORT_KEEP_HISTORY = "keepHistory";
77      private String fileName;
78      public static final String EXPORT_FILE_NAME = "fileName";
79      private String mimeExtension;
80      public static final String EXPORT_MIME_EXTENSION = "mimeExtension";
81      private OutputStream outputStream;
82      public static final String EXPORT_OUTPUT_STREAM = "outputStream";
83  
84      @Override
85      public boolean execute(Context context) throws Exception {
86          setRepository(StringUtils.isBlank(getRepository()) ? RepositoryConstants.WEBSITE : getRepository());
87          setPath(StringUtils.isBlank(getPath()) ? "/" : getPath());
88  
89          log.debug("Will export content from {} repository with uuid {} and path {}", getRepository(), getUuid(), getPath());
90  
91          // Check Permission
92          if (!checkPermissions(getRepository(), getPath(), Permission.READ)) {
93              // escape to prevent XSS attack
94              throw new AccessDeniedException("Read permission needed for export. User not allowed to READ path [" + StringEscapeUtils.escapeHtml4(getPath()) + "]");
95          }
96          // Define the MimeType based on the extension
97          setMimeExtension(getContentType(ext));
98  
99          String pathName = DataTransporter.createExportPath(getPath());
100         pathName = DataTransporter.encodePath(pathName, DataTransporter.DOT, DataTransporter.UTF8);
101         if (DataTransporter.DOT.equals(pathName)) {
102             // root node
103             pathName = StringUtils.EMPTY;
104         }
105 
106         try {
107             // Create the XML representation of the Node structure
108             DataTransporter.executeExport(getOutputStream(), keepHistory, format, MgnlContext.getJCRSession(getRepository()), getPath(), getRepository(), getExt());
109             // Set the file name.
110             setFileName(getRepository() + pathName + getExt());
111         } catch (RuntimeException e) {
112             throw e;
113         } finally {
114             IOUtils.closeQuietly(outputStream);
115         }
116 
117         return true;
118     }
119 
120     /**
121      * Uses access manager to authorize this request.
122      *
123      * @return boolean true if read access is granted
124      */
125     public boolean checkPermissions(String repository, String basePath, long permissionType) {
126 
127         AccessManager accessManager = MgnlContext.getAccessManager(repository);
128         if (accessManager != null) {
129             if (!accessManager.isGranted(basePath, permissionType)) {
130                 return false;
131             }
132         }
133         return true;
134     }
135 
136     /**
137      * Define the Content Type based on the requested extension.
138      */
139     private String getContentType(String ext) {
140         if (ext.equalsIgnoreCase(DataTransporter.ZIP)) {
141             return MIME_APPLICATION_ZIP;
142         } else if (ext.equalsIgnoreCase(DataTransporter.GZ)) {
143             return MIME_GZIP;
144         } else {
145             return MIME_TEXT_XML;
146         }
147     }
148 
149     /**
150      * @return the ext
151      */
152     public String getExt() {
153         return ext;
154     }
155 
156     /**
157      * @param ext the ext to set
158      */
159     public void setExt(String ext) {
160         this.ext = ext;
161     }
162 
163     /**
164      * @return the format
165      */
166     public boolean isFormat() {
167         return format;
168     }
169 
170     /**
171      * @param format the format to set
172      */
173     public void setFormat(boolean format) {
174         this.format = format;
175     }
176 
177     /**
178      * @return the keepHistory
179      */
180     public boolean isKeepHistory() {
181         return keepHistory;
182     }
183 
184     /**
185      * @param keepHistory the keepHistory to set
186      */
187     public void setKeepHistory(boolean keepHistory) {
188         this.keepHistory = keepHistory;
189     }
190 
191     /**
192      * @return the fileName
193      */
194     public String getFileName() {
195         return fileName;
196     }
197 
198     /**
199      * @param fileName the fileName to set
200      */
201     public void setFileName(String fileName) {
202         this.fileName = fileName;
203     }
204 
205     /**
206      * @return the outputStream
207      */
208     public OutputStream getOutputStream() {
209         return outputStream;
210     }
211 
212     /**
213      * @param outputStream the outputStream to set
214      */
215     public void setOutputStream(OutputStream outputStream) {
216         this.outputStream = outputStream;
217     }
218 
219     /**
220      * @return the mimeExtension
221      */
222     public String getMimeExtension() {
223         return mimeExtension;
224     }
225 
226     /**
227      * @param mimeExtension the mimeExtension to set
228      */
229     public void setMimeExtension(String mimeExtension) {
230         this.mimeExtension = mimeExtension;
231     }
232 
233 }