View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.web.controller;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.krad.UserSession;
20  import org.kuali.rice.krad.uif.UifParameters;
21  import org.kuali.rice.krad.uif.util.UifFormManager;
22  import org.kuali.rice.krad.uif.util.UifWebUtils;
23  import org.kuali.rice.krad.uif.view.View;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.util.KRADUtils;
26  import org.kuali.rice.krad.web.form.UifFormBase;
27  import org.springframework.web.servlet.HandlerInterceptor;
28  import org.springframework.web.servlet.ModelAndView;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * Spring controller intercepter for KRAD controllers
35   *
36   * <p>
37   * Provides infrastructure for preparing the form and view before and after the controller is invoked.
38   * Included in this is form session management and preparation of the view for rendering
39   * </p>
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class UifControllerHandlerInterceptor implements HandlerInterceptor {
44      private static final Logger LOG = Logger.getLogger(UifControllerHandlerInterceptor.class);
45  
46      /**
47       * Before the controller executes the user session is set on GlobalVariables
48       * and messages are cleared
49       *
50       * TODO: do we need to clear the messages before this so that formatting and
51       * validation errors done during the binding are not cleared out?
52       *
53       * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
54       *      javax.servlet.http.HttpServletResponse, java.lang.Object)
55       */
56      @Override
57      public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
58              Object handler) throws Exception {
59          final UserSession session = KRADUtils.getUserSessionFromRequest(request);
60  
61          if (session == null) {
62              throw new IllegalStateException("the user session has not been established");
63          }
64  
65          GlobalVariables.setUserSession(session);
66          GlobalVariables.clear();
67  
68          return true;
69      }
70  
71      /**
72       * After the controller logic is executed, the form is placed into session
73       * and the corresponding view is prepared for rendering
74       *
75       * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
76       *      javax.servlet.http.HttpServletResponse, java.lang.Object,
77       *      org.springframework.web.servlet.ModelAndView)
78       */
79      @Override
80      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
81              ModelAndView modelAndView) throws Exception {
82          UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
83      }
84  
85      /**
86       * After the view is rendered we can do some cleaning to reduce the size of the form storage in memory
87       *
88       * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
89       *      javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
90       */
91      @Override
92      public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
93              Exception ex) throws Exception {
94          UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(UifParameters.FORM_MANAGER);
95  
96          UifFormBase uifForm = uifFormManager.getCurrentForm();
97          if (uifForm != null) {
98              if (uifForm.isSkipViewInit()) {
99                  // partial refresh or query
100                 View postedView = uifForm.getPostedView();
101                 if (postedView != null) {
102                     postedView.getViewHelperService().cleanViewAfterRender(postedView);
103                 }
104             } else {
105                 // full view render
106                 View view = uifForm.getView();
107                 if (view != null) {
108                     view.getViewHelperService().cleanViewAfterRender(view);
109                 }
110 
111                 uifForm.setPostedView(view);
112                 uifForm.setView(null);
113             }
114         }
115     }
116 
117 }