001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.page;
018:
019: import java.security.Principal;
020: import java.util.Iterator;
021:
022: import javax.security.auth.Subject;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.jetspeed.om.common.SecurityConstraints;
027: import org.apache.jetspeed.om.folder.Folder;
028: import org.apache.jetspeed.om.folder.FolderNotFoundException;
029: import org.apache.jetspeed.om.page.Link;
030: import org.apache.jetspeed.om.page.Page;
031: import org.apache.jetspeed.page.document.NodeException;
032: import org.apache.jetspeed.security.RolePrincipal;
033: import org.apache.jetspeed.security.SecurityHelper;
034: import org.apache.jetspeed.security.UserPrincipal;
035:
036: /**
037: * PageManagerUtils
038: *
039: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
040: * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
041: * @version $Id: $
042: */
043: public class PageManagerUtils {
044: protected static Log log = LogFactory
045: .getLog(PageManagerUtils.class);
046:
047: /**
048: * Creates a user's home page from the roles of the current user.
049: * The use case: when a portal is setup to use shared pages, but then
050: * the user attempts to customize. At this point, we create the new page(s) for the user.
051: *
052: * @param subject
053: */
054: public static void createUserHomePagesFromRoles(
055: PageManager pageManager, Subject subject)
056: throws NodeException {
057: Principal principal = SecurityHelper.getBestPrincipal(subject,
058: UserPrincipal.class);
059: if (principal == null) {
060: String errorMessage = "Could not create user home for null principal";
061: log.error(errorMessage);
062: throw new NodeException(errorMessage);
063: }
064: try {
065: String userName = principal.getName();
066: // get user home
067: Folder newUserFolder;
068: if (pageManager.userFolderExists(userName)) {
069: newUserFolder = pageManager.getUserFolder(userName);
070: } else {
071: newUserFolder = pageManager
072: .newFolder(Folder.USER_FOLDER + userName);
073: SecurityConstraints constraints = pageManager
074: .newSecurityConstraints();
075: newUserFolder.setSecurityConstraints(constraints);
076: newUserFolder.getSecurityConstraints().setOwner(
077: userName);
078: pageManager.updateFolder(newUserFolder);
079: }
080: // for each role for a user, deep copy the folder contents for that role
081: // into the user's home
082: // TODO: this algorithm could actually merge pages on dups
083: Iterator roles = SecurityHelper.getPrincipals(subject,
084: RolePrincipal.class).iterator();
085: while (roles.hasNext()) {
086: RolePrincipal role = (RolePrincipal) roles.next();
087: if (pageManager.folderExists(Folder.ROLE_FOLDER
088: + role.getName())) {
089: Folder roleFolder = pageManager
090: .getFolder(Folder.ROLE_FOLDER
091: + role.getName());
092: deepMergeFolder(pageManager, roleFolder,
093: Folder.USER_FOLDER
094: + newUserFolder.getName(),
095: userName, role.getName());
096: }
097: }
098: } catch (Exception e) {
099: String errorMessage = "createUserHomePagesFromRoles failed: "
100: + e.getMessage();
101: log.error(errorMessage, e);
102: throw new NodeException(errorMessage, e);
103: }
104: }
105:
106: /**
107: * Deep merges from a source folder into a destination path for the given owner.
108: * The unique name is used in conflict resolution for name collisions.
109: * Example: deep merge a given role folder 'X' into /_user/david
110: * uniqueName = 'X'
111: * owner = 'david'
112: * destinationPath = '_user/david'
113: *
114: * @param srcFolder
115: * @param destinationPath
116: * @param owner
117: * @param uniqueName
118: * @throws NodeException
119: */
120: public static void deepMergeFolder(PageManager pageManager,
121: Folder srcFolder, String destinationPath, String owner,
122: String uniqueName) throws NodeException {
123: Iterator pages = srcFolder.getPages().iterator();
124: while (pages.hasNext()) {
125: Page srcPage = (Page) pages.next();
126: String path = concatenatePaths(destinationPath, srcPage
127: .getName());
128: if (!pageManager.pageExists(path)) {
129: Page dstPage = pageManager.copyPage(srcPage, path);
130: pageManager.updatePage(dstPage);
131: }
132: //Commented, as these were creating the duplicate objects
133: /*
134: else
135: {
136: path = concatenatePaths(destinationPath, uniqueName + "-" +srcPage.getName());
137: Page dstPage = pageManager.copyPage(srcPage, path);
138: pageManager.updatePage(dstPage);
139: }
140: */
141: }
142:
143: Iterator links = srcFolder.getLinks().iterator();
144: while (links.hasNext()) {
145: Link srcLink = (Link) links.next();
146: String path = concatenatePaths(destinationPath, srcLink
147: .getName());
148: if (!pageManager.linkExists(path)) {
149: Link dstLink = pageManager.copyLink(srcLink, path);
150: 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: }
162: Iterator folders = srcFolder.getFolders().iterator();
163: while (folders.hasNext()) {
164: Folder folder = (Folder) folders.next();
165: String newPath = concatenatePaths(destinationPath, folder
166: .getName());
167: if (!pageManager.folderExists(newPath)) {
168: Folder dstFolder = pageManager.copyFolder(folder,
169: newPath);
170: pageManager.updateFolder(dstFolder);
171: }
172: deepMergeFolder(pageManager, folder, newPath, null,
173: uniqueName);
174: }
175: }
176:
177: public static String concatenatePaths(String base, String path) {
178: String result = "";
179: if (base == null) {
180: if (path == null) {
181: return result;
182: }
183: return path;
184: } else {
185: if (path == null) {
186: return base;
187: }
188: }
189: if (base.endsWith(Folder.PATH_SEPARATOR)) {
190: if (path.startsWith(Folder.PATH_SEPARATOR)) {
191: result = base.concat(path.substring(1));
192: return result;
193: }
194:
195: } else {
196: if (!path.startsWith(Folder.PATH_SEPARATOR)) {
197: result = base.concat(Folder.PATH_SEPARATOR)
198: .concat(path);
199: return result;
200: }
201: }
202: return base.concat(path);
203: }
204:
205: /**
206: * Deep copy a folder
207: *
208: * @param source source folder
209: * @param dest destination folder
210: */
211: public static void deepCopyFolder(PageManager pageManager,
212: Folder srcFolder, String destinationPath, String owner)
213: throws NodeException {
214: boolean found = true;
215: try {
216: pageManager.getFolder(destinationPath);
217: } catch (FolderNotFoundException e) {
218: found = false;
219: }
220: if (found) {
221: throw new NodeException("Destination already exists");
222: }
223: Folder dstFolder = pageManager.copyFolder(srcFolder,
224: destinationPath);
225: if (owner != null) {
226: SecurityConstraints constraints = dstFolder
227: .getSecurityConstraints();
228: if (constraints == null) {
229: constraints = pageManager.newSecurityConstraints();
230: dstFolder.setSecurityConstraints(constraints);
231: }
232: dstFolder.getSecurityConstraints().setOwner(owner);
233: }
234: pageManager.updateFolder(dstFolder);
235:
236: Iterator pages = srcFolder.getPages().iterator();
237: while (pages.hasNext()) {
238: Page srcPage = (Page) pages.next();
239: String path = PageManagerUtils.concatenatePaths(
240: destinationPath, srcPage.getName());
241: Page dstPage = pageManager.copyPage(srcPage, path);
242: pageManager.updatePage(dstPage);
243: }
244:
245: Iterator links = srcFolder.getLinks().iterator();
246: while (links.hasNext()) {
247: Link srcLink = (Link) links.next();
248: String path = PageManagerUtils.concatenatePaths(
249: destinationPath, srcLink.getName());
250: Link dstLink = pageManager.copyLink(srcLink, path);
251: pageManager.updateLink(dstLink);
252: }
253:
254: Iterator folders = srcFolder.getFolders().iterator();
255: while (folders.hasNext()) {
256: Folder folder = (Folder) folders.next();
257: String newPath = concatenatePaths(destinationPath, folder
258: .getName());
259: deepCopyFolder(pageManager, folder, newPath, null);
260: }
261: }
262:
263: }
|