View Javadoc

1   /**
2    * This file Copyright (c) 2013 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
155      *            the ext to set
156      */
157     public void setExt(String ext) {
158         this.ext = ext;
159     }
160 
161     /**
162      * @return the format
163      */
164     public boolean isFormat() {
165         return format;
166     }
167 
168     /**
169      * @param format
170      *            the format to set
171      */
172     public void setFormat(boolean format) {
173         this.format = format;
174     }
175 
176     /**
177      * @return the keepHistory
178      */
179     public boolean isKeepHistory() {
180         return keepHistory;
181     }
182 
183     /**
184      * @param keepHistory
185      *            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
200      *            the fileName to set
201      */
202     public void setFileName(String fileName) {
203         this.fileName = fileName;
204     }
205 
206     /**
207      * @return the outputStream
208      */
209     public OutputStream getOutputStream() {
210         return outputStream;
211     }
212 
213     /**
214      * @param outputStream
215      *            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
230      *            the mimeExtension to set
231      */
232     public void setMimeExtension(String mimeExtension) {
233         this.mimeExtension = mimeExtension;
234     }
235 
236 }