OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
BESContainerStorageCatalog.cc
Go to the documentation of this file.
00001 // BESContainerStorageCatalog.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 "BESContainerStorageCatalog.h"
00034 #include "BESContainer.h"
00035 #include "BESCatalogUtils.h"
00036 #include "BESInternalError.h"
00037 #include "BESForbiddenError.h"
00038 #include "BESInfo.h"
00039 #include "BESServiceRegistry.h"
00040 #include "GNURegex.h"
00041 #include "Error.h"
00042 
00043 using namespace libdap ;
00044 
00066 BESContainerStorageCatalog::BESContainerStorageCatalog( const string &n )
00067     : BESContainerStorageVolatile( n )
00068 {
00069     _utils = BESCatalogUtils::Utils( n ) ;
00070     _root_dir = _utils->get_root_dir() ;
00071     _follow_sym_links = _utils->follow_sym_links() ;
00072 }
00073 
00074 BESContainerStorageCatalog::~BESContainerStorageCatalog()
00075 { 
00076 }
00077 
00106 void
00107 BESContainerStorageCatalog::add_container( const string &sym_name,
00108                                            const string &real_name,
00109                                            const string &type )
00110 {
00111     // make sure that the real name passed in is not oon the exclude list
00112     // for the catalog. First, remove any trailing slashes. Then find the
00113     // basename of the remaining real name. The make sure it's not on the
00114     // exclude list.
00115     string::size_type stopat = real_name.length() - 1 ;
00116     while( real_name[stopat] == '/' )
00117     {
00118         stopat-- ;
00119     }
00120     string new_name = real_name.substr( 0, stopat + 1 ) ;
00121 
00122     string basename ;
00123     string::size_type slash = new_name.rfind( "/" ) ;
00124     if( slash != string::npos )
00125     {
00126         basename = new_name.substr( slash+1, new_name.length() - slash ) ;
00127     }
00128     else
00129     {
00130         basename = new_name ;
00131     }
00132     if( !_utils->include( basename ) || _utils->exclude( basename ) )
00133     {
00134         string s = "Attempting to create a container with real name "
00135                    + real_name + " which is on the exclude list" ;
00136         throw BESForbiddenError( s, __FILE__, __LINE__ ) ;
00137     }
00138 
00139     // If the type is specified, then just pass that on. If not, then match
00140     // it against the types in the type list.
00141     string new_type = type ;
00142     if( new_type == "" )
00143     {
00144         BESCatalogUtils::match_citer i = _utils->match_list_begin() ;
00145         BESCatalogUtils::match_citer ie = _utils->match_list_end() ;
00146         bool done = false ;
00147         for( ; i != ie && !done; i++ )
00148         {
00149             BESCatalogUtils::type_reg match = (*i) ;
00150             try
00151             {
00152                 Regex reg_expr( match.reg.c_str() ) ;
00153                 if( reg_expr.match( real_name.c_str(), real_name.length() ) ==
00154                     static_cast<int>(real_name.length()) )
00155                 {
00156                     new_type = match.type ;
00157                     done = true ;
00158                 }
00159             }
00160             catch( Error &e )
00161             {
00162                 string serr = (string)"Unable to match data type, "
00163                               + "malformed Catalog TypeMatch parameter " 
00164                               + "in bes configuration file around " 
00165                               + match.reg + ": " + e.get_error_message() ;
00166                 throw BESInternalError( serr, __FILE__, __LINE__ ) ;
00167             }
00168         }
00169     }
00170     BESContainerStorageVolatile::add_container( sym_name, real_name, new_type );
00171 }
00172 
00182 bool
00183 BESContainerStorageCatalog::isData( const string &inQuestion,
00184                                     list<string> &provides )
00185 {
00186     string node_type = "" ;
00187     BESCatalogUtils::match_citer i = _utils->match_list_begin() ;
00188     BESCatalogUtils::match_citer ie = _utils->match_list_end() ;
00189     bool done = false ;
00190     for( ; i != ie && !done; i++ )
00191     {
00192         BESCatalogUtils::type_reg match = (*i) ;
00193         try
00194         {
00195             Regex reg_expr( match.reg.c_str() ) ;
00196             if( reg_expr.match( inQuestion.c_str(), inQuestion.length() ) ==
00197                 static_cast<int>(inQuestion.length()) )
00198             {
00199                 node_type = match.type ;
00200                 done = true ;
00201             }
00202         }
00203         catch( Error &e )
00204         {
00205             string serr = (string)"Unable to determine data products (is data), "
00206                           + "malformed Catalog TypeMatch parameter " 
00207                           + "in bes configuration file around " 
00208                           + match.reg + ": " + e.get_error_message() ;
00209             throw BESInternalError( serr, __FILE__, __LINE__ ) ;
00210         }
00211     }
00212 
00213     BESServiceRegistry::TheRegistry()->services_handled( node_type, provides ) ;
00214 
00215     return done ;
00216 }
00217 
00225 void
00226 BESContainerStorageCatalog::dump( ostream &strm ) const
00227 {
00228     strm << BESIndent::LMarg << "BESContainerStorageCatalog::dump - ("
00229                              << (void *)this << ")" << endl ;
00230     BESIndent::Indent() ;
00231     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00232     strm << BESIndent::LMarg << "utils: " << get_name() << endl ;
00233     BESIndent::Indent() ;
00234     _utils->dump( strm ) ;
00235     BESIndent::UnIndent() ;
00236     BESIndent::UnIndent() ;
00237 }
00238