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.mvc;
20  
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.springframework.extensions.surf.FrameworkUtil;
29  import org.springframework.extensions.surf.PageMapper;
30  import org.springframework.extensions.surf.PageMapperFactory;
31  import org.springframework.extensions.surf.RequestContext;
32  import org.springframework.extensions.surf.WebFrameworkConstants;
33  import org.springframework.extensions.surf.WebFrameworkServiceRegistry;
34  import org.springframework.extensions.surf.exception.PageMapperException;
35  import org.springframework.extensions.surf.exception.RendererExecutionException;
36  import org.springframework.extensions.surf.exception.RequestDispatchException;
37  import org.springframework.extensions.surf.render.PresentationUtil;
38  import org.springframework.extensions.surf.render.RenderContext;
39  import org.springframework.extensions.surf.render.RenderFocus;
40  import org.springframework.extensions.surf.types.TemplateInstance;
41  import org.springframework.extensions.webscripts.ProcessorModelHelper;
42  import org.springframework.extensions.webscripts.URLHelper;
43  import org.springframework.web.util.WebUtils;
44  
45  /**
46   * Default view implementation for Surf system pages
47   * 
48   * @author muzquiano
49   */
50  public class ErrorHandlerPageView extends AbstractWebFrameworkView 
51  {
52      private static Log logger = LogFactory.getLog(ErrorHandlerPageView.class);
53          
54      public ErrorHandlerPageView(WebFrameworkServiceRegistry serviceRegistry)
55      {
56          super(serviceRegistry);
57      }
58  
59      /* (non-Javadoc)
60       * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
61       */
62      protected void renderMergedOutputModel(
63              Map model, HttpServletRequest request, HttpServletResponse response) throws Exception 
64      {
65          // the request context
66          RequestContext context = (RequestContext) FrameworkUtil.getCurrentRequestContext();
67  
68          // the error handler page id
69          String errorHandlerPageId = this.getUrl();
70          
71          // set up the request context
72          setupRequestContext(context, model);
73                  
74          // Expose the model object as request attributes.
75          exposeModelAsRequestAttributes(model, request);
76          
77          // Bind url helper into context (if not already bound)
78          if (context.getValue(ProcessorModelHelper.MODEL_URL) == null)
79          {
80              // build a URLHelper object one time - it is immutable and can be reused
81              URLHelper urlHelper = new URLHelper(context.getRequest());
82              context.setValue(ProcessorModelHelper.MODEL_URL, urlHelper);
83          }        
84          
85          // create the top render context
86          RenderContext renderContext = getRenderService().provideRenderContext(context, request, response);        
87          
88          // dispatch to render the page
89          try
90          {
91              FrameworkUtil.getServiceRegistry().getRenderService().renderErrorHandlerPage(renderContext, errorHandlerPageId);
92          }
93          catch (Throwable t)
94          {
95              // log but not mandatory handling
96              FrameworkUtil.logFullStacktrace(t);
97          }
98          finally
99          {
100             // release any resources associated with the request context
101             context.release();            
102         }    
103     }
104 
105     /**
106      * Expose forward request attributes.
107      * 
108      * @param request the request
109      */
110     protected void exposeForwardRequestAttributes(HttpServletRequest request) {
111         WebUtils.exposeForwardRequestAttributes(request);
112     }
113     
114     /* (non-Javadoc)
115      * @see org.alfresco.web.framework.dispatcher.AbstractDispatcher#dispatchContext(org.alfresco.web.framework.render.RenderContext)
116      */
117     public void dispatchTemplate(RenderContext context)
118         throws RequestDispatchException
119     {
120         TemplateInstance templateInstance = context.getTemplate();
121         if (templateInstance != null)
122         {
123             // render content
124             PresentationUtil.renderContent(context, RenderFocus.BODY);
125         }
126         else
127         {
128             // there was an associated content display template instance
129             // however, it appears to be missing or unloadable
130             try
131             {
132                 getRenderService().renderSystemPage(context, 
133                     WebFrameworkConstants.SYSTEM_PAGE_CONTENT_ASSOCIATION_MISSING);
134             }
135             catch (RendererExecutionException ree)
136             {
137                 throw new RequestDispatchException(ree);
138             }                    
139         }
140     }    
141         
142     /**
143      * Sets up the request context with page binding information
144      * 
145      * @param context the context
146      * @param model the model
147      * 
148      * @throws PageMapperException the page mapper exception
149      */
150     public void setupRequestContext(RequestContext context, Map model)
151         throws PageMapperException
152     {
153         // set the spring mvc model onto the context
154         context.setViewModel(model);
155 
156         // run the page mapper to establish additional context
157         // this includes faulted objects, formats and more
158         PageMapperFactory factory = this.getServiceRegistry().getPageMapperFactory();
159         PageMapper pageMapper = factory.newInstance();
160         pageMapper.execute(context, (HttpServletRequest) context.getRequest());
161     }
162     
163 }