1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
66
67
68
69 public class ExportPage extends TemplatedMVCHandler {
70
71
72
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
84
85 public static final String VIEW_EXPORT="export";
86
87
88
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
106
107
108 public String getExt() {
109 return this.ext;
110 }
111
112
113
114
115
116 public void setExt(String ext) {
117 this.ext = ext;
118 }
119
120
121
122
123
124 public boolean isMgnlFormat() {
125 return this.mgnlFormat;
126 }
127
128
129
130
131
132 public void setMgnlFormat(boolean mgnlFormat) {
133 this.mgnlFormat = mgnlFormat;
134 }
135
136
137
138
139
140 public boolean isMgnlKeepVersions() {
141 return this.mgnlKeepVersions;
142 }
143
144
145
146
147
148 public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
149 this.mgnlKeepVersions = mgnlKeepVersions;
150 }
151
152
153
154
155
156 public String getMgnlPath() {
157 return this.mgnlPath;
158 }
159
160
161
162
163
164 public void setMgnlPath(String mgnlPath) {
165 this.mgnlPath = mgnlPath;
166 }
167
168
169
170
171
172 public String getMgnlRepository() {
173 return this.mgnlRepository;
174 }
175
176
177
178
179
180 public void setMgnlRepository(String mgnlRepository) {
181 this.mgnlRepository = mgnlRepository;
182 }
183
184
185
186
187
188 public boolean isExportxml() {
189 return this.exportxml;
190 }
191
192
193
194
195
196 public void setExportxml(boolean exportxml) {
197 this.exportxml = exportxml;
198 }
199
200
201
202
203
204
205 public ExportPage(String name, HttpServletRequest request, HttpServletResponse response) {
206 super(name, request, response);
207 }
208
209
210
211
212 @Override
213 public String getCommand() {
214 if (this.exportxml) {
215 return "exportxml";
216 }
217 return super.getCommand();
218 }
219
220
221
222
223
224
225
226
227
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
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
297
298
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
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 }