View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.support;
20  
21  import javax.servlet.ServletRequest;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.springframework.extensions.surf.PageMapper;
26  import org.springframework.extensions.surf.RequestContext;
27  import org.springframework.extensions.surf.WebFrameworkServiceRegistry;
28  import org.springframework.extensions.surf.exception.PageMapperException;
29  import org.springframework.extensions.surf.types.Page;
30  
31  /**
32   * Abstract base class for PageMapper implementations.  This
33   * is provided as a convenience to developers who wish to build their
34   * own custom PageMapper variations.
35   * 
36   * @author muzquiano
37   */
38  public abstract class AbstractPageMapper extends BaseFactoryBean implements PageMapper
39  {
40      public static Log logger = LogFactory.getLog(AbstractPageMapper.class);
41      
42      public AbstractPageMapper(WebFrameworkServiceRegistry serviceRegistry)
43      {
44          super(serviceRegistry);
45      }
46          
47      /* (non-Javadoc)
48       * @see org.alfresco.web.site.PageMapper#execute(org.alfresco.web.site.RequestContext, javax.servlet.ServletRequest)
49       */
50      public void execute(RequestContext context, ServletRequest request)
51          throws PageMapperException
52      {
53          executeMapper(context, request);
54          
55          // run some additional cleanup logic
56          postExecute(context, request);
57      }
58      
59      /**
60       * Execute mapper.
61       * 
62       * @param context the context
63       * @param request the request
64       * 
65       * @throws PageMapperException the page mapper exception
66       */
67      public abstract void executeMapper(RequestContext context,
68          ServletRequest request) throws PageMapperException;
69      
70      /**
71       * Handles clean up cases
72       * 
73       * @param context the context
74       * @param request the request
75       * 
76       * @throws PageMapperException the page mapper exception
77       */
78      public void postExecute(RequestContext context, ServletRequest request)
79          throws PageMapperException
80      {
81          if (context.getSiteConfiguration() == null)
82          {
83              if (logger.isDebugEnabled())
84                  debug(context, "No site configuration - performing reset");
85  
86              context.setPage(null);
87              context.setCurrentObject(null);
88          }
89          
90          // if we have absolutely nothing to dispatch to, then check to
91          // see if there is a root-page declared to which we can go
92          if (context.getPage() == null && context.getCurrentObjectId() == null)
93          {
94              // if the site configuration exists...
95              if (context.getSiteConfiguration() != null)
96              {
97                  // check if a root page exists to which we can forward
98                  Page rootPage = context.getRootPage();
99                  if (rootPage != null)
100                 {
101                     if (logger.isDebugEnabled())
102                         debug(context, "Set root page as current page");
103                     
104                     context.setPage(rootPage);
105                 }            
106             }
107         }    
108     }
109 
110     /**
111      * Helper method for debugging
112      */
113     protected void debug(RequestContext context, String value)
114     {
115         logger.debug("PageMapper [" + context.getId() + "] " + value);
116     }   
117 }