1 /**
2 * Copyright 2005-2013 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.UifConstants;
21 import org.kuali.rice.krad.uif.UifParameters;
22 import org.kuali.rice.krad.web.form.UifFormManager;
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 UifControllerHelper.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 UifFormBase uifForm = (UifFormBase) request.getAttribute(UifConstants.REQUEST_FORM);
96
97 if ((uifForm == null) || (uifForm.getView() == null && uifForm.getPostedView() == null)) {
98 return;
99 }
100
101 // perform form session handling
102 boolean persistFormToSession = uifForm.getView() != null ? uifForm.getView().isPersistFormToSession() :
103 uifForm.getPostedView().isPersistFormToSession();
104
105 // cleaning of view structure
106 if (uifForm.isRequestRedirected() || uifForm.isUpdateNoneRequest()) {
107 // view wasn't rendered, just set to null and leave previous posted view
108 uifForm.setView(null);
109 } else if (uifForm.isUpdateViewRequest() || uifForm.isUpdateDialogRequest()) {
110 // partial refresh on posted view
111 View postedView = uifForm.getPostedView();
112 if (postedView != null) {
113 postedView.getViewHelperService().cleanViewAfterRender(postedView);
114 }
115 } else {
116 // full view render
117 View view = uifForm.getView();
118 if (view != null) {
119 view.getViewHelperService().cleanViewAfterRender(view);
120 }
121
122 uifForm.setPostedView(view);
123 uifForm.setView(null);
124 }
125
126 // remove the session transient variables from the request form before adding it to the list of
127 // Uif session forms
128 if (persistFormToSession) {
129 uifFormManager.purgeForm(uifForm);
130 uifFormManager.addSessionForm(uifForm);
131 }
132 }
133
134 }