View Javadoc

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.profiler.impl;
18  
19  import org.apache.jetspeed.om.folder.Folder;
20  import org.apache.jetspeed.om.folder.FolderNotFoundException;
21  import org.apache.jetspeed.page.PageManager;
22  import org.apache.jetspeed.pipeline.PipelineException;
23  import org.apache.jetspeed.pipeline.valve.ValveContext;
24  import org.apache.jetspeed.portalsite.PortalSite;
25  import org.apache.jetspeed.profiler.Profiler;
26  import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
27  import org.apache.jetspeed.request.RequestContext;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * CreateUserTemplatePagesValveImpl
34   * 
35   * Create User Pages from template folder on first login feature
36   * The CreateUserTemplatePagesValveImpl creates a new user's home page from the user template folder.
37   * 
38   * @author Woonsan Ko
39   * @version $Id: CreateUserTemplatePagesValveImpl.java 555324 2007-07-11 16:23:56Z ate $
40   */
41  public class CreateUserTemplatePagesValveImpl extends ProfilerValveImpl
42  {
43      private static final Log log = LogFactory.getLog(CreateUserTemplatePagesValveImpl.class);
44  
45      public static final String USER_TEMPLATE_FOLDER_REQUEST_ATTR_KEY = "org.apache.jetspeed.profiler.UserTemplateFolder";
46      
47      protected PageManager pageManager;
48      protected String defaultTemplateFolder = "/_user/template/";
49  
50      /***
51       * CreateUserTemplatePagesValveImpl - constructor
52       *
53       * @param profiler profiler component reference
54       * @param portalSite portal site component reference
55       * @param requestFallback flag to enable root folder fallback
56       * @param useHistory flag to enable selection of last visited folder page
57       * @param pageManager pageManagerComponent reference
58       */
59      public CreateUserTemplatePagesValveImpl(Profiler profiler, PortalSite portalSite, boolean requestFallback, boolean useHistoryPageManager, PageManager pageManager)
60      {
61          super(profiler, portalSite, requestFallback, useHistoryPageManager);
62          this.pageManager = pageManager;
63      }
64      
65      /***
66       * CreateUserTemplatePagesValveImpl - constructor
67       *
68       * @param profiler profiler component reference
69       * @param portalSite portal site component reference
70       * @param requestFallback flag to enable root folder fallback
71       * @param pageManager pageManagerComponent reference
72       */
73      public CreateUserTemplatePagesValveImpl(Profiler profiler, PortalSite portalSite, 
74                               boolean requestFallback, PageManager pageManager)
75      {
76          super(profiler, portalSite, requestFallback);
77          this.pageManager = pageManager;
78      }
79  
80      /***
81       * CreateUserTemplatePagesValveImpl - constructor
82       *
83       * @param profiler profiler component reference
84       * @param portalSite portal site component reference
85       * @param pageManager pageManagerComponent reference
86       */
87      public CreateUserTemplatePagesValveImpl(Profiler profiler, PortalSite portalSite, PageManager pageManager)
88      {
89          super(profiler, portalSite);
90          this.pageManager = pageManager;
91      }
92      
93      public void setDefaultTemplateFolder(String defaultTemplateFolder)
94      {
95          this.defaultTemplateFolder = defaultTemplateFolder;
96      }
97      
98      /***
99       * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)
100      */
101     public void invoke(RequestContext request, ValveContext context) throws PipelineException
102     {
103         boolean created = false;
104         
105         try
106         {
107             created = createUserFolderPages(request);
108         }
109         catch (Exception e)
110         {
111             log.error("Exception occurred during creating user folder.", e);
112         }
113         finally
114         {
115             if (created)
116             {
117                 super.invoke(request, context);
118             }
119             else
120             {
121                 context.invokeNext(request);
122             }
123         }
124     }
125     
126     private boolean createUserFolderPages(RequestContext request) throws Exception
127     {
128         boolean created = false;
129         
130         String userName = request.getUserPrincipal().getName();
131         String userFolder = Folder.USER_FOLDER + userName;        
132         
133         boolean found = true;
134         
135         try
136         {
137             this.pageManager.getFolder(userFolder);
138         }
139         catch (FolderNotFoundException e)
140         {
141             found = false;
142         }
143 
144         if (!found)
145         {
146             try
147             {
148                 String templateFolder = (String) request.getAttribute(USER_TEMPLATE_FOLDER_REQUEST_ATTR_KEY);
149                 
150                 if (templateFolder == null)
151                 {
152                     templateFolder = this.defaultTemplateFolder;
153                 }
154                 
155                 Folder source = this.pageManager.getFolder(templateFolder);
156                 
157                 // copy the entire dir tree from the template folder
158                 this.pageManager.deepCopyFolder(source, userFolder, userName);
159                 created = true;
160             }
161             catch (Exception e)
162             {
163                 throw e;
164             }
165         }
166         
167         return created;
168     }
169 }