package faqapp.struts.actions;

import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

import faqapp.beans.ExceptionWrapperBean;
import faqapp.utils.Constants;

/**
 * A generic action that facilitates the extended display of an exception
 * on a separate JSP page. The Exception to be displayed is obtained from the
 * user HttpSession. The Exception is used to populate a JavaBean, which is then 
 * passed to a JSP for display. 
 * 
 * To use this generic stacktrace facility
 * <UL>Place the exception to be displayed in the user session, using a key of
 * faqapp.utils.Constants.THROWNEXCEPTION.
 * <UL>Add a link to the /showStacktrace.do action which will invoke this action
 * which will wrap the exception it finds under the known key and forward to a
 * JSP to display.
 */

public class ShowStackTraceAction extends Action
{
    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception java.io.IOException if an input/output error occurs
     * @exception javax.servlet.ServletException if a servlet exception occurs
     */
    public final ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws IOException, ServletException
    {
      // Get the exception to display from our session. Since this page is
      // not guaranteed to be called from a forward or include, the request
      // attributes can't be used reliably.  
       
      HttpSession session = (HttpSession)request.getSession();
      Exception ex = (Exception) session.getAttribute(Constants.THROWNEXCEPTION);

      // Could choose to remove the exception from the session so it 
      // can not be displayed again, preventing the invocation of this action
      // as a URL at any time to show errors.
   

      // Create a new bean to wrapper the exception.
      ExceptionWrapperBean exb = new ExceptionWrapperBean();

      // If there is no exception found in the session, create a new exception
      // with a default message that there is no exception available to display.
      if(ex!=null)
      {
        exb.setException(ex);
      }
      else
      {
	  Exception noValidException = new Exception(Constants.EXCEPTIONWRAPPERBEANMSG);
        exb.setException(noValidException);
      }
      request.setAttribute(Constants.EXCEPTIONWRAPPERBEAN,exb);
      return mapping.findForward("forward");
    }

}
