001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.web.controller;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.rice.krad.UserSession;
020    import org.kuali.rice.krad.uif.UifParameters;
021    import org.kuali.rice.krad.uif.util.UifFormManager;
022    import org.kuali.rice.krad.uif.util.UifWebUtils;
023    import org.kuali.rice.krad.uif.view.View;
024    import org.kuali.rice.krad.util.GlobalVariables;
025    import org.kuali.rice.krad.util.KRADUtils;
026    import org.kuali.rice.krad.web.form.UifFormBase;
027    import org.springframework.web.servlet.HandlerInterceptor;
028    import org.springframework.web.servlet.ModelAndView;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * Spring controller intercepter for KRAD controllers
035     *
036     * <p>
037     * Provides infrastructure for preparing the form and view before and after the controller is invoked.
038     * Included in this is form session management and preparation of the view for rendering
039     * </p>
040     *
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    public class UifControllerHandlerInterceptor implements HandlerInterceptor {
044        private static final Logger LOG = Logger.getLogger(UifControllerHandlerInterceptor.class);
045    
046        /**
047         * Before the controller executes the user session is set on GlobalVariables
048         * and messages are cleared
049         *
050         * TODO: do we need to clear the messages before this so that formatting and
051         * validation errors done during the binding are not cleared out?
052         *
053         * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
054         *      javax.servlet.http.HttpServletResponse, java.lang.Object)
055         */
056        @Override
057        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
058                Object handler) throws Exception {
059            final UserSession session = KRADUtils.getUserSessionFromRequest(request);
060    
061            if (session == null) {
062                throw new IllegalStateException("the user session has not been established");
063            }
064    
065            GlobalVariables.setUserSession(session);
066            GlobalVariables.clear();
067    
068            return true;
069        }
070    
071        /**
072         * After the controller logic is executed, the form is placed into session
073         * and the corresponding view is prepared for rendering
074         *
075         * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
076         *      javax.servlet.http.HttpServletResponse, java.lang.Object,
077         *      org.springframework.web.servlet.ModelAndView)
078         */
079        @Override
080        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
081                ModelAndView modelAndView) throws Exception {
082            UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
083        }
084    
085        /**
086         * After the view is rendered we can do some cleaning to reduce the size of the form storage in memory
087         *
088         * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
089         *      javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
090         */
091        @Override
092        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
093                Exception ex) throws Exception {
094            UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(UifParameters.FORM_MANAGER);
095    
096            UifFormBase uifForm = uifFormManager.getCurrentForm();
097            if (uifForm != null) {
098                if (uifForm.isSkipViewInit()) {
099                    // 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    }