KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > pages > ExportPage


1 package info.magnolia.module.admininterface.pages;
2
3 import info.magnolia.cms.beans.config.ContentRepository;
4 import info.magnolia.cms.core.HierarchyManager;
5 import info.magnolia.cms.core.ie.DataTransporter;
6 import info.magnolia.cms.i18n.Messages;
7 import info.magnolia.cms.i18n.MessagesManager;
8 import info.magnolia.cms.security.AccessDeniedException;
9 import info.magnolia.cms.security.AccessManager;
10 import info.magnolia.cms.security.Permission;
11 import info.magnolia.context.MgnlContext;
12 import info.magnolia.module.admininterface.TemplatedMVCHandler;
13
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import javax.jcr.Session;
19 import javax.jcr.Workspace;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * Simple servlet used to import/export data from jcr using the standard jcr import/export features.
31  * @author Fabrizio Giustina
32  * @version $Id: ExportPage.java 6341 2006-09-12 09:18:27Z philipp $
33  */

34 public class ExportPage extends TemplatedMVCHandler {
35
36     /**
37      * Stable serialVersionUID.
38      */

39     public static final long serialVersionUID = 222L;
40
41     public static final String JavaDoc MIME_TEXT_XML = "text/xml";
42
43     public static final String JavaDoc MIME_GZIP = "application/x-gzip";
44
45     public static final String JavaDoc MIME_APPLICATION_ZIP = "application/zip";
46
47     /**
48      * View value for the export file stream (won't render anything)
49      */

50     public static final String JavaDoc VIEW_EXPORT="export";
51     
52     /**
53      * Logger.
54      */

55     private static Logger log = LoggerFactory.getLogger(ExportPage.class);
56
57     protected String JavaDoc mgnlRepository;
58
59     protected String JavaDoc mgnlPath;
60
61     protected boolean mgnlKeepVersions;
62
63     private boolean mgnlFormat;
64
65     private String JavaDoc ext;
66
67     private boolean exportxml;
68
69     /**
70      * Getter for <code>ext</code>.
71      * @return Returns the ext.
72      */

73     public String JavaDoc getExt() {
74         return this.ext;
75     }
76
77     /**
78      * Setter for <code>ext</code>.
79      * @param ext The ext to set.
80      */

81     public void setExt(String JavaDoc ext) {
82         this.ext = ext;
83     }
84
85     /**
86      * Getter for <code>mgnlFormat</code>.
87      * @return Returns the mgnlFormat.
88      */

89     public boolean isMgnlFormat() {
90         return this.mgnlFormat;
91     }
92
93     /**
94      * Setter for <code>mgnlFormat</code>.
95      * @param mgnlFormat The mgnlFormat to set.
96      */

97     public void setMgnlFormat(boolean mgnlFormat) {
98         this.mgnlFormat = mgnlFormat;
99     }
100
101     /**
102      * Getter for <code>mgnlKeepVersions</code>.
103      * @return Returns the mgnlKeepVersions.
104      */

105     public boolean isMgnlKeepVersions() {
106         return this.mgnlKeepVersions;
107     }
108
109     /**
110      * Setter for <code>mgnlKeepVersions</code>.
111      * @param mgnlKeepVersions The mgnlKeepVersions to set.
112      */

113     public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
114         this.mgnlKeepVersions = mgnlKeepVersions;
115     }
116
117     /**
118      * Getter for <code>mgnlPath</code>.
119      * @return Returns the mgnlPath.
120      */

121     public String JavaDoc getMgnlPath() {
122         return this.mgnlPath;
123     }
124
125     /**
126      * Setter for <code>mgnlPath</code>.
127      * @param mgnlPath The mgnlPath to set.
128      */

129     public void setMgnlPath(String JavaDoc mgnlPath) {
130         this.mgnlPath = mgnlPath;
131     }
132
133     /**
134      * Getter for <code>mgnlRepository</code>.
135      * @return Returns the mgnlRepository.
136      */

137     public String JavaDoc getMgnlRepository() {
138         return this.mgnlRepository;
139     }
140
141     /**
142      * Setter for <code>mgnlRepository</code>.
143      * @param mgnlRepository The mgnlRepository to set.
144      */

145     public void setMgnlRepository(String JavaDoc mgnlRepository) {
146         this.mgnlRepository = mgnlRepository;
147     }
148
149     /**
150      * Getter for <code>exportxml</code>.
151      * @return Returns the exportxml.
152      */

