Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

GenericServlet.java

00001 package edu.virtualschool.jwaa;
00002 
00003 import java.io.IOException;
00004 import java.util.HashMap;
00005 
00006 import javax.servlet.RequestDispatcher;
00007 import javax.servlet.ServletContext;
00008 import javax.servlet.ServletException;
00009 import javax.servlet.http.HttpServlet;
00010 import javax.servlet.http.HttpServletRequest;
00011 import javax.servlet.http.HttpServletResponse;
00012 import javax.servlet.http.HttpSession;
00013 
00014 import org.apache.log4j.BasicConfigurator;
00015 import org.apache.log4j.Logger;
00016 
00027 public abstract class GenericServlet extends HttpServlet
00028 {
00029   final HashMap dispatchTable = new HashMap();
00030   static Logger logger = Logger.getLogger(GenericServlet .class.getName());
00031 
00038   public GenericServlet(MetaPage[] metaPages)
00039   {
00040     BasicConfigurator.configure();
00041     for (int i = 0; i < metaPages.length; i++)
00042     {
00043       MetaPage metaPage = metaPages[i];
00044       String url = metaPage.link.getText();
00045 
00046       MetaPage prior = (MetaPage)dispatchTable.get(url);
00047       if (prior != null)
00048         logger.error("Non-unique url: " + url + ". See \n" +
00049             " page "+metaPage+" and \n" +
00050             " page "+prior + "\n");
00051 
00052       dispatchTable.put(url, metaPage);
00053     }
00054   }
00058   public void doGet(HttpServletRequest req, HttpServletResponse rsp)
00059     throws ServletException, IOException
00060   {
00061     doRequest(req, rsp);
00062   }
00066   public void doPost(HttpServletRequest req, HttpServletResponse rsp)
00067     throws ServletException, IOException
00068   {
00069     doRequest(req, rsp);
00070   }
00080   public final void  doRequest(HttpServletRequest req, HttpServletResponse rsp)
00081     throws IOException, ServletException
00082   {
00083     AccountAbstraction person = getAccountOrNull(req);
00084     String uri = req.getRequestURI();
00085     MetaPage meta = findMetaPage(uri);
00086     logRequest(meta, req, rsp);
00087     if (!meta.allowsAccessBy(person))
00088         meta = getRefusePage();
00089     try
00090     {
00091       GenericPage page = (GenericPage)meta.pageClass.newInstance();
00092       page.init(meta, this, req, rsp);
00093       page.run();
00094     }
00095     catch (IgnorableFault e)
00096     {
00097     }
00098     catch (IOFault e)
00099     {
00100       doFault(meta, req, rsp, e);
00101       throw (IOException)e.getChainedException();
00102     }
00103     catch (IOException e)
00104     {
00105       doFault(meta, req, rsp, e);
00106       throw e;
00107     }
00108     catch (ServletException e)
00109     {
00110       doFault(meta, req, rsp, e);
00111       throw e;
00112     }
00113     catch (Throwable e)
00114     {
00115       doFault(meta, req, rsp, e);
00116     }
00117   }
00125   public AccountAbstraction getAccountOrNull(HttpServletRequest req)
00126   {
00127     HttpSession session = req.getSession();
00128     if (session == null)
00129       return null;
00130     AccountAbstraction person = (AccountAbstraction) session.getAttribute(GenericPage.SESSION_ATTRIBUTE_NAME);
00131     if (person == null)
00132       return null;
00133     else
00134       return person;
00135   }
00143   public void  logRequest(MetaPage meta, HttpServletRequest req, HttpServletResponse rsp)
00144   {
00145     if (logger.isDebugEnabled())
00146     {
00147       logger.debug("request: "+req.getClass().getName());
00148       logger.debug("  contextPath:    "+req.getContextPath());
00149       logger.debug("  servletPath:    "+req.getServletPath());
00150       logger.debug("  pathInfo:       "+req.getPathInfo());
00151       logger.debug("  queryString:    "+req.getQueryString());
00152       logger.debug("  requestURI:     "+req.getRequestURI());
00153       logger.debug("  page:           "+meta);
00154     }
00155   }
00165   public MetaPage findMetaPage(String key)
00166   {
00167     Object o = dispatchTable.get(key);
00168     MetaPage meta = (MetaPage)o;
00169     if (meta == null)
00170       meta = getDefaultPage(); 
00171     return meta;
00172   }
00180   public abstract void doFault(MetaPage meta, HttpServletRequest req, HttpServletResponse rsp, Throwable e);
00188   public abstract AccountAbstraction getNullAccount();
00194   public abstract MetaPage getDefaultPage();
00200   public abstract MetaPage getRefusePage();
00209   public abstract MetaPage getRootPage();
00210 //  /**
00211 //   * Get the servlet name from ServletConfig
00212 //   * 
00213 //   * @return String
00214 //   */
00215 //  public final String getServletNameX()
00216 //  {
00217 //    ServletConfig config = getServletConfig();
00218 //    if (config == null)
00219 //      return "ServletConfig not initialized";
00220 //    else
00221 //      return config.getServletName();
00222 //  }
00223 //  public final String getContextNameX()
00224 //  {
00225 //    ServletConfig config = getServletConfig();
00226 //    ServletContext context = config.getServletContext();
00227 //    return context.getServletContextName();
00228 //  }
00229 //  public final String toString()
00230 //  {
00231 //    return getServletName();
00232 //  }
00233 
00246   public final RequestDispatcher getRequestDispatcher(String path) throws IOFault
00247   {
00248     if (!path.startsWith("/"))
00249       throw new IOFault("" + path + " does not begin with \"/\"");
00250     else
00251     {
00252       int i = path.indexOf("/", 1);
00253       if (i < 0)
00254         throw new IOFault("" + path + " does not begin with \"/contextName/\"");
00255 
00256       String pathInContext = path.substring(i);
00257       ServletContext servletContext = getServletContext();
00258       RequestDispatcher dispatcher =
00259         servletContext.getRequestDispatcher(pathInContext);
00260 
00261       if (dispatcher == null)
00262         throw new IOFault(
00263           "" + this +".getRequestDispatcher: " + path + " failed");
00264       return dispatcher;
00265     }
00266   }
00267 }