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