Package web.servlets

Source Code of web.servlets.AbstractServlet

package web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.json.ExceptionJsonContainer;
import org.scotlandyard.impl.engine.GameEngine;
import org.scotlandyard.utils.ServletUtils;
import org.xml.sax.SAXException;

import com.meterware.pseudoserver.HttpRequest;
/**
* TODO add a description for this class
*
*
* @author Hussain Al-Mutawa
* @version 2.0
* @since Sun Sep 23, 2011
*/
public abstract class AbstractServlet extends HttpServlet implements IRequestProcessor {

  private String sessionId;

  @Override
  public Object processRequest(
        final HttpServletRequest request,
      final GameEngine engine) throws Exception{

    validateRequest (request, engine);
    return getOutput(request, engine);

  }

  public String getSessionId(){
    return this.sessionId;
  }

    @Override
    public abstract Object getOutput(
        final HttpServletRequest request,
      final GameEngine engine) throws Exception;

    @Override
    public abstract void validateRequest(
        final HttpServletRequest request,
      final GameEngine engine
        ) throws Exception;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws GameException
     */
    @SuppressWarnings("unchecked")
  private final void processRequest(
        HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException{

      PrintWriter out=response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma","no-cache");
        response.setDateHeader ("Expires", -1);


        //try{
      final GameEngine engine = GameEngine.instance();//(GameEngine)this.getServletContext().getAttribute("GameEngine");
      if (engine == null) {
        throw new ServletException("Game Engine has not been initialized, you may refresh the page to get it done. Or come backe later");
      }
      this.sessionId=request.getSession().getId();
        try {
        out.println(processRequest(request, engine));

      } catch (Exception e) {

        out.print(new ExceptionJsonContainer(e).toJson());

        e.printStackTrace();

      } finally{
        out.close();
      }


  }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
//      try{
        processRequest(request, response);
//      } catch (IOException ex) {
//        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "|"+request.getRequestURI()+"|"+ex.getClass().getName() +" was thrown", ex);
//          throw new IOException(ex);
//
//      } catch (ServletException ex) {
//        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "|"+request.getRequestURI()+"|"+ex.getClass().getName() +" was thrown", ex);
//          throw new ServletException(ex);
//      }
    }



  /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
//      try{
        processRequest(request, response);
//      } catch (IOException ex) {
//        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "|"+request.getRequestURI()+"|"+ex.getClass().getName() +" was thrown", ex);
//          throw new IOException(ex);
//
//      } catch (ServletException ex) {
//        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "|"+request.getRequestURI()+"|"+ex.getClass().getName() +" was thrown", ex);
//          throw new ServletException(ex);
//      }
    }





  /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
TOP

Related Classes of web.servlets.AbstractServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.