/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.web.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A servlet that sends error responses based on the errorCode parameter.
 * 
 * @author  Scott.Stark@jboss.org
 * @version $Revision: 1.1 $
 */
public class ErrorGeneratorServlet extends HttpServlet
{
   /**
    * Issues a response.sendError() for the errorCode parameter passed in. If
    * there is no errorCode parameter an IllegalStateException is thrown.
    * 
    * @param request
    * @param response
    * @throws ServletException
    * @throws IOException
    */ 
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
   {
      String errorCode = request.getParameter("errorCode");
      if( errorCode == null )
         throw new IllegalStateException("No errorCode parameter seen");

      int code = Integer.parseInt(errorCode);
      response.sendError(code);
   }

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
   {
      processRequest(request, response);
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
   {
      processRequest(request, response);
   }
   
}
