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