CGIApplication.cc

00001 /*
00002  * $Id: CGIApplication.cc,v 1.8 2006/04/03 19:47:25 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #include "CGIApplication.h"
00039 #include <fstream>
00040 #include <stdexcept>
00041 #include <syslog.h>
00042 #include "ClearSilverException.h"
00043 
00044 using namespace std;
00045 
00046 namespace ClearSilver
00047 {
00048 
00049                                 // constructors
00050   CGIApplication::CGIApplication ()
00051     : TraceObject(debug(), "CGIApplication"), cgi_()
00052   {}
00053 
00054   CGIApplication::CGIApplication (const char* name)
00055     : TraceObject(debug(), "CGIApplication", "const char*"), cgi_()
00056   {
00057     if (name)
00058       {
00059         ifstream f (name);
00060         if (!f)
00061           {
00062             string message;
00063             message += "ClearSilver::CGIApplication::CGIApplication(";
00064             message += name;
00065             message += "):  cannot read HDF file ";
00066             message += name;
00067             throw invalid_argument (message);
00068           }
00069         f.close();
00070         HDF hdf (name);
00071         cgi_ = CGI(hdf);
00072       }
00073   }
00074 
00075   CGIApplication::CGIApplication (const std::string& name)
00076     : TraceObject(debug(), "CGIApplication", "const std::string&"),
00077       cgi_()
00078   {
00079     if (!name.empty())
00080       {
00081         ifstream f (name.c_str());
00082         if (!f)
00083           {
00084             string message;
00085             message += "ClearSilver::CGIApplication::CGIApplication(";
00086             message += name;
00087             message += "):  cannot read HDF file ";
00088             message += name;
00089             throw invalid_argument (message);
00090           }
00091         f.close();
00092         HDF hdf (name);
00093         cgi_ = CGI(hdf);
00094       }
00095   }
00096 
00097   CGIApplication::CGIApplication (const HDF& hdf)
00098     : TraceObject(debug(), "CGIApplication", "const HDF&"),
00099       cgi_(hdf)
00100   {}
00101 
00102   CGIApplication::CGIApplication (const CGI& cgi)
00103     : TraceObject(debug(), "CGIApplication", "const CGI&"),
00104       cgi_(cgi)
00105   {}
00106 
00107   CGIApplication::CGIApplication (const CGIApplication& a)
00108     : TraceObject(a), cgi_(a.cgi_)
00109   {}
00110 
00111   CGIApplication::~CGIApplication () throw()
00112   {}
00113 
00114                                 // assignment
00115                                 // XXX - cannot use construct/swap idiom
00116                                 //       because of pure abstract functions
00117   CGIApplication&
00118   CGIApplication::operator = (const CGIApplication& a)
00119   {
00120     TraceObject::operator = (a);
00121     cgi_ = a.cgi_;
00122     return *this;
00123   }
00124 
00125   CGIApplication&
00126   CGIApplication::operator = (const CGI& cgi)
00127   {
00128     if (debug())
00129       syslog (LOG_DEBUG, "%s: this=0x%x, CGI=0x%x",
00130               "ClearSilver::CGIApplication::operator = (const CGI&)",
00131               this, &cgi);
00132     cgi_ = cgi;
00133     return *this;
00134   }
00135 
00136                                 // swap contents
00137   void
00138   CGIApplication::swap (CGIApplication& a) throw()
00139   {
00140     TraceObject::swap (a);
00141     cgi_.swap (a.cgi_);
00142   }
00143 
00144 
00145                                 // CGI application entry point
00146   void
00147   CGIApplication::operator () ()
00148   {
00149     if (debug())
00150       syslog (LOG_DEBUG, "%s", "ClearSilver::CGIApplication::operator()()");
00151     try
00152       {
00153         initialize ();
00154         if (authenticate ())
00155           {
00156             execute ();
00157             display ();
00158           }
00159         else
00160           {
00161             execute_unauthenticated ();
00162             display_unauthenticated ();
00163           }
00164         terminate ();
00165         cgi_.hdf().remove_cgi();
00166         if (debug())
00167           syslog (LOG_DEBUG, "%s",
00168                   "ClearSilver::CGIApplication::operator()() done");
00169       }
00170     catch (const ClearSilverException& e)
00171       {
00172         string message;
00173         message += "ClearSilverException:  ";
00174         message += e.what ();
00175         syslog (LOG_ERR, "%s", message.c_str());
00176         handle_exception (message);
00177       }
00178     catch (const invalid_argument& e)
00179       {
00180         string message;
00181         message += "std::invalid_argument:  ";
00182         message += e.what ();
00183         syslog (LOG_ERR, "%s", message.c_str());
00184         handle_exception (message);
00185       }
00186     catch (const logic_error& e)
00187       {
00188         string message;
00189         message += "std::logic_error:  ";
00190         message += e.what ();
00191         syslog (LOG_ERR, "%s", message.c_str());
00192         handle_exception (message);
00193       }
00194     catch (const runtime_error& e)
00195       {
00196         string message;
00197         message += "std::runtime_error:  ";
00198         message += e.what ();
00199         syslog (LOG_ERR, "%s", message.c_str());
00200         handle_exception (message);
00201       }
00202     catch (...)
00203       {
00204         string message;
00205         message += "Unknown exception caught.";
00206         syslog (LOG_ERR, "%s", message.c_str());
00207         handle_exception (message);
00208       }
00209   }
00210 
00211                                 // access data structures
00212   CGI
00213   CGIApplication::cgi () const
00214   {
00215     return cgi_;
00216   }
00217 
00218   HDF
00219   CGIApplication::hdf ()
00220   {
00221     return cgi_.hdf();
00222   }
00223 
00224                                 // CGI application hooks
00225   void
00226   CGIApplication::initialize () {}
00227 
00228   void
00229   CGIApplication::execute () {}
00230 
00231   void
00232   CGIApplication::display ()
00233   {
00234     if (debug())
00235       syslog (LOG_DEBUG, "%s", "ClearSilver::CGIApplication::display()");
00236     if (cgi())
00237       {
00238         HDF::const_iterator ti = hdf().find("hdf.templates.authenticated");
00239         if (ti != hdf().end())
00240           cgi().display(ti->value());
00241         else
00242           {
00243             ti = hdf().find("hdf.template");
00244             if (ti != hdf().end())
00245               cgi().display(ti->value());
00246             else
00247               throw ClearSilverException ("No ClearSilver template found.");
00248           }
00249       }
00250     else
00251       throw ClearSilverException ("Not running in a CGI environment.");
00252     if (debug())
00253       syslog (LOG_DEBUG, "%s",
00254               "ClearSilver::CGIApplication::display(): done");
00255   }
00256 
00257   void
00258   CGIApplication::execute_unauthenticated () {}
00259 
00260   void
00261   CGIApplication::display_unauthenticated ()
00262   {
00263     if (debug())
00264       syslog (LOG_DEBUG, "%s",
00265               "ClearSilver::CGIApplication::display_unauthenticated()");
00266     if (cgi())
00267       {
00268         HDF::const_iterator ti = hdf().find("hdf.templates.unauthenticated");
00269         if (ti != hdf().end())
00270           cgi().display(ti->value());
00271         else
00272           {
00273             ti = hdf().find("hdf.template");
00274             if (ti != hdf().end())
00275               cgi().display(ti->value());
00276             else
00277               throw ClearSilverException ("No ClearSilver template found.");
00278           }
00279       }
00280     else
00281       throw ClearSilverException ("Not running in a CGI environment.");
00282     if (debug())
00283       syslog (LOG_DEBUG, "%s",
00284               "ClearSilver::CGIApplication::display_unauthenticated(): done");
00285   }
00286 
00287   void
00288   CGIApplication::terminate () {}
00289 
00290   void
00291   CGIApplication::handle_exception (const string& message)
00292   {
00293     ClearSilverException e (message);
00294     throw e;
00295   }
00296 
00297   // debuging flag
00298   bool&
00299   CGIApplication::debug ()
00300   {
00301     static bool debug_ = false;
00302     return debug_;
00303   }
00304 
00305 };                              // namespace ClearSilver

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5