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

GenericPage.java

00001 package edu.virtualschool.jwaa;
00002 
00003 import java.io.IOException;
00004 import java.io.PrintWriter;
00005 import java.util.Enumeration;
00006 import java.util.HashMap;
00007 import java.util.Iterator;
00008 
00009 import javax.servlet.ServletException;
00010 import javax.servlet.http.Cookie;
00011 import javax.servlet.http.HttpServletRequest;
00012 import javax.servlet.http.HttpServletResponse;
00013 import javax.servlet.http.HttpSession;
00014 
00015 import org.apache.log4j.Logger;
00016 
00017 import edu.virtualschool.jwaa.field.Validatable;
00018 
00025 public abstract class GenericPage
00026 {
00027   public MetaPage meta;
00029   public GenericServlet servlet;
00031   public HttpServletRequest httpRequest;
00033   public HttpServletResponse httpResponse;
00034 
00035   final static Logger logger =
00036     Logger.getLogger(GenericPage.class.getName());
00042   final static String SESSION_ATTRIBUTE_NAME = "_PERSON_";
00043 
00053   public void init(
00054     MetaPage meta,
00055     GenericServlet servlet,
00056     HttpServletRequest req,
00057     HttpServletResponse rsp
00058   ) throws IOException, ServletException
00059   {
00060     this.meta = meta;
00061     this.servlet = servlet;
00062     this.httpRequest = req;
00063     this.httpResponse = rsp;
00064     rsp.setContentType("text/html");
00065   }
00074   public abstract void run() throws Fault;
00081   public void send(Object html) throws IOFault
00082   {
00083     try
00084     {
00085       PrintWriter writer = httpResponse.getWriter();
00086       writer.println(html.toString());
00087     }
00088     catch (IOException e)
00089     {
00090       throw new IOFault(e);
00091     }
00092   }
00100   public final AccountAbstraction getAccount()
00101   {
00102     HttpSession session = getSession();
00103     if (session == null)
00104       return servlet.getNullAccount();
00105     AccountAbstraction person = (AccountAbstraction) session.getAttribute(SESSION_ATTRIBUTE_NAME);
00106     if (person == null)
00107       return servlet.getNullAccount();
00108     else
00109       return person;
00110   }
00115   public final void setAccount(AccountAbstraction account)
00116   {
00117     if (account == null)
00118       account = servlet.getNullAccount();
00119     HttpSession session = getSession();
00120     if (session == null)
00121       throw new RuntimeFault("session is null. Should not happen");
00122     session.setAttribute(SESSION_ATTRIBUTE_NAME, account);
00123   }
00132   public RoleAbstraction getRequiredRole()
00133   {
00134     return meta.requiredRole;
00135   }
00145   public void redirect(MetaPage meta) throws Fault
00146   {
00147     try
00148     {
00149       String path = meta.link.getText();
00150       httpResponse.sendRedirect(httpResponse.encodeRedirectURL(path));
00151       throw new IgnorableFault();
00152     }
00153     catch (IOException e)
00154     {
00155       throw new IOFault("Fault redirecting to page " + meta);
00156     }
00157   }
00165   public void forward(MetaPage meta) throws Fault
00166   {
00167     logger.debug("forward: " + meta);
00168     meta.forward(this);
00169   }
00174   public MetaPage getParent()
00175   {
00176     return (MetaPage) meta.getParentNode();
00177   }
00182   public Iterator children()
00183   {
00184     return meta.childrenIterator();
00185   }
00186   public String getTitle()
00187   {
00188     return meta.defaultTitle;
00189   }
00190   public String toString()
00191   {
00192     return getClass().getName();
00193   }
00202   public final Validatable getField(
00203     String parameterName,
00204     Validatable defaultValue) throws Fault
00205   {
00206     String value = getParameter(parameterName, defaultValue.toString());
00207     return defaultValue.cloneWithValue(value);
00208   }
00217   public final Validatable[] getFields(
00218     String parameterName,
00219     Validatable defaultValue) throws Fault
00220   {
00221     String[] values = httpRequest.getParameterValues(parameterName);
00222     if (values == null)
00223       values = new String[0]; // paranoia
00224     Validatable[] array =
00225       (Validatable[]) java.lang.reflect.Array.newInstance(
00226         defaultValue.getClass(),
00227         values.length);
00228     for (int i = 0; i < values.length; i++)
00229       array[i] = defaultValue.cloneWithValue(values[i]);
00230     return array;
00231   }
00236   public final HashMap getParameterMap()
00237   {
00238     HashMap map = new HashMap();
00239     for (Enumeration e = httpRequest.getParameterNames(); e.hasMoreElements();)
00240     {
00241       String key = (String) e.nextElement();
00242       String value = httpRequest.getParameter(key);
00243       map.put(key, value);
00244     }
00245     return map;
00246   }
00255   public final String getParameter(Object key, Object defaultValue)
00256   {
00257     String value = httpRequest.getParameter(key.toString());
00258     if (value == null)
00259       return defaultValue.toString();
00260     else
00261       return value;
00262   }
00270   public final Object getAttribute(String attributeName) throws IOFault
00271   {
00272     return getSession().getAttribute(attributeName);
00273   }
00279   public final String getContextName()
00280   {
00281     return httpRequest.getContextPath();
00282   }
00291   public final Validatable getCookie(
00292     String cookieName,
00293     Validatable defaultValue) throws Fault
00294   {
00295     Cookie[] cookies = httpRequest.getCookies();
00296     for (int i = 0; i < cookies.length; i++)
00297     {
00298       if (cookies[i].getName().equals(cookieName))
00299         return defaultValue.cloneWithValue(cookies[i].getValue());
00300     }
00301     return defaultValue;
00302   }
00308   public final HttpServletRequest getHttpRequest()
00309   {
00310     return httpRequest;
00311   }
00317   public final HttpServletResponse getHttpResponse()
00318   {
00319     return httpResponse;
00320   }
00325   public final GenericServlet getServlet()
00326   {
00327     return servlet;
00328   }
00334   public final String getServletName()
00335   {
00336     return httpRequest.getServletPath();
00337   }
00343   public final HttpSession getSession()
00344   {
00345     return httpRequest.getSession(true);
00346   }
00354   public final PrintWriter getWriter() throws IOFault
00355   {
00356     try
00357     {
00358       return httpResponse.getWriter();
00359     }
00360     catch (IOException e)
00361     {
00362       throw new IOFault(e);
00363     }
00364   }
00371   public final synchronized void setAttribute(String key, Object value)
00372   {
00373     HttpSession session = getSession();
00374     // if (session == null) Log.fault(this + " ignoring null session", null);
00375     session.setAttribute(key, value);
00376   }
00384   public final void setCookie(String cookieName, String cookieValue, int msec)
00385   {
00386     Cookie cookie = new Cookie(cookieName, cookieValue);
00387     cookie.setMaxAge(cookieValue == null ? 0 : msec);
00388     httpResponse.addCookie(cookie);
00389   }
00395   public final String getArguments()
00396   {
00397     StringBuffer buf = new StringBuffer();
00398     for (Enumeration e = httpRequest.getParameterNames(); e.hasMoreElements();)
00399     {
00400       String key = (String) e.nextElement();
00401       String[] values = httpRequest.getParameterValues(key);
00402       String value = StringUtil.join('&', values);
00403       buf.append(key + "=" + value + (e.hasMoreElements() ? "&" : ""));
00404     }
00405     return buf.toString();
00406   }
00412   public String emitLink(MetaPage meta)
00413   {
00414     return meta.emitLink(this, null, null, null);
00415   }
00423   public String emitLink(MetaPage meta, Object anchor)
00424   {
00425     return meta.emitLink(this, anchor, null, null);
00426   }
00435   public String emitLink(MetaPage meta, Object anchor, Object title)
00436   {
00437     return meta.emitLink(this, anchor, title, null);
00438   }
00447   public String emitLink(MetaPage meta, Object anchor, Object[] args)
00448   {
00449     return meta.emitLink(this, anchor, null, args);
00450   }
00451   public String link(MetaPage meta, Object anchor, Object title, Object[] args)
00452   {
00453     return meta.emitLink(this, anchor, title, args);
00454   }
00459   public String emitForm()
00460   {
00461     return meta.emitForm(this, null);
00462   }
00471   public String emitForm(MetaPage meta)
00472   {
00473     return meta.emitForm(this, null);
00474   }
00484   public String emitForm(MetaPage meta, Object[] args)
00485   {
00486     return meta.emitForm(this, args);
00487   }
00488 }