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