1 /**
2 * Copyright 2005-2011 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.service.KRADServiceLocatorWeb;
21 import org.kuali.rice.krad.service.SessionDocumentService;
22 import org.kuali.rice.krad.uif.service.ViewService;
23 import org.kuali.rice.krad.uif.util.UifWebUtils;
24 import org.kuali.rice.krad.util.GlobalVariables;
25 import org.kuali.rice.krad.util.KRADUtils;
26 import org.springframework.web.servlet.HandlerInterceptor;
27 import org.springframework.web.servlet.ModelAndView;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 /**
33 * Spring controller intercepter for KRAD controllers
34 *
35 * <p>
36 * Provides infrastructure for preparing the form and view before and after the controller is invoked.
37 * Included in this is form session management and preparation of the view for rendering
38 * </p>
39 *
40 * @author Kuali Rice Team (rice.collab@kuali.org)
41 */
42 public class UifControllerHandlerInterceptor implements HandlerInterceptor {
43 private static final Logger LOG = Logger.getLogger(UifControllerHandlerInterceptor.class);
44
45 @Override
46 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
47 throws Exception {
48 // do nothing
49 }
50
51 /**
52 * After the controller logic is executed, the form is placed into session
53 * and the corresponding view is prepared for rendering
54 *
55 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
56 * javax.servlet.http.HttpServletResponse, java.lang.Object,
57 * org.springframework.web.servlet.ModelAndView)
58 */
59 @Override
60 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
61 ModelAndView modelAndView) throws Exception {
62 UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
63 }
64
65 /**
66 * Before the controller executes the user session is set on GlobalVariables
67 * and messages are cleared
68 *
69 * TODO: do we need to clear the messages before this so that formatting and
70 * validation errors done during the binding are not cleared out?
71 *
72 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
73 * javax.servlet.http.HttpServletResponse, java.lang.Object)
74 */
75 @Override
76 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
77 final UserSession session = KRADUtils.getUserSessionFromRequest(request);
78
79 if (session == null) {
80 throw new IllegalStateException("the user session has not been established");
81 }
82
83 GlobalVariables.setUserSession(session);
84 GlobalVariables.clear();
85
86 return true;
87 }
88
89 public SessionDocumentService getSessionDocumentService() {
90 return KRADServiceLocatorWeb.getSessionDocumentService();
91 }
92
93 protected ViewService getViewService() {
94 return KRADServiceLocatorWeb.getViewService();
95 }
96
97 }