1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 public class PageManagerUtils
45 {
46 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 Principal principal = SecurityHelper.getBestPrincipal(subject, UserPrincipal.class);
59 if (principal == null)
60 {
61 String errorMessage = "Could not create user home for null principal";
62 log.error(errorMessage);
63 throw new NodeException(errorMessage);
64 }
65 try
66 {
67 String userName = principal.getName();
68
69 Folder newUserFolder;
70 if (pageManager.userFolderExists(userName))
71 {
72 newUserFolder = pageManager.getUserFolder(userName);
73 }
74 else
75 {
76 newUserFolder = pageManager.newFolder(Folder.USER_FOLDER + userName);
77 SecurityConstraints constraints = pageManager.newSecurityConstraints();
78 newUserFolder.setSecurityConstraints(constraints);
79 newUserFolder.getSecurityConstraints().setOwner(userName);
80 pageManager.updateFolder(newUserFolder);
81 }
82
83
84
85 Iterator roles = SecurityHelper.getPrincipals(subject, RolePrincipal.class).iterator();
86 while (roles.hasNext())
87 {
88 RolePrincipal role = (RolePrincipal)roles.next();
89 if (pageManager.folderExists(Folder.ROLE_FOLDER + role.getName()))
90 {
91 Folder roleFolder = pageManager.getFolder(Folder.ROLE_FOLDER + role.getName());
92 deepMergeFolder(pageManager, roleFolder, Folder.USER_FOLDER + newUserFolder.getName(), userName, role.getName());
93 }
94 }
95 }
96 catch (Exception e)
97 {
98 String errorMessage = "createUserHomePagesFromRoles failed: " + e.getMessage();
99 log.error(errorMessage, e);
100 throw new NodeException(errorMessage, e);
101 }
102 }
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 Iterator pages = srcFolder.getPages().iterator();
122 while (pages.hasNext())
123 {
124 Page srcPage = (Page)pages.next();
125 String path = concatenatePaths(destinationPath, srcPage.getName());
126 if (!pageManager.pageExists(path))
127 {
128 Page dstPage = pageManager.copyPage(srcPage, path);
129 pageManager.updatePage(dstPage);
130 }
131
132
133
134
135
136
137
138
139
140 }
141
142 Iterator links = srcFolder.getLinks().iterator();
143 while (links.hasNext())
144 {
145 Link srcLink = (Link)links.next();
146 String path = concatenatePaths(destinationPath, srcLink.getName());
147 if (!pageManager.linkExists(path))
148 {
149 Link dstLink = pageManager.copyLink(srcLink, path);
150 pageManager.updateLink(dstLink);
151 }
152
153
154
155
156
157
158
159
160
161 }
162 Iterator folders = srcFolder.getFolders().iterator();
163 while (folders.hasNext())
164 {
165 Folder folder = (Folder)folders.next();
166 String newPath = concatenatePaths(destinationPath, folder.getName());
167 if (!pageManager.folderExists(newPath))
168 {
169 Folder dstFolder = pageManager.copyFolder(folder, newPath);
170 pageManager.updateFolder(dstFolder);
171 }
172 deepMergeFolder(pageManager, folder, newPath, null, uniqueName);
173 }
174 }
175
176 public static String concatenatePaths(String base, String path)
177 {
178 String result = "";
179 if (base == null)
180 {
181 if (path == null)
182 {
183 return result;
184 }
185 return path;
186 }
187 else
188 {
189 if (path == null)
190 {
191 return base;
192 }
193 }
194 if (base.endsWith(Folder.PATH_SEPARATOR))
195 {
196 if (path.startsWith(Folder.PATH_SEPARATOR))
197 {
198 result = base.concat(path.substring(1));
199 return result;
200 }
201
202 }
203 else
204 {
205 if (!path.startsWith(Folder.PATH_SEPARATOR))
206 {
207 result = base.concat(Folder.PATH_SEPARATOR).concat(path);
208 return result;
209 }
210 }
211 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 boolean found = true;
224 try
225 {
226 pageManager.getFolder(destinationPath);
227 }
228 catch (FolderNotFoundException e)
229 {
230 found = false;
231 }
232 if (found)
233 {
234 throw new NodeException("Destination already exists");
235 }
236 Folder dstFolder = pageManager.copyFolder(srcFolder, destinationPath);
237 if (owner != null)
238 {
239 SecurityConstraints constraints = dstFolder.getSecurityConstraints();
240 if (constraints == null)
241 {
242 constraints = pageManager.newSecurityConstraints();
243 dstFolder.setSecurityConstraints(constraints);
244 }
245 dstFolder.getSecurityConstraints().setOwner(owner);
246 }
247 pageManager.updateFolder(dstFolder);
248
249 Iterator pages = srcFolder.getPages().iterator();
250 while (pages.hasNext())
251 {
252 Page srcPage = (Page)pages.next();
253 String path = PageManagerUtils.concatenatePaths(destinationPath, srcPage.getName());
254 Page dstPage = pageManager.copyPage(srcPage, path);
255 pageManager.updatePage(dstPage);
256 }
257
258 Iterator links = srcFolder.getLinks().iterator();
259 while (links.hasNext())
260 {
261 Link srcLink = (Link)links.next();
262 String path = PageManagerUtils.concatenatePaths(destinationPath, srcLink.getName());
263 Link dstLink = pageManager.copyLink(srcLink, path);
264 pageManager.updateLink(dstLink);
265 }
266
267 Iterator folders = srcFolder.getFolders().iterator();
268 while (folders.hasNext())
269 {
270 Folder folder = (Folder)folders.next();
271 String newPath = concatenatePaths(destinationPath, folder.getName());
272 deepCopyFolder(pageManager, folder, newPath, null);
273 }
274 }
275
276 }