/** * Copyright (c) 2009-2010 Misys Open Source Solutions (MOSS) and others * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. * * Contributors: * Misys Open Source Solutions - initial API and implementation * - */ package org.openhealthtools.openxds.webapp.action; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.openhealthtools.openxds.webapp.control.HttpUtils; import com.opensymphony.xwork2.ActionSupport; /** * Implementation of <strong>ActionSupport</strong> that contains * convenience methods for subclasses. For example, getting the current * user and saving messages/errors. This class is intended to * be a base class for all Action classes. * * @author <a href="anilkumar.reddy@misys.com">Anil Kumar</a> */ public class BaseAction extends ActionSupport { private static final long serialVersionUID = 3525445612504421307L; /** * Constant for cancel result String */ public static final String CANCEL = "cancel"; /** * Transient log to prevent session synchronization issues - children can use instance for logging. */ protected transient final Log log = LogFactory.getLog(getClass()); protected String cancel; /** * Indicator for the page the user came from. */ protected String from; /** * Set to "delete" when a "delete" request parameter is passed in */ protected String delete; /** * Set to "save" when a "save" request parameter is passed in */ protected String save; PrintWriter _writer = null; HttpUtils _h = null; /** * Simple method that returns "cancel" result * @return "cancel" */ public String cancel() { return CANCEL; } /** * Save the message in the session, appending if messages already exist * @param msg the message to put in the session */ @SuppressWarnings("unchecked") protected void saveMessage(String msg) { List messages = (List) getRequest().getSession().getAttribute("messages"); if (messages == null) { messages = new ArrayList(); } messages.add(msg); getRequest().getSession().setAttribute("messages", messages); } /** * Convenience method to get the request * @return current request */ protected HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } /** * Convenience method to get the response * @return current response */ protected HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } /** * Convenience method to get the session. This will create a session if one doesn't exist. * @return the session from the request (request.getSession()). */ protected HttpSession getSession() { return getRequest().getSession(); } /** * Convenience method for setting a "from" parameter to indicate the previous page. * @param from indicator for the originating page */ public void setFrom(String from) { this.from = from; } public void setDelete(String delete) { this.delete = delete; } public void setSave(String save) { this.save = save; } protected HttpUtils h() throws ServletException { if (_h == null) _h = new HttpUtils(get_writer()); return _h; } protected void close() { if (_h != null) { _h.close(); _h = null; } _writer = null; } private PrintWriter get_writer() throws ServletException { if (_writer == null) { try { _writer = getResponse().getWriter(); } catch (IOException e) { throw new ServletException("doGet: cannot getWriter()"); } } return _writer; } }