001/**
002 * Copyright 2005-2018 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 */
016package org.kuali.rice.krad.web.controller;
017
018import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
019import org.kuali.rice.krad.uif.UifConstants;
020import org.kuali.rice.krad.uif.util.ScriptUtils;
021import org.kuali.rice.krad.util.GlobalVariables;
022import org.kuali.rice.krad.util.KRADPropertyConstants;
023import org.kuali.rice.krad.web.form.UifFormBase;
024import org.springframework.stereotype.Controller;
025import org.springframework.validation.BindingResult;
026import org.springframework.web.bind.annotation.ModelAttribute;
027import org.springframework.web.bind.annotation.RequestMapping;
028import org.springframework.web.bind.annotation.RequestParam;
029import org.springframework.web.bind.annotation.ResponseBody;
030import org.springframework.web.servlet.ModelAndView;
031
032import javax.servlet.http.HttpServletRequest;
033import javax.servlet.http.HttpServletResponse;
034
035/**
036 * Controller that receives various ajax requests from the client to manager server side state
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040@Controller
041@RequestMapping(value = "/listener")
042public class UifClientListener extends UifControllerBase {
043
044    /**
045     * {@inheritDoc}
046     */
047    @Override
048    protected UifFormBase createInitialForm() {
049        return new UifFormBase();
050    }
051
052    /**
053     * Invoked from the client when the user is leaving a view (by the portal tabs or other mechanism) to clear
054     * the form from session storage
055     *
056     * @param formKeyToClear key of form that should be cleared
057     * @return String json success string
058     */
059    @MethodAccessible
060    @RequestMapping(params = "methodToCall=clearForm")
061    public
062    @ResponseBody
063    String clearForm(@RequestParam("formKeyToClear") String formKeyToClear, HttpServletRequest request,
064            HttpServletResponse response) {
065
066        // clear form from session
067        GlobalVariables.getUifFormManager().removeFormWithHistoryFormsByKey(formKeyToClear);
068
069        return "{\"status\":\"success\"}";
070    }
071
072    /**
073     * Invoked from the client to retrieve text for a message
074     *
075     * @param key - key for the message
076     * @return String response in JSON format containing the message text
077     */
078    @MethodAccessible
079    @RequestMapping(params = "methodToCall=retrieveMessage")
080    public
081    @ResponseBody
082    String retrieveMessage(@RequestParam("key") String key, HttpServletRequest request, HttpServletResponse response) {
083        // namespace and component are not required, therefore may be null
084        String namespace = request.getParameter(KRADPropertyConstants.NAMESPACE_CODE);
085        String componentCode = request.getParameter(KRADPropertyConstants.COMPONENT_CODE);
086
087        String messageText = KRADServiceLocatorWeb.getMessageService().getMessageText(namespace, componentCode, key);
088
089        if (messageText == null) {
090            messageText = "";
091        }
092        else {
093            messageText = ScriptUtils.escapeJSONString(messageText);
094        }
095
096        return "{\"messageText\":\"" + messageText + "\"}";
097    }
098
099    /**
100     * Invoked from the session timeout warning dialog to keep a session alive on behalf of a user
101     *
102     * @return String json success string
103     */
104    @MethodAccessible
105    @RequestMapping(params = "methodToCall=keepSessionAlive")
106    public
107    @ResponseBody
108    String keepSessionAlive(HttpServletRequest request, HttpServletResponse response) {
109        return "{\"status\":\"success\"}";
110    }
111
112    /**
113     * Invoked from the session timeout warning dialog to log the user out, forwards to logout message view
114     */
115    @MethodAccessible
116    @RequestMapping(params = "methodToCall=logout")
117    public ModelAndView logout(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
118                HttpServletRequest request, HttpServletResponse response) {
119
120        request.getSession().invalidate();
121
122        return getModelAndViewWithInit(form, UifConstants.LOGGED_OUT_VIEW_ID);
123    }
124
125}