|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
   package org.kuali.rice.kns.uif.util;  | 
  |  12 |     | 
     | 
  |  13 |     | 
   import javax.servlet.http.HttpServletRequest;  | 
  |  14 |     | 
   import javax.servlet.http.HttpServletResponse;  | 
  |  15 |     | 
     | 
  |  16 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  17 |     | 
   import org.apache.log4j.Logger;  | 
  |  18 |     | 
   import org.kuali.rice.kns.UserSession;  | 
  |  19 |     | 
   import org.kuali.rice.kns.service.KNSServiceLocatorWeb;  | 
  |  20 |     | 
   import org.kuali.rice.kns.service.SessionDocumentService;  | 
  |  21 |     | 
   import org.kuali.rice.kns.uif.UifConstants;  | 
  |  22 |     | 
   import org.kuali.rice.kns.uif.UifParameters;  | 
  |  23 |     | 
   import org.kuali.rice.kns.uif.container.View;  | 
  |  24 |     | 
   import org.kuali.rice.kns.uif.core.Component;  | 
  |  25 |     | 
   import org.kuali.rice.kns.uif.history.History;  | 
  |  26 |     | 
   import org.kuali.rice.kns.uif.service.ViewService;  | 
  |  27 |     | 
   import org.kuali.rice.kns.util.KNSConstants;  | 
  |  28 |     | 
   import org.kuali.rice.kns.web.spring.controller.UifControllerBase;  | 
  |  29 |     | 
   import org.kuali.rice.kns.web.spring.form.DocumentFormBase;  | 
  |  30 |     | 
   import org.kuali.rice.kns.web.spring.form.UifFormBase;  | 
  |  31 |     | 
   import org.springframework.web.servlet.ModelAndView;  | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |    0 |    public class UifWebUtils { | 
  |  45 |     | 
     | 
  |  46 |    0 |        private static final Logger LOG = Logger.getLogger(UifWebUtils.class);  | 
  |  47 |     | 
     | 
  |  48 |     | 
         | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
     | 
  |  59 |     | 
       public static UifFormBase getFormFromRequest(HttpServletRequest request) { | 
  |  60 |    0 |            UifFormBase form = null;  | 
  |  61 |    0 |            String formKeyParam = request.getParameter(UifParameters.FORM_KEY);  | 
  |  62 |    0 |            String docId = request.getParameter(KNSConstants.DOCUMENT_DOCUMENT_NUMBER);  | 
  |  63 |    0 |            if (StringUtils.isNotBlank(formKeyParam)) { | 
  |  64 |    0 |                form = (UifFormBase) request.getSession().getAttribute(formKeyParam);  | 
  |  65 |     | 
                 | 
  |  66 |    0 |                if (form == null) { | 
  |  67 |    0 |                    UserSession userSession = (UserSession) request.getSession()  | 
  |  68 |     | 
                           .getAttribute(KNSConstants.USER_SESSION_KEY);  | 
  |  69 |    0 |                    form = KNSServiceLocatorWeb.getSessionDocumentService().getUifDocumentForm(docId, formKeyParam,  | 
  |  70 |     | 
                           userSession, request.getRemoteAddr());  | 
  |  71 |     | 
               }  | 
  |  72 |     | 
           }  | 
  |  73 |    0 |            return form;  | 
  |  74 |     | 
       }  | 
  |  75 |     | 
     | 
  |  76 |     | 
         | 
  |  77 |     | 
     | 
  |  78 |     | 
     | 
  |  79 |     | 
     | 
  |  80 |     | 
     | 
  |  81 |     | 
     | 
  |  82 |     | 
     | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |     | 
     | 
  |  86 |     | 
     | 
  |  87 |     | 
     | 
  |  88 |     | 
     | 
  |  89 |     | 
       public static ModelAndView getUIFModelAndView(UifFormBase form, String viewId, String pageId) { | 
  |  90 |     | 
             | 
  |  91 |    0 |            form.setViewId(viewId);  | 
  |  92 |    0 |            if (StringUtils.isNotBlank(pageId)) { | 
  |  93 |    0 |                form.setPageId(pageId);  | 
  |  94 |     | 
           }  | 
  |  95 |     | 
     | 
  |  96 |     | 
             | 
  |  97 |    0 |            ModelAndView modelAndView = new ModelAndView();  | 
  |  98 |    0 |            modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);  | 
  |  99 |    0 |            modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);  | 
  |  100 |     | 
     | 
  |  101 |    0 |            return modelAndView;  | 
  |  102 |     | 
       }  | 
  |  103 |     | 
         | 
  |  104 |     | 
       public static ModelAndView getComponentModelAndView(Component component, Object model) { | 
  |  105 |     | 
     | 
  |  106 |    0 |            ModelAndView modelAndView = new ModelAndView();  | 
  |  107 |    0 |            modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);  | 
  |  108 |    0 |            modelAndView.addObject("Component", component); | 
  |  109 |    0 |            modelAndView.setViewName("ComponentUpdate"); | 
  |  110 |     | 
     | 
  |  111 |    0 |            return modelAndView;  | 
  |  112 |     | 
       }  | 
  |  113 |     | 
     | 
  |  114 |     | 
         | 
  |  115 |     | 
     | 
  |  116 |     | 
     | 
  |  117 |     | 
     | 
  |  118 |     | 
       public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,  | 
  |  119 |     | 
               ModelAndView modelAndView) throws Exception { | 
  |  120 |    0 |            if (handler instanceof UifControllerBase && (modelAndView != null)) { | 
  |  121 |    0 |                UifControllerBase controller = (UifControllerBase) handler;  | 
  |  122 |    0 |                UifFormBase form = null;  | 
  |  123 |     | 
                 | 
  |  124 |    0 |                if(modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)){ | 
  |  125 |    0 |                    Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);  | 
  |  126 |    0 |                    if (model instanceof UifFormBase) { | 
  |  127 |    0 |                        form = (UifFormBase) model;  | 
  |  128 |     | 
                         | 
  |  129 |     | 
                         | 
  |  130 |    0 |                        View view = form.getView();  | 
  |  131 |     | 
                         | 
  |  132 |     | 
                         | 
  |  133 |    0 |                        view.determineViewLabelPropertyName();  | 
  |  134 |     | 
                         | 
  |  135 |     | 
                         | 
  |  136 |    0 |                        History history = form.getFormHistory();  | 
  |  137 |    0 |                        if(history == null || request.getMethod().equals("GET")){ | 
  |  138 |    0 |                            history = new History();  | 
  |  139 |    0 |                            history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());  | 
  |  140 |    0 |                            history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());  | 
  |  141 |    0 |                            history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());  | 
  |  142 |     | 
                             | 
  |  143 |     | 
                             | 
  |  144 |    0 |                            if(StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))){ | 
  |  145 |    0 |                                history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HOME)));  | 
  |  146 |     | 
                           }  | 
  |  147 |    0 |                            if(StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))){ | 
  |  148 |    0 |                                history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY)));  | 
  |  149 |     | 
                           }  | 
  |  150 |    0 |                            history.setCurrent(form, request);  | 
  |  151 |    0 |                            history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));  | 
  |  152 |    0 |                            form.setFormHistory(history);  | 
  |  153 |     | 
                       }  | 
  |  154 |     | 
                         | 
  |  155 |    0 |                        form.setPreviousView(null);  | 
  |  156 |     | 
         | 
  |  157 |     | 
                         | 
  |  158 |    0 |                        request.getSession().setAttribute(form.getFormKey(), model);  | 
  |  159 |    0 |                        if (form instanceof DocumentFormBase){ | 
  |  160 |    0 |                            UserSession userSession = (UserSession) request.getSession().getAttribute(KNSConstants.USER_SESSION_KEY);              | 
  |  161 |    0 |                            getSessionDocumentService().setDocumentForm((DocumentFormBase)form, userSession, request.getRemoteAddr());  | 
  |  162 |     | 
                       }  | 
  |  163 |     | 
         | 
  |  164 |     | 
                         | 
  |  165 |    0 |                        checkMethodToCallAuthorization(request, controller, form);  | 
  |  166 |     | 
         | 
  |  167 |     | 
                         | 
  |  168 |    0 |                        prepareViewForRendering(form);  | 
  |  169 |     | 
                   }  | 
  |  170 |     | 
               }  | 
  |  171 |     | 
           }  | 
  |  172 |    0 |        }  | 
  |  173 |     | 
     | 
  |  174 |     | 
         | 
  |  175 |     | 
     | 
  |  176 |     | 
     | 
  |  177 |     | 
     | 
  |  178 |     | 
     | 
  |  179 |     | 
     | 
  |  180 |     | 
     | 
  |  181 |     | 
     | 
  |  182 |     | 
     | 
  |  183 |     | 
     | 
  |  184 |     | 
     | 
  |  185 |     | 
     | 
  |  186 |     | 
     | 
  |  187 |     | 
       public static void checkMethodToCallAuthorization(HttpServletRequest request, UifControllerBase controller,  | 
  |  188 |     | 
               UifFormBase form) { | 
  |  189 |     | 
             | 
  |  190 |     | 
             | 
  |  191 |    0 |            String methodToCall = request.getParameter(KNSConstants.DISPATCH_REQUEST_PARAMETER);  | 
  |  192 |     | 
     | 
  |  193 |    0 |            if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) { | 
  |  194 |    0 |                if (LOG.isDebugEnabled()) { | 
  |  195 |    0 |                    LOG.debug("'" + methodToCall + "' not in set of excempt methods: " | 
  |  196 |     | 
                           + controller.getMethodToCallsToNotCheckAuthorization());  | 
  |  197 |     | 
               }  | 
  |  198 |     | 
     | 
  |  199 |    0 |                controller.checkAuthorization(form, methodToCall);  | 
  |  200 |     | 
           } else { | 
  |  201 |    0 |                if (LOG.isDebugEnabled()) { | 
  |  202 |    0 |                    LOG.debug("'" + methodToCall + "' is exempt from auth checks."); | 
  |  203 |     | 
               }  | 
  |  204 |     | 
           }  | 
  |  205 |    0 |        }  | 
  |  206 |     | 
     | 
  |  207 |     | 
         | 
  |  208 |     | 
     | 
  |  209 |     | 
     | 
  |  210 |     | 
     | 
  |  211 |     | 
     | 
  |  212 |     | 
     | 
  |  213 |     | 
     | 
  |  214 |     | 
     | 
  |  215 |     | 
     | 
  |  216 |     | 
     | 
  |  217 |     | 
     | 
  |  218 |     | 
     | 
  |  219 |     | 
     | 
  |  220 |     | 
     | 
  |  221 |     | 
     | 
  |  222 |     | 
     | 
  |  223 |     | 
       public static void prepareViewForRendering(UifFormBase form) { | 
  |  224 |     | 
             | 
  |  225 |     | 
             | 
  |  226 |    0 |            View view = form.getView();  | 
  |  227 |    0 |            String viewId = form.getViewId();  | 
  |  228 |    0 |            if ((view == null) || !StringUtils.equals(viewId, view.getId())) { | 
  |  229 |    0 |                if (LOG.isDebugEnabled()) { | 
  |  230 |    0 |                    LOG.debug("Getting new view instance for view id: " + viewId); | 
  |  231 |     | 
               }  | 
  |  232 |     | 
     | 
  |  233 |    0 |                view = getViewService().getView(viewId, form.getViewRequestParameters());  | 
  |  234 |     | 
     | 
  |  235 |     | 
                 | 
  |  236 |    0 |                form.setRenderFullView(true);  | 
  |  237 |     | 
           }  | 
  |  238 |     | 
     | 
  |  239 |     | 
             | 
  |  240 |    0 |            if (StringUtils.equals(UifConstants.ViewStatus.FINAL, view.getViewStatus())) { | 
  |  241 |    0 |                if (LOG.isDebugEnabled()) { | 
  |  242 |    0 |                    LOG.debug("Rebuilding view due to final status, view id: " + viewId); | 
  |  243 |     | 
               }  | 
  |  244 |     | 
     | 
  |  245 |    0 |                view = getViewService().rebuildView(viewId, form, form.getViewRequestParameters());  | 
  |  246 |     | 
           } else { | 
  |  247 |     | 
                 | 
  |  248 |    0 |                getViewService().buildView(view, form);  | 
  |  249 |     | 
           }  | 
  |  250 |     | 
     | 
  |  251 |     | 
             | 
  |  252 |    0 |            form.setValidateDirty(view.isValidateDirty());  | 
  |  253 |     | 
     | 
  |  254 |     | 
             | 
  |  255 |    0 |            if (StringUtils.isNotBlank(form.getPageId())) { | 
  |  256 |    0 |                view.setCurrentPageId(form.getPageId());  | 
  |  257 |     | 
           }  | 
  |  258 |    0 |        }  | 
  |  259 |     | 
     | 
  |  260 |     | 
       protected static SessionDocumentService getSessionDocumentService() { | 
  |  261 |    0 |            return KNSServiceLocatorWeb.getSessionDocumentService();  | 
  |  262 |     | 
       }  | 
  |  263 |     | 
     | 
  |  264 |     | 
       protected static ViewService getViewService() { | 
  |  265 |    0 |            return KNSServiceLocatorWeb.getViewService();  | 
  |  266 |     | 
       }  | 
  |  267 |     | 
     | 
  |  268 |     | 
   }  |