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 ; 15 import java.io.OutputStream ; 16 import java.util.Iterator ; 17 18 import javax.jcr.Session; 19 import javax.jcr.Workspace; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 24 import org.apache.commons.lang.StringUtils; 25 import org.slf4j.Logger; 26 import org.slf4j.LoggerFactory; 27 28 29 34 public class ExportPage extends TemplatedMVCHandler { 35 36 39 public static final long serialVersionUID = 222L; 40 41 public static final String MIME_TEXT_XML = "text/xml"; 42 43 public static final String MIME_GZIP = "application/x-gzip"; 44 45 public static final String MIME_APPLICATION_ZIP = "application/zip"; 46 47 50 public static final String VIEW_EXPORT="export"; 51 52 55 private static Logger log = LoggerFactory.getLogger(ExportPage.class); 56 57 protected String mgnlRepository; 58 59 protected String mgnlPath; 60 61 protected boolean mgnlKeepVersions; 62 63 private boolean mgnlFormat; 64 65 private String ext; 66 67 private boolean exportxml; 68 69 73 public String getExt() { 74 return this.ext; 75 } 76 77 81 public void setExt(String ext) { 82 this.ext = ext; 83 } 84 85 89 public boolean isMgnlFormat() { 90 return this.mgnlFormat; 91 } 92 93 97 public void setMgnlFormat(boolean mgnlFormat) { 98 this.mgnlFormat = mgnlFormat; 99 } 100 101 105 public boolean isMgnlKeepVersions() { 106 return this.mgnlKeepVersions; 107 } 108 109 113 public void setMgnlKeepVersions(boolean mgnlKeepVersions) { 114 this.mgnlKeepVersions = mgnlKeepVersions; 115 } 116 117 121 public String getMgnlPath() { 122 return this.mgnlPath; 123 } 124 125 129 public void setMgnlPath(String mgnlPath) { 130 this.mgnlPath = mgnlPath; 131 } 132 133 137 public String getMgnlRepository() { 138 return this.mgnlRepository; 139 } 140 141 145 public void setMgnlRepository(String mgnlRepository) { 146 this.mgnlRepository = mgnlRepository; 147 } 148 149 153 public boolean isExportxml() { 154 return this.exportxml; 155 } 156 157 161 public void setExportxml(boolean exportxml) { 162 this.exportxml = exportxml; 163 } 164 165 170 public ExportPage(String name, HttpServletRequest request, HttpServletResponse response) { 171 super(name, request, response); 172 } 173 174 177 public String getCommand() { 178 if (this.exportxml) { 179 return "exportxml"; 180 } 181 return super.getCommand(); 182 } 183 184 193 public String exportxml() throws Exception { 194 195 if (StringUtils.isEmpty(mgnlRepository)) { 196 mgnlRepository = ContentRepository.WEBSITE; 197 } 198 if (StringUtils.isEmpty(mgnlPath)) { 199 mgnlPath = "/"; } 201 if (StringUtils.isEmpty(ext)) { 202 ext = DataTransporter.XML; 203 } 204 205 if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) { 206 207 throw new ServletException (new AccessDeniedException( 208 "Write permission needed for export. User not allowed to WRITE path [" + mgnlPath 210 + "]")); 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"); } 227 228 String pathName = StringUtils.replace(mgnlPath, "/", "."); if (".".equals(pathName)) { pathName = StringUtils.EMPTY; 232 } 233 response.setHeader("content-disposition", "attachment; filename=" + mgnlRepository + pathName + ext); OutputStream 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 e) { 247 response.setContentType("text/html; charset=UTF-8"); 248 response.setHeader("content-disposition", "inline"); throw e; 250 } 251 252 return VIEW_EXPORT; 253 } 254 255 260 protected boolean checkPermissions(HttpServletRequest request, String repository, String 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 view) throws IOException { 273 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 getRepositories() { 285 return ContentRepository.getAllRepositoryNames(); 286 } 287 288 } | Popular Tags |