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 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
0 |
public class PageManagerUtils |
45 |
|
{ |
46 |
0 |
protected static Log log = LogFactory.getLog(PageManagerUtils.class); |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
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 |
|
|
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 |
|
|
83 |
|
|
84 |
|
|
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 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
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 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
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 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
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 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
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 |
|
} |