Coverage Report - org.melati.servlet.ConfigServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigServlet
96%
76/79
67%
4/6
1.571
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/servlet/ConfigServlet.java,v $
 3  
  * $Revision: 1.35 $
 4  
  *
 5  
  * Copyright (C) 2000 Tim Joyce
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     Tim Joyce <timj At paneris.org>
 42  
  *     http://paneris.org/
 43  
  *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
 44  
  */
 45  
 
 46  
 package org.melati.servlet;
 47  
 
 48  
 import java.io.PrintWriter;
 49  
 import java.io.IOException;
 50  
 
 51  
 import javax.servlet.ServletConfig;
 52  
 import javax.servlet.ServletException;
 53  
 import javax.servlet.http.HttpServlet;
 54  
 import javax.servlet.http.HttpServletRequest;
 55  
 import javax.servlet.http.HttpServletResponse;
 56  
 
 57  
 import org.melati.Melati;
 58  
 import org.melati.PoemContext;
 59  
 import org.melati.MelatiConfig;
 60  
 import org.melati.util.ConnectionPendingException;
 61  
 import org.melati.util.MelatiWriter;
 62  
 
 63  
 /**
 64  
  * Config Servlet is the simplest way to use Melati.
 65  
  *
 66  
  * All a ConfigServlet does is to configure a melati and combine the
 67  
  * doGet and doPost methods.  Importantly it does not establish a poem session
 68  
  * leaving you to do this for yourself.
 69  
  *
 70  
  * If you want a poem session established, please extend PoemServlet.
 71  
  *
 72  
  * ConfigServlet does set up a basic PoemContext with the Method set,
 73  
  * but not the POEM logicaldatabase, table or troid.
 74  
  *
 75  
  * The URL is expected to take one of the following form:
 76  
  *
 77  
  * <BLOCKQUOTE><TT>
 78  
  * http://<I>h</I>/<I>s</I>/<I>meth</I>
 79  
  * </TT></BLOCKQUOTE>
 80  
  *
 81  
  * the method is broken out of the path info and passed to
 82  
  * your application code in the <TT>Melati</TT> and
 83  
  * <TT>PoemContext</TT> parameter
 84  
  *
 85  
  * <TABLE>
 86  
  *   <TR>
 87  
  *     <TD><TT><I>h</I></TT></TD>
 88  
  *     <TD>host name, such as <TT>www.melati.org</TT></TD>
 89  
  *   </TR>
 90  
  *   <TR>
 91  
  *     <TD><TT><I>s</I></TT></TD>
 92  
  *     <TD>
 93  
  *       servlet-determining part, such as
 94  
  *       <TT>melati/org.melati.admin.Admin</TT>
 95  
  *     </TD>
 96  
  *   </TR>
 97  
  *   <TR>
 98  
  *     <TD><TT><I>meth</I></TT></TD>
 99  
  *     <TD>
 100  
  *       A freeform string telling your servlet what it is meant to do.  This
 101  
  *       is automatically made available in templates as
 102  
  *       <TT>$melati.Method</TT>.
 103  
  *     </TD>
 104  
  *   </TR>
 105  
  * </TABLE>
 106  
  *
 107  
  * You can change the way these things are determined by overriding
 108  
  * <TT>poemContext(Melati)</TT>.
 109  
  */
 110  
 
 111  22
 public abstract class ConfigServlet extends HttpServlet {
 112  
 
 113  
   protected MelatiConfig melatiConfig;
 114  22
   protected String sysAdminName = "nobody";
 115  22
   protected String sysAdminEmail = "nobody@nobody.com";;
 116  
   
 117  
   /**
 118  
    * Inititialise Melati.
 119  
    *
 120  
    * @param config a <code>ServletConfig</code>
 121  
    * @throws ServletException is anything goes wrong
 122  
    */
 123  
   public void init(ServletConfig config) throws ServletException {
 124  22
     super.init(config);
 125  
     try {
 126  22
       melatiConfig = melatiConfig();
 127  1
     } catch (Exception e) {
 128  
       // log it to system.err as ServletExceptions go to the
 129  
       // servlet runner log (eg jserv.log), and don't have a stack trace!
 130  1
       e.printStackTrace(System.err);
 131  1
       throw new ServletException(e.toString ());
 132  21
     }
 133  21
   }
 134  
 
 135  
   /**
 136  
    * Handles GET.
 137  
    *
 138  
    * @param request the incoming <code>HttpServletRequest</code>
 139  
    * @param response the outgoing <code>HttpServletResponse</code>
 140  
    */
 141  
   public void doGet(HttpServletRequest request, 
 142  
                     HttpServletResponse response) {
 143  4
     doGetPostRequest(request, response);
 144  4
   }
 145  
 
 146  
   /**
 147  
    * Handle a POST.
 148  
    *
 149  
    * @param request the incoming <code>HttpServletRequest</code>
 150  
    * @param response the outgoing <code>HttpServletResponse</code>
 151  
    */
 152  
   public void doPost(HttpServletRequest request, 
 153  
                      HttpServletResponse response) {
 154  14
     doGetPostRequest(request, response);
 155  14
   }
 156  
 
 157  
   /**
 158  
    * Process the request.
 159  
    *
 160  
    * Exceptions are presented to the user if practicable, or written to the log. 
 161  
    * 
 162  
    * @param request the incoming <code>HttpServletRequest</code>
 163  
    * @param response the outgoing <code>HttpServletResponse</code>
 164  
    */
 165  
   private void doGetPostRequest(final HttpServletRequest request, 
 166  
                                 final HttpServletResponse response) {
 167  18
     Melati melati = new Melati(melatiConfig, request, response);
 168  
     try {
 169  18
       melati.establishCharsets();
 170  18
       PoemContext poemContext = poemContext(melati);
 171  18
       melati.setPoemContext(poemContext);
 172  18
       doConfiguredRequest(melati);
 173  
       // send the output to the client
 174  14
       melati.write();
 175  
     }
 176  4
     catch (Exception e) {
 177  4
       error(melati,e);
 178  14
     }
 179  18
   }
 180  
 
 181  
   /**
 182  
    * Send an error message.
 183  
    *
 184  
    * @param melati the {@link Melati}
 185  
    * @param e      the {@link Exception} to report
 186  
    */
 187  
   public void error(Melati melati, Exception e) {
 188  
     // has it been trapped already, if so, we don't need to relog it here
 189  2
     if (! (e instanceof TrappedException)) {
 190  
       try { 
 191  
         // log it
 192  2
         e.printStackTrace(System.err);
 193  
         // and put it on the page
 194  2
         melati.setResponseContentType ("text/html");
 195  2
         MelatiWriter mw =  melati.getWriter();
 196  
         // get rid of anything that has been written so far
 197  2
         mw.reset();
 198  2
         PrintWriter out = mw.getPrintWriter();
 199  2
         if (e instanceof ConnectionPendingException) {
 200  1
           writeConnectionPendingException(out,e);
 201  
         } else {
 202  1
           writeError(out,e);
 203  
         }
 204  2
         melati.write();
 205  0
       } catch (IOException f) {
 206  0
         e.printStackTrace(System.err);
 207  0
         throw new TrappedException(f.toString());
 208  2
       }
 209  
     }
 210  2
   }
 211  
   
 212  
   /**
 213  
    * Print an error directly to the client.
 214  
    *
 215  
    * This is rarely called, eg when the template engine 
 216  
    * fails to render the default error template.
 217  
    *
 218  
    * @param out the <code>PrintWriter</code> to print to 
 219  
    * @param e   the {@link Exception} to report
 220  
    */
 221  
   public void writeError(PrintWriter out, Exception e) {
 222  1
     out.println("<html><head><title>Melati Error</title></head>");
 223  1
     out.println("<!-- HTML generated in " + 
 224  
                 "org.melati.servlet.ConfigServlet.java -->");
 225  1
     out.println("<body><h2>Melati Error</h2>");
 226  1
     out.println("<h3>Reported from ConfigServlet</h3>");
 227  1
     out.println("<p>An error has occured in the application"); 
 228  1
     out.println("that runs this website, please contact <a href='mailto:");
 229  1
     out.println(getSysAdminEmail() + "'>" + getSysAdminName() + "</a>");
 230  1
     out.println(", with the information given below.</p>");
 231  1
     out.println("<h4><font color=red><pre>");
 232  1
     e.printStackTrace(out);
 233  1
     out.println("</pre></font></h4></body></html>");
 234  1
   }    
 235  
   
 236  
   /**
 237  
    * Print the <code>ConnectionPendingException</code>  directly to the client.
 238  
    *
 239  
    * This is called if a request is made whilst the system is 
 240  
    * still being initialised.
 241  
    *
 242  
    * @param out the <code>PrintWriter</code> to print to 
 243  
    * @param e   the {@link Exception} to report
 244  
    */
 245  
   public void writeConnectionPendingException(PrintWriter out, Exception e) {
 246  1
     out.println("<html><head><title>Database Initialising</title>\n");
 247  1
     out.println("<META HTTP-EQUIV='Refresh' CONTENT='30'>\n</head>\n");
 248  1
     out.println("<!-- Generated in org.melati.servlet.ConfigServlet.java -->");
 249  1
     out.println("<body><center><h2>Database Initialising</h2><p>&nbsp;</p>");
 250  1
     out.println("<p><b>Sorry</b>, ");
 251  1
     out.println("the database that runs this website is just starting up.");
 252  1
     out.println("This takes a few seconds, ");
 253  1
     out.println("so you should be able to use the site in a moment.");
 254  1
     out.println("<p>This page will refresh in 30 seconds, ");
 255  1
     out.println("and you will be able to continue.</p>");
 256  1
     out.println("<!--");
 257  1
     e.printStackTrace(out);
 258  1
     out.println("--></center></body></html>");
 259  1
   }    
 260  
 
 261  
   /** 
 262  
    * This method <b>SHOULD</b> be overidden.
 263  
    * @return the System Administrators name.
 264  
    */
 265  
   public String getSysAdminName () {
 266  2
     return sysAdminName;
 267  
   }
 268  
 
 269  
   /** 
 270  
    * This method <b>SHOULD</b> be overidden.
 271  
    * @return the System Administrators email address.
 272  
    */
 273  
   public String getSysAdminEmail () {
 274  2
     return sysAdminEmail;
 275  
   }
 276  
 
 277  
   /**
 278  
    * @param sysAdminEmail The sysAdminEmail to set.
 279  
    */
 280  
   protected void setSysAdminEmail(String sysAdminEmail) {
 281  12
     this.sysAdminEmail = sysAdminEmail;
 282  12
   }
 283  
 
 284  
   
 285  
   /**
 286  
    * @param sysAdminName The sysAdminName to set.
 287  
    */
 288  
   protected void setSysAdminName(String sysAdminName) {
 289  12
     this.sysAdminName = sysAdminName;
 290  12
   }
 291  
 
 292  
   protected PoemContext poemContext(Melati melati) 
 293  
       throws PathInfoException {
 294  6
     PoemContext it = new PoemContext();
 295  6
     String[] parts = melati.getPathInfoParts();
 296  6
     if (parts.length > 0)
 297  6
      it.setMethod(parts[parts.length - 1]);
 298  6
    return it;
 299  
  }
 300  
   
 301  
   /** 
 302  
    * To override any setting from org.melati.MelatiConfig.properties,
 303  
    * simply override this method and return a valid MelatiConfig.
 304  
    *
 305  
    * eg to use a different AccessHandler from the default:
 306  
    *
 307  
    * <PRE>
 308  
    *   protected MelatiConfig melatiConfig() throws MelatiException {
 309  
    *     MelatiConfig config = super.melatiConfig();
 310  
    *     config.setAccessHandler(new YourAccessHandler());
 311  
    *     return config;
 312  
    *   }
 313  
    * </PRE>
 314  
    * 
 315  
    * @return a new {@link MelatiConfig}
 316  
    */
 317  
   protected MelatiConfig melatiConfig() {
 318  21
     MelatiConfig m = new MelatiConfig();
 319  21
     m.setRealPath(getServletConfig().getServletContext().getRealPath("/"));
 320  21
     return m;
 321  
   }
 322  
   
 323  
   /**
 324  
    * Instantiate this method to build up your own output.
 325  
    * @param melati
 326  
    * @throws Exception if anything goes wrong
 327  
    */
 328  
   protected abstract void doConfiguredRequest(Melati melati)
 329  
       throws Exception;
 330  
 
 331  
   
 332  
 }