Coverage report

  %line %branch
org.apache.jetspeed.page.PageManagerUtils
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.page;
 18  
 
 19  
 import java.security.Principal;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import javax.security.auth.Subject;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.apache.jetspeed.om.common.SecurityConstraints;
 27  
 import org.apache.jetspeed.om.folder.Folder;
 28  
 import org.apache.jetspeed.om.folder.FolderNotFoundException;
 29  
 import org.apache.jetspeed.om.page.Link;
 30  
 import org.apache.jetspeed.om.page.Page;
 31  
 import org.apache.jetspeed.page.document.NodeException;
 32  
 import org.apache.jetspeed.security.RolePrincipal;
 33  
 import org.apache.jetspeed.security.SecurityHelper;
 34  
 import org.apache.jetspeed.security.UserPrincipal;
 35  
 
 36  
 
 37  
 /**
 38  
  * PageManagerUtils
 39  
  * 
 40  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 41  
  * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
 42  
  * @version $Id: $
 43  
  */
 44  0
 public class PageManagerUtils
 45  
 {
 46  0
     protected static Log log = LogFactory.getLog(PageManagerUtils.class);    
 47  
     
 48  
     /**
 49  
      * Creates a user's home page from the roles of the current user.
 50  
      * The use case: when a portal is setup to use shared pages, but then
 51  
      * the user attempts to customize. At this point, we create the new page(s) for the user.
 52  
      * 
 53  
      * @param subject
 54  
      */
 55  
     public static void createUserHomePagesFromRoles(PageManager pageManager, Subject subject)
 56  
     throws NodeException
 57  
     {
 58  0
         Principal principal = SecurityHelper.getBestPrincipal(subject, UserPrincipal.class); 
 59  0
         if (principal == null)
 60  
         {
 61  0
             String errorMessage = "Could not create user home for null principal";
 62  0
             log.error(errorMessage);
 63  0
             throw new NodeException(errorMessage);
 64  
         }
 65  
         try
 66  
         {
 67  0
             String userName = principal.getName();            
 68  
             // get user home
 69  
             Folder newUserFolder;
 70  0
             if (pageManager.userFolderExists(userName))
 71  
             {
 72  0
                 newUserFolder = pageManager.getUserFolder(userName);
 73  
             }
 74  
             else
 75  
             {
 76  0
                 newUserFolder = pageManager.newFolder(Folder.USER_FOLDER + userName);
 77  0
                 SecurityConstraints constraints = pageManager.newSecurityConstraints();
 78  0
                 newUserFolder.setSecurityConstraints(constraints);
 79  0
                 newUserFolder.getSecurityConstraints().setOwner(userName);
 80  0
                 pageManager.updateFolder(newUserFolder);                
 81  
             }            
 82  
             // for each role for a user, deep copy the folder contents for that role 
 83  
             // into the user's home
 84  
             // TODO: this algorithm could actually merge pages on dups
 85  0
             Iterator roles = SecurityHelper.getPrincipals(subject, RolePrincipal.class).iterator();
 86  0
             while (roles.hasNext())
 87  
             {                            
 88  0
                 RolePrincipal role = (RolePrincipal)roles.next();
 89  0
                 if (pageManager.folderExists(Folder.ROLE_FOLDER + role.getName()))
 90  
                 {
 91  0
                     Folder roleFolder = pageManager.getFolder(Folder.ROLE_FOLDER + role.getName());                    
 92  0
                     deepMergeFolder(pageManager, roleFolder, Folder.USER_FOLDER + newUserFolder.getName(), userName, role.getName());
 93  
                 }
 94  0
             }
 95  
         }
 96  0
         catch (Exception e)
 97  
         {
 98  0
             String errorMessage = "createUserHomePagesFromRoles failed: " + e.getMessage();
 99  0
             log.error(errorMessage, e);
 100  0
             throw new NodeException(errorMessage, e);
 101  0
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Deep merges from a source folder into a destination path for the given owner.
 106  
      * The unique name is used in conflict resolution for name collisions.
 107  
      * Example: deep merge a given role folder 'X' into /_user/david
 108  
      *          uniqueName = 'X'
 109  
      *          owner = 'david'
 110  
      *          destinationPath = '_user/david'
 111  
      *          
 112  
      * @param srcFolder
 113  
      * @param destinationPath
 114  
      * @param owner
 115  
      * @param uniqueName
 116  
      * @throws NodeException
 117  
      */
 118  
     public static void deepMergeFolder(PageManager pageManager, Folder srcFolder, String destinationPath, String owner, String uniqueName)
 119  
     throws NodeException
 120  
     {        
 121  0
         Iterator pages = srcFolder.getPages().iterator();
 122  0
         while (pages.hasNext())
 123  
         {
 124  0
             Page srcPage = (Page)pages.next();
 125  0
             String path = concatenatePaths(destinationPath, srcPage.getName());
 126  0
             if (!pageManager.pageExists(path))
 127  
             {
 128  0
                 Page dstPage = pageManager.copyPage(srcPage, path);
 129  0
                 pageManager.updatePage(dstPage);
 130  
             }
 131  
             //Commented, as these were creating the duplicate objects 
 132  
             /*            
 133  
             else
 134  
             {
 135  
                 path = concatenatePaths(destinationPath, uniqueName + "-" +srcPage.getName());               
 136  
                 Page dstPage = pageManager.copyPage(srcPage, path);                
 137  
                 pageManager.updatePage(dstPage);                
 138  
             }
 139  
             */
 140  0
         }
 141  
      
 142  0
         Iterator links = srcFolder.getLinks().iterator();
 143  0
         while (links.hasNext())
 144  
         {
 145  0
             Link srcLink = (Link)links.next();
 146  0
             String path = concatenatePaths(destinationPath, srcLink.getName());
 147  0
             if (!pageManager.linkExists(path))
 148  
             {
 149  0
                 Link dstLink = pageManager.copyLink(srcLink, path);
 150  0
                 pageManager.updateLink(dstLink);
 151  
             }
 152  
             //Commented, as these were creating the duplicate objects            
 153  
             /*
 154  
             else
 155  
             {
 156  
                 path = concatenatePaths(destinationPath, uniqueName + "-" +srcLink.getName());               
 157  
                 Link dstLink = pageManager.copyLink(srcLink, path);                
 158  
                 pageManager.updateLink(dstLink);                                
 159  
             }
 160  
             */
 161  0
         }     
 162  0
         Iterator folders = srcFolder.getFolders().iterator();
 163  0
         while (folders.hasNext())
 164  
         {
 165  0
             Folder folder = (Folder)folders.next();
 166  0
             String newPath = concatenatePaths(destinationPath, folder.getName());
 167  0
             if (!pageManager.folderExists(newPath))
 168  
             {
 169  0
                 Folder dstFolder = pageManager.copyFolder(folder, newPath);
 170  0
                 pageManager.updateFolder(dstFolder);
 171  
             }
 172  0
             deepMergeFolder(pageManager, folder, newPath, null, uniqueName);
 173  0
         }                
 174  0
     }
 175  
 
 176  
     public static String concatenatePaths(String base, String path)
 177  
     {
 178  0
         String result = "";
 179  0
         if (base == null)
 180  
         {
 181  0
             if (path == null)
 182  
             {
 183  0
                 return result;
 184  
             }
 185  0
             return path;
 186  
         }
 187  
         else
 188  
         {
 189  0
             if (path == null)
 190  
             {
 191  0
                 return base;
 192  
             }
 193  
         }
 194  0
         if (base.endsWith(Folder.PATH_SEPARATOR)) 
 195  
         {
 196  0
             if (path.startsWith(Folder.PATH_SEPARATOR))
 197  
             {
 198  0
                 result = base.concat(path.substring(1));
 199  0
                 return result;
 200  
             }
 201  
         
 202  
         }
 203  
         else
 204  
         {
 205  0
             if (!path.startsWith(Folder.PATH_SEPARATOR)) 
 206  
             {
 207  0
                 result = base.concat(Folder.PATH_SEPARATOR).concat(path);
 208  0
                 return result;
 209  
             }
 210  
         }
 211  0
         return base.concat(path);
 212  
     }
 213  
     
 214  
     /**
 215  
      * Deep copy a folder
 216  
      *  
 217  
      * @param source source folder
 218  
      * @param dest destination folder
 219  
      */
 220  
     public static void deepCopyFolder(PageManager pageManager, Folder srcFolder, String destinationPath, String owner)
 221  
     throws NodeException
 222  
     {
 223  0
         boolean found = true;
 224  
         try
 225  
         {
 226  0
             pageManager.getFolder(destinationPath);
 227  
         }
 228  0
         catch (FolderNotFoundException e)
 229  
         {
 230  0
             found = false;
 231  0
         }
 232  0
         if (found)
 233  
         {
 234  0
             throw new NodeException("Destination already exists");
 235  
         }
 236  0
         Folder dstFolder = pageManager.copyFolder(srcFolder, destinationPath);
 237  0
         if (owner != null)
 238  
         {
 239  0
             SecurityConstraints constraints = dstFolder.getSecurityConstraints();
 240  0
             if (constraints == null)
 241  
             {
 242  0
                 constraints = pageManager.newSecurityConstraints();
 243  0
                 dstFolder.setSecurityConstraints(constraints);
 244  
             }
 245  0
             dstFolder.getSecurityConstraints().setOwner(owner);
 246  
         }
 247  0
         pageManager.updateFolder(dstFolder);
 248  
         
 249  0
         Iterator pages = srcFolder.getPages().iterator();
 250  0
         while (pages.hasNext())
 251  
         {
 252  0
             Page srcPage = (Page)pages.next();
 253  0
             String path = PageManagerUtils.concatenatePaths(destinationPath, srcPage.getName());
 254  0
             Page dstPage = pageManager.copyPage(srcPage, path);
 255  0
             pageManager.updatePage(dstPage);
 256  0
         }
 257  
      
 258  0
         Iterator links = srcFolder.getLinks().iterator();
 259  0
         while (links.hasNext())
 260  
         {
 261  0
             Link srcLink = (Link)links.next();
 262  0
             String path = PageManagerUtils.concatenatePaths(destinationPath, srcLink.getName());
 263  0
             Link dstLink = pageManager.copyLink(srcLink, path);
 264  0
             pageManager.updateLink(dstLink);
 265  0
         }
 266  
      
 267  0
         Iterator folders = srcFolder.getFolders().iterator();
 268  0
         while (folders.hasNext())
 269  
         {
 270  0
             Folder folder = (Folder)folders.next();
 271  0
             String newPath = concatenatePaths(destinationPath, folder.getName()); 
 272  0
             deepCopyFolder(pageManager, folder, newPath, null);
 273  0
         }        
 274  0
     }
 275  
     
 276  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.