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.profiler.impl;
018:
019: import org.apache.jetspeed.om.folder.Folder;
020: import org.apache.jetspeed.om.folder.FolderNotFoundException;
021: import org.apache.jetspeed.page.PageManager;
022: import org.apache.jetspeed.pipeline.PipelineException;
023: import org.apache.jetspeed.pipeline.valve.ValveContext;
024: import org.apache.jetspeed.portalsite.PortalSite;
025: import org.apache.jetspeed.profiler.Profiler;
026: import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
027: import org.apache.jetspeed.request.RequestContext;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * CreateUserTemplatePagesValveImpl
034: *
035: * Create User Pages from template folder on first login feature
036: * The CreateUserTemplatePagesValveImpl creates a new user's home page from the user template folder.
037: *
038: * @author Woonsan Ko
039: * @version $Id: CreateUserTemplatePagesValveImpl.java 555324 2007-07-11 16:23:56Z ate $
040: */
041: public class CreateUserTemplatePagesValveImpl extends ProfilerValveImpl {
042: private static final Log log = LogFactory
043: .getLog(CreateUserTemplatePagesValveImpl.class);
044:
045: public static final String USER_TEMPLATE_FOLDER_REQUEST_ATTR_KEY = "org.apache.jetspeed.profiler.UserTemplateFolder";
046:
047: protected PageManager pageManager;
048: protected String defaultTemplateFolder = "/_user/template/";
049:
050: /**
051: * CreateUserTemplatePagesValveImpl - constructor
052: *
053: * @param profiler profiler component reference
054: * @param portalSite portal site component reference
055: * @param requestFallback flag to enable root folder fallback
056: * @param useHistory flag to enable selection of last visited folder page
057: * @param pageManager pageManagerComponent reference
058: */
059: public CreateUserTemplatePagesValveImpl(Profiler profiler,
060: PortalSite portalSite, boolean requestFallback,
061: boolean useHistoryPageManager, PageManager pageManager) {
062: super (profiler, portalSite, requestFallback,
063: useHistoryPageManager);
064: this .pageManager = pageManager;
065: }
066:
067: /**
068: * CreateUserTemplatePagesValveImpl - constructor
069: *
070: * @param profiler profiler component reference
071: * @param portalSite portal site component reference
072: * @param requestFallback flag to enable root folder fallback
073: * @param pageManager pageManagerComponent reference
074: */
075: public CreateUserTemplatePagesValveImpl(Profiler profiler,
076: PortalSite portalSite, boolean requestFallback,
077: PageManager pageManager) {
078: super (profiler, portalSite, requestFallback);
079: this .pageManager = pageManager;
080: }
081:
082: /**
083: * CreateUserTemplatePagesValveImpl - constructor
084: *
085: * @param profiler profiler component reference
086: * @param portalSite portal site component reference
087: * @param pageManager pageManagerComponent reference
088: */
089: public CreateUserTemplatePagesValveImpl(Profiler profiler,
090: PortalSite portalSite, PageManager pageManager) {
091: super (profiler, portalSite);
092: this .pageManager = pageManager;
093: }
094:
095: public void setDefaultTemplateFolder(String defaultTemplateFolder) {
096: this .defaultTemplateFolder = defaultTemplateFolder;
097: }
098:
099: /**
100: * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)
101: */
102: public void invoke(RequestContext request, ValveContext context)
103: throws PipelineException {
104: boolean created = false;
105:
106: try {
107: created = createUserFolderPages(request);
108: } catch (Exception e) {
109: log.error(
110: "Exception occurred during creating user folder.",
111: e);
112: } finally {
113: if (created) {
114: super .invoke(request, context);
115: } else {
116: context.invokeNext(request);
117: }
118: }
119: }
120:
121: private boolean createUserFolderPages(RequestContext request)
122: throws Exception {
123: boolean created = false;
124:
125: String userName = request.getUserPrincipal().getName();
126: String userFolder = Folder.USER_FOLDER + userName;
127:
128: boolean found = true;
129:
130: try {
131: this .pageManager.getFolder(userFolder);
132: } catch (FolderNotFoundException e) {
133: found = false;
134: }
135:
136: if (!found) {
137: try {
138: String templateFolder = (String) request
139: .getAttribute(USER_TEMPLATE_FOLDER_REQUEST_ATTR_KEY);
140:
141: if (templateFolder == null) {
142: templateFolder = this .defaultTemplateFolder;
143: }
144:
145: Folder source = this .pageManager
146: .getFolder(templateFolder);
147:
148: // copy the entire dir tree from the template folder
149: this .pageManager.deepCopyFolder(source, userFolder,
150: userName);
151: created = true;
152: } catch (Exception e) {
153: throw e;
154: }
155: }
156:
157: return created;
158: }
159: }
|