View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.module.admininterface.pages;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.i18n.Messages;
39  import info.magnolia.cms.i18n.MessagesManager;
40  import info.magnolia.cms.security.AccessDeniedException;
41  import info.magnolia.cms.security.AccessManager;
42  import info.magnolia.cms.security.Permission;
43  import info.magnolia.cms.util.AlertUtil;
44  import info.magnolia.context.MgnlContext;
45  import info.magnolia.importexport.DataTransporter;
46  import info.magnolia.module.admininterface.TemplatedMVCHandler;
47  import info.magnolia.repository.RepositoryConstants;
48  
49  import java.io.IOException;
50  import java.io.OutputStream;
51  import java.util.Iterator;
52  
53  import javax.jcr.Session;
54  import javax.jcr.Workspace;
55  import javax.servlet.ServletException;
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.commons.lang.StringUtils;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  
64  /**
65   * Simple servlet used to import/export data from jcr using the standard jcr import/export features.
66   * @author Fabrizio Giustina
67   * @version $Id$
68   */
69  public class ExportPage extends TemplatedMVCHandler {
70  
71      /**
72       * Stable serialVersionUID.
73       */
74      public static final long serialVersionUID = 222L;
75  
76      public static final String MIME_TEXT_XML = "text/xml";
77  
78      public static final String MIME_GZIP = "application/x-gzip";
79  
80      public static final String MIME_APPLICATION_ZIP = "application/zip";
81  
82      /**
83       * View value for the export file stream (won't render anything)
84       */
85      public static final String VIEW_EXPORT="export";
86  
87      /**
88       * Logger.
89       */
90      private static Logger log = LoggerFactory.getLogger(ExportPage.class);
91  
92      protected String mgnlRepository;
93  
94      protected String mgnlPath;
95  
96      protected boolean mgnlKeepVersions;
97  
98      private boolean mgnlFormat;
99  
100     private String ext;
101 
102     private boolean exportxml;
103 
104     /**
105      * Getter for <code>ext</code>.
106      * @return Returns the ext.
107      */
108     public String getExt() {
109         return this.ext;
110     }
111 
112     /**
113      * Setter for <code>ext</code>.
114      * @param ext The ext to set.
115      */
116     public void setExt(String ext) {
117         this.ext = ext;
118     }
119 
120     /**
121      * Getter for <code>mgnlFormat</code>.
122      * @return Returns the mgnlFormat.
123      */
124     public boolean isMgnlFormat() {
125         return this.mgnlFormat;
126     }
127 
128     /**
129      * Setter for <code>mgnlFormat</code>.
130      * @param mgnlFormat The mgnlFormat to set.
131      */
132     public void setMgnlFormat(boolean mgnlFormat) {
133         this.mgnlFormat = mgnlFormat;
134     }
135 
136     /**
137      * Getter for <code>mgnlKeepVersions</code>.
138      * @return Returns the mgnlKeepVersions.
139      */
140     public boolean isMgnlKeepVersions() {
141         return this.mgnlKeepVersions;
142     }
143 
144     /**
145      * Setter for <code>mgnlKeepVersions</code>.
146      * @param mgnlKeepVersions The mgnlKeepVersions to set.
147      */
148     public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
149         this.mgnlKeepVersions = mgnlKeepVersions;
150     }
151 
152     /**
153      * Getter for <code>mgnlPath</code>.
154      * @return Returns the mgnlPath.
155      */
156     public String getMgnlPath() {
157         return this.mgnlPath;
158     }
159 
160     /**
161      * Setter for <code>mgnlPath</code>.
162      * @param mgnlPath The mgnlPath to set.
163      */
164     public void setMgnlPath(String mgnlPath) {
165         this.mgnlPath = mgnlPath;
166     }
167 
168     /**
169      * Getter for <code>mgnlRepository</code>.
170      * @return Returns the mgnlRepository.
171      */
172     public String getMgnlRepository() {
173         return this.mgnlRepository;
174     }
175 
176     /**
177      * Setter for <code>mgnlRepository</code>.
178      * @param mgnlRepository The mgnlRepository to set.
179      */
180     public void setMgnlRepository(String mgnlRepository) {
181         this.mgnlRepository = mgnlRepository;
182     }
183 
184     /**
185      * Getter for <code>exportxml</code>.
186      * @return Returns the exportxml.
187      */
188     public boolean isExportxml() {
189         return this.exportxml;
190     }
191 
192     /**
193      * Setter for <code>exportxml</code>.
194      * @param exportxml The exportxml to set.
195      */
196     public void setExportxml(boolean exportxml) {
197         this.exportxml = exportxml;
198     }
199 
200     /**
201      * @param name
202      * @param request
203      * @param response
204      */
205     public ExportPage(String name, HttpServletRequest request, HttpServletResponse response) {
206         super(name, request, response);
207     }
208 
209     /**
210      * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
211      */
212     @Override
213     public String getCommand() {
214         if (this.exportxml) {
215             return "exportxml";
216         }
217         return super.getCommand();
218     }
219 
220     /**
221      * Actually perform export. The generated file is sent to the client.
222      * @param response HttpServletResponse
223      * @param repository selected repository
224      * @param basepath base path in repository
225      * @param format should we format the resulting xml
226      * @param keepVersionHistory if <code>false</code> version info will be stripped from the exported document
227      * @throws IOException for errors while accessing the servlet output stream
228      */
229     public String exportxml() throws Exception {
230 
231         if (StringUtils.isEmpty(mgnlRepository)) {
232             mgnlRepository = RepositoryConstants.WEBSITE;
233         }
234         if (StringUtils.isEmpty(mgnlPath)) {
235             mgnlPath = "/";
236         }
237         if (StringUtils.isEmpty(ext)) {
238             ext = DataTransporter.XML;
239         }
240 
241         if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
242 
243             AlertUtil.setMessage("Write permission needed for export. User not allowed to WRITE path [" + mgnlPath + "]");
244 
245             throw new ServletException(new AccessDeniedException(
246                 "Write permission needed for export. User not allowed to WRITE path ["
247                     + mgnlPath
248                     + "]"));
249 
250         }
251         HierarchyManager hr = MgnlContext.getHierarchyManager(mgnlRepository);
252         Workspace ws = hr.getWorkspace();
253         Session session = ws.getSession();
254 
255         if (ext.equalsIgnoreCase(DataTransporter.ZIP)) {
256             response.setContentType(MIME_APPLICATION_ZIP);
257         }
258         else if (ext.equalsIgnoreCase(DataTransporter.GZ)) {
259             response.setContentType(MIME_GZIP);
260         }
261         else {
262             response.setContentType(MIME_TEXT_XML);
263             response.setCharacterEncoding("UTF-8");
264         }
265 
266         String pathName = DataTransporter.createExportPath(mgnlPath);
267         pathName = DataTransporter.encodePath(pathName, DataTransporter.DOT, DataTransporter.UTF8);
268         if (DataTransporter.DOT.equals(pathName)) {
269             // root node
270             pathName = StringUtils.EMPTY;
271         }
272 
273         response.setHeader("content-disposition", "attachment; filename=" + mgnlRepository + pathName + ext);
274         OutputStream baseOutputStream = response.getOutputStream();
275 
276         try {
277             DataTransporter.executeExport(
278                 baseOutputStream,
279                 mgnlKeepVersions,
280                 mgnlFormat,
281                 session,
282                 mgnlPath,
283                 mgnlRepository,
284                 ext);
285         }
286         catch (RuntimeException e) {
287             response.setContentType("text/html; charset=UTF-8");
288             response.setHeader("content-disposition", "inline");
289             throw e;
290         }
291 
292         return VIEW_EXPORT;
293     }
294 
295     /**
296      * Uses access manager to authorise this request.
297      * @param request HttpServletRequest as received by the service method
298      * @return boolean true if read access is granted
299      */
300     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
301         long permissionType) {
302 
303         AccessManager accessManager = MgnlContext.getAccessManager(repository);
304         if (accessManager != null) {
305             if (!accessManager.isGranted(basePath, permissionType)) {
306                 return false;
307             }
308         }
309         return true;
310     }
311 
312     @Override
313     public void renderHtml(String view) throws IOException {
314         // if we are exporing the file, everything is already done --> do not render
315         if(VIEW_EXPORT.equals(view)){
316             return;
317         }
318         super.renderHtml(view);
319     }
320 
321     public Messages getMessages() {
322         return MessagesManager.getMessages();
323     }
324 
325     public Iterator getRepositories() {
326         return ContentRepository.getAllRepositoryNames();
327     }
328 
329 }