OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESRequestHandlerList.cc
Go to the documentation of this file.
00001 // BESRequestHandlerList.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include "BESRequestHandlerList.h"
00034 #include "BESRequestHandler.h"
00035 #include "BESInternalError.h"
00036 #include "BESDataNames.h"
00037 
00038 BESRequestHandlerList *BESRequestHandlerList::_instance = 0 ;
00039 
00049 bool
00050 BESRequestHandlerList::add_handler( const string &handler_name,
00051                                     BESRequestHandler *handler_object )
00052 {
00053     if( find_handler( handler_name ) == 0 )
00054     {
00055         _handler_list[handler_name] = handler_object ;
00056         return true ;
00057     }
00058     return false ;
00059 }
00060 
00073 BESRequestHandler *
00074 BESRequestHandlerList::remove_handler( const string &handler_name )
00075 {
00076     BESRequestHandler *ret = 0 ;
00077     BESRequestHandlerList::Handler_iter i ;
00078     i = _handler_list.find( handler_name ) ;
00079     if( i != _handler_list.end() )
00080     {
00081         ret = (*i).second;
00082         _handler_list.erase( i ) ;
00083     }
00084     return ret ;
00085 }
00086 
00093 BESRequestHandler *
00094 BESRequestHandlerList::find_handler( const string &handler_name )
00095 {
00096     BESRequestHandlerList::Handler_citer i ;
00097     i = _handler_list.find( handler_name ) ;
00098     if( i != _handler_list.end() )
00099     {
00100         return (*i).second;
00101     }
00102     return 0 ;
00103 }
00104 
00112 BESRequestHandlerList::Handler_citer
00113 BESRequestHandlerList::get_first_handler()
00114 {
00115     return _handler_list.begin() ;
00116 }
00117 
00123 BESRequestHandlerList::Handler_citer
00124 BESRequestHandlerList::get_last_handler()
00125 {
00126     return _handler_list.end() ;
00127 }
00128 
00136 string
00137 BESRequestHandlerList::get_handler_names()
00138 {
00139     string ret ;
00140     bool first_name = true ;
00141     BESRequestHandlerList::Handler_citer i = _handler_list.begin() ;
00142     for( ; i != _handler_list.end(); i++ )
00143     {
00144         if( !first_name )
00145             ret += ", " ;
00146         ret += (*i).first ;
00147         first_name = false ;
00148     }
00149     return ret ;
00150 }
00151 
00172 void
00173 BESRequestHandlerList::execute_each( BESDataHandlerInterface &dhi )
00174 {
00175     dhi.first_container() ;
00176     while( dhi.container )
00177     {
00178         execute_current( dhi ) ;
00179         dhi.next_container() ;
00180     }
00181 }
00182 
00198 void
00199 BESRequestHandlerList::execute_all( BESDataHandlerInterface &dhi )
00200 {
00201     BESRequestHandlerList::Handler_citer i = get_first_handler() ;
00202     BESRequestHandlerList::Handler_citer ie = get_last_handler() ;
00203     for( ; i != ie; i++ ) 
00204     {
00205         BESRequestHandler *rh = (*i).second ;
00206         p_request_handler p = rh->find_handler( dhi.action ) ;
00207         if( p )
00208         {
00209             p( dhi ) ;
00210         }
00211     }
00212 }
00213 
00232 void
00233 BESRequestHandlerList::execute_once( BESDataHandlerInterface &dhi )
00234 {
00235     dhi.first_container() ;
00236     execute_current( dhi ) ;
00237 }
00238 
00255 void
00256 BESRequestHandlerList::execute_current( BESDataHandlerInterface &dhi )
00257 {
00258     if( dhi.container )
00259     {
00260         // FIXME: This needs to happen here, but really should be done
00261         // in the get_container_type method in the container class if it
00262         // needs to happen. But those methods are not virtual and would
00263         // require a release of all modules.
00264         dhi.container->access() ;
00265 
00266         BESRequestHandler *rh = find_handler( (dhi.container->get_container_type()).c_str() ) ;
00267         if( rh )
00268         {
00269             p_request_handler p = rh->find_handler( dhi.action ) ;
00270             // if we can't find the function, see if there is a catch all
00271             // function that handles or redirects the request.
00272             if( !p )
00273             {
00274                 p = rh->find_handler( BES_REQUEST_HANDLER_CATCH_ALL ) ;
00275             }
00276 
00277             if( p )
00278             {
00279                 p( dhi ) ;
00280                 if( dhi.container )
00281                 {
00282                     string c_list = dhi.data[REAL_NAME_LIST] ;
00283                     if( !c_list.empty() )
00284                        c_list += ", " ;
00285                     c_list += dhi.container->get_real_name() ;
00286                     dhi.data[REAL_NAME_LIST] = c_list ;
00287                 }
00288             } else {
00289                 string se = "Request handler \""
00290                             + dhi.container->get_container_type()
00291                             + "\" does not handle the response type \""
00292                             + dhi.action + "\"" ;
00293                 throw BESInternalError( se, __FILE__, __LINE__ ) ;
00294             }
00295         } else {
00296             string se = "The data handler \""
00297                         + dhi.container->get_container_type()
00298                         + "\" does not exist" ;
00299             throw BESInternalError( se, __FILE__, __LINE__ ) ;
00300         }
00301     }
00302 }
00303 
00311 void
00312 BESRequestHandlerList::dump( ostream &strm ) const
00313 {
00314     strm << BESIndent::LMarg << "BESRequestHandlerList::dump - ("
00315                              << (void *)this << ")" << endl ;
00316     BESIndent::Indent() ;
00317     if( _handler_list.size() )
00318     {
00319         strm << BESIndent::LMarg << "registered handlers:" << endl ;
00320         BESIndent::Indent() ;
00321         BESRequestHandlerList::Handler_citer i = _handler_list.begin() ;
00322         BESRequestHandlerList::Handler_citer ie = _handler_list.end() ;
00323         for( ; i != ie; i++ ) 
00324         {
00325             BESRequestHandler *rh = (*i).second ;
00326             rh->dump( strm ) ;
00327         }
00328         BESIndent::UnIndent() ;
00329     }
00330     else
00331     {
00332         strm << BESIndent::LMarg << "registered handlers: none" << endl ;
00333     }
00334     BESIndent::UnIndent() ;
00335 }
00336 
00337 BESRequestHandlerList *
00338 BESRequestHandlerList::TheList()
00339 {
00340     if( _instance == 0 )
00341     {
00342         _instance = new BESRequestHandlerList ;
00343     }
00344     return _instance ;
00345 }
00346