153     public boolean isExportxml() {
154         return this.exportxml;
155     }
156
157     /**
158      * Setter for <code>exportxml</code>.
159      * @param exportxml The exportxml to set.
160      */

161     public void setExportxml(boolean exportxml) {
162         this.exportxml = exportxml;
163     }
164
165     /**
166      * @param name
167      * @param request
168      * @param response
169      */

170     public ExportPage(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
171         super(name, request, response);
172     }
173
174     /**
175      * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
176      */

177     public String JavaDoc getCommand() {
178         if (this.exportxml) {
179             return "exportxml";
180         }
181         return super.getCommand();
182     }
183
184     /**
185      * Actually perform export. The generated file is sent to the client.
186      * @param response HttpServletResponse
187      * @param repository selected repository
188      * @param basepath base path in repository
189      * @param format should we format the resulting xml
190      * @param keepVersionHistory if <code>false</code> version info will be stripped from the exported document
191      * @throws IOException for errors while accessing the servlet output stream
192      */

193     public String JavaDoc exportxml() throws Exception JavaDoc {
194
195         if (StringUtils.isEmpty(mgnlRepository)) {
196             mgnlRepository = ContentRepository.WEBSITE;
197         }
198         if (StringUtils.isEmpty(mgnlPath)) {
199             mgnlPath = "/"; //$NON-NLS-1$
200
}
201         if (StringUtils.isEmpty(ext)) {
202             ext = DataTransporter.XML;
203         }
204
205         if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
206
207             throw new ServletException JavaDoc(new AccessDeniedException(
208                 "Write permission needed for export. User not allowed to WRITE path [" //$NON-NLS-1$
209
+ mgnlPath
210                     + "]")); //$NON-NLS-1$
211

212         }
213         HierarchyManager hr = MgnlContext.getHierarchyManager(mgnlRepository);
214         Workspace ws = hr.getWorkspace();
215         Session session = ws.getSession();
216
217         if (ext.equalsIgnoreCase(DataTransporter.ZIP)) {
218             response.setContentType(MIME_APPLICATION_ZIP);
219         }
220         else if (ext.equalsIgnoreCase(DataTransporter.GZ)) {
221             response.setContentType(MIME_GZIP);
222         }
223         else {
224             response.setContentType(MIME_TEXT_XML);
225             response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
226
}
227
228         String JavaDoc pathName = StringUtils.replace(mgnlPath, "/", "."); //$NON-NLS-1$ //$NON-NLS-2$
229
if (".".equals(pathName)) { //$NON-NLS-1$
230
// root node
231
pathName = StringUtils.EMPTY;
232         }
233         response.setHeader("content-disposition", "attachment; filename=" + mgnlRepository + pathName + ext); //$NON-NLS-1$ //$NON-NLS-2$
234
OutputStream JavaDoc baseOutputStream = response.getOutputStream();
235
236         try {
237             DataTransporter.executeExport(
238                 baseOutputStream,
239                 mgnlKeepVersions,
240                 mgnlFormat,
241                 session,
242                 mgnlPath,
243                 mgnlRepository,
244                 ext);
245         }
246         catch (RuntimeException JavaDoc e) {
247             response.setContentType("text/html; charset=UTF-8");
248             response.setHeader("content-disposition", "inline"); //$NON-NLS-1$ //$NON-NLS-2$
249
throw e;
250         }
251
252         return VIEW_EXPORT;
253     }
254
255     /**
256      * Uses access manager to authorise this request.
257      * @param request HttpServletRequest as received by the service method
258      * @return boolean true if read access is granted
259      */

260     protected boolean checkPermissions(HttpServletRequest JavaDoc request, String JavaDoc repository, String JavaDoc basePath,
261         long permissionType) {
262
263         AccessManager accessManager = MgnlContext.getAccessManager(repository);
264         if (accessManager != null) {
265             if (!accessManager.isGranted(basePath, permissionType)) {
266                 return false;
267             }
268         }
269         return true;
270     }
271     
272     public void renderHtml(String JavaDoc view) throws IOException JavaDoc {
273         // if we are exporing the file, everything is already done --> do not render
274
if(view == VIEW_EXPORT){
275             return;
276         }
277         super.renderHtml(view);
278     }
279
280     public Messages getMessages() {
281         return MessagesManager.getMessages();
282     }
283
284     public Iterator JavaDoc getRepositories() {
285         return ContentRepository.getAllRepositoryNames();
286     }
287
288 }
Popular Tags