1 13 package info.magnolia.module.admininterface.pages; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.beans.config.ModuleLoader; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.core.ItemType; 20 import info.magnolia.cms.core.Path; 21 import info.magnolia.cms.core.Content.ContentFilter; 22 import info.magnolia.cms.core.ie.DataTransporter; 23 import info.magnolia.cms.security.AccessDeniedException; 24 import info.magnolia.cms.util.AlertUtil; 25 import info.magnolia.cms.util.ContentUtil; 26 import info.magnolia.context.MgnlContext; 27 import info.magnolia.module.admininterface.TemplatedMVCHandler; 28 29 import java.io.File ; 30 import java.io.FileNotFoundException ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.util.Iterator ; 34 35 import javax.jcr.PathNotFoundException; 36 import javax.jcr.RepositoryException; 37 import javax.jcr.Session; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 41 import org.apache.commons.io.IOUtils; 42 import org.apache.commons.lang.StringUtils; 43 import org.slf4j.Logger; 44 import org.slf4j.LoggerFactory; 45 46 47 52 public class DevelopmentUtilsPage extends TemplatedMVCHandler { 53 54 private boolean templates; 55 56 private boolean paragraphs; 57 58 private boolean dialogs; 59 60 private boolean website; 61 62 private boolean users; 63 64 private boolean groups; 65 66 private boolean roles; 67 68 private boolean secure; 69 70 private String rootdir; 71 72 private String parentpath; 73 74 private String repository; 75 76 private static ContentFilter MAGNOLIA_FILTER = new ContentFilter() { 77 78 public boolean accept(Content content) { 79 80 try { 81 String nodetype = content.getNodeType().getName(); 82 return nodetype.startsWith("mgnl:"); 84 } 85 catch (RepositoryException e) { 86 log.error("Unable to read nodetype for node {}", content.getHandle()); 87 } 88 return false; 89 } 90 }; 91 92 95 protected static Logger log = LoggerFactory.getLogger(DevelopmentUtilsPage.class); 96 97 102 public DevelopmentUtilsPage(String name, HttpServletRequest request, HttpServletResponse response) { 103 super(name, request, response); 104 } 105 106 110 public void setDialogs(boolean dialogs) { 111 this.dialogs = dialogs; 112 } 113 114 118 public void setParagraphs(boolean paragraphs) { 119 this.paragraphs = paragraphs; 120 } 121 122 126 public void setTemplates(boolean templates) { 127 this.templates = templates; 128 } 129 130 134 public void setRootdir(String rootdir) { 135 this.rootdir = rootdir; 136 } 137 138 142 public void setWebsite(boolean website) { 143 this.website = website; 144 } 145 146 150 public void setParentpath(String parentpath) { 151 this.parentpath = parentpath; 152 } 153 154 158 public void setGroups(boolean groups) { 159 this.groups = groups; 160 } 161 162 166 public void setRoles(boolean roles) { 167 this.roles = roles; 168 } 169 170 174 public void setUsers(boolean users) { 175 this.users = users; 176 } 177 178 182 public void setRepository(String repository) { 183 this.repository = repository; 184 } 185 186 public Iterator getRepositories() { 187 return ContentRepository.getAllRepositoryNames(); 188 } 189 190 194 public void setSecure(boolean secure) { 195 this.secure = secure; 196 } 197 198 public String backup() { 199 200 Iterator modules = ModuleLoader.getInstance().getModuleInstances().keySet().iterator(); 201 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.CONFIG); 202 Session session = hm.getWorkspace().getSession(); 203 204 while (modules.hasNext()) { 205 String moduleName = (String ) modules.next(); 206 207 if (!"templating".equals(moduleName)) { 208 continue; 210 } 211 try { 212 Content moduleroot = hm.getContent("/modules/" + moduleName); 213 if (templates) { 214 exportChildren(ContentRepository.CONFIG, session, moduleroot, "templates", new ItemType[]{ 215 ItemType.CONTENT, 216 ItemType.CONTENTNODE}, false); 217 } 218 if (paragraphs) { 219 exportChildren(ContentRepository.CONFIG, session, moduleroot, "paragraphs", new ItemType[]{ 220 ItemType.CONTENT, 221 ItemType.CONTENTNODE}, false); 222 } 223 if (dialogs) { 224 exportChildren( 225 ContentRepository.CONFIG, 226 session, 227 moduleroot, 228 "dialogs", 229 new ItemType[]{ItemType.CONTENT}, 230 true); 231 } 232 AlertUtil.setMessage("Backup done!"); 233 } 234 catch (Exception e) { 235 log.error(e.getMessage(), e); 236 AlertUtil.setMessage("Error while processing module " + moduleName, e); 237 } 238 239 } 240 241 if (secure) { 242 backupChildren(ContentRepository.CONFIG, "/server/secureURIList"); 243 backupChildren(ContentRepository.CONFIG, "/server/unsecureURIList"); 244 } 245 246 if (website) { 247 extractWorkspaceRoots(ContentRepository.WEBSITE); 248 } 249 250 if (users) { 251 extractWorkspaceRoots(ContentRepository.USERS); 252 } 253 254 if (groups) { 255 extractWorkspaceRoots(ContentRepository.USER_GROUPS); 256 } 257 258 if (roles) { 259 extractWorkspaceRoots(ContentRepository.USER_ROLES); 260 } 261 262 return this.show(); 263 } 264 265 268 private void extractWorkspaceRoots(String repositoryName) { 269 try { 270 HierarchyManager hm = MgnlContext.getHierarchyManager(repositoryName); 271 Content wesiteRoot = hm.getRoot(); 272 273 Iterator children = wesiteRoot.getChildren(MAGNOLIA_FILTER).iterator(); 274 while (children.hasNext()) { 275 Content exported = (Content) children.next(); 276 exportNode(repositoryName, hm.getWorkspace().getSession(), exported); 277 } 278 } 279 catch (Exception e) { 280 log.error(e.getMessage(), e); 281 AlertUtil.setMessage("Error while processing " + repositoryName + " repository", e); 282 } 283 } 284 285 public String backupChildren() { 286 backupChildren(this.repository, this.parentpath); 287 AlertUtil.setMessage("Backup done!"); 288 289 return this.show(); 290 } 291 292 private void backupChildren(String repository, String parentpath) { 293 HierarchyManager hm = MgnlContext.getHierarchyManager(repository); 294 try { 295 Content parentNode = hm.getContent(parentpath); 296 Iterator children = parentNode.getChildren(MAGNOLIA_FILTER).iterator(); 297 while (children.hasNext()) { 298 Content exported = (Content) children.next(); 299 exportNode(repository, hm.getWorkspace().getSession(), exported); 300 } 301 302 } 303 catch (Exception e) { 304 log.error(e.getMessage(), e); 305 AlertUtil.setMessage("Error while processing actions", e); 306 } 307 308 } 309 310 320 private void exportChildren(String repository, Session session, Content moduleroot, String path, 321 ItemType[] itemTypes, boolean exportContentContainingContentNodes) throws PathNotFoundException, 322 RepositoryException, AccessDeniedException, FileNotFoundException , IOException { 323 Content templateRoot = moduleroot.getContent(path); 324 325 Iterator children = ContentUtil.collectAllChildren(templateRoot, itemTypes).iterator(); 326 while (children.hasNext()) { 327 Content exported = (Content) children.next(); 328 if (!exported.getNodeDataCollection().isEmpty() || (exportContentContainingContentNodes && exported.hasChildren(ItemType.CONTENTNODE.getSystemName()))) { 330 exportNode(repository, session, exported); 331 } 332 } 333 } 334 335 341 private void exportNode(String repository, Session session, Content exported) throws FileNotFoundException , 342 IOException { 343 String handle = exported.getHandle(); 344 345 String xmlName = repository + StringUtils.replace(handle, "/", ".") + ".xml"; 346 347 File folder = new File (Path.getAbsoluteFileSystemPath(rootdir), repository); 349 folder.mkdirs(); 350 File xmlFile = new File (folder.getAbsoluteFile(), xmlName); 351 FileOutputStream fos = new FileOutputStream (xmlFile); 352 353 try { 354 DataTransporter.executeExport(fos, false, true, session, handle, repository, DataTransporter.XML); 355 } 356 finally { 357 IOUtils.closeQuietly(fos); 358 } 359 } 360 } 361 | Popular Tags |