bes  Updated for version 3.20.6
BESCatalogResponseHandler.cc
1 // BESCatalogResponseHandler.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "BESCatalogResponseHandler.h"
34 #include "BESInfoList.h"
35 #include "BESInfo.h"
36 #include "BESRequestHandlerList.h"
37 #include "BESRequestHandler.h"
38 #include "BESNames.h"
39 #include "BESDataNames.h"
40 #include "BESCatalogList.h"
41 #include "BESCatalog.h"
42 #include "BESCatalogEntry.h"
43 #include "BESCatalogUtils.h"
44 #include "BESSyntaxUserError.h"
45 #include "BESNotFoundError.h"
46 #include "BESDebug.h"
47 #include "BESStopWatch.h"
48 
49 using std::endl;
50 using std::ostream;
51 using std::string;
52 
53 BESCatalogResponseHandler::BESCatalogResponseHandler(const string &name) :
54  BESResponseHandler(name)
55 {
56 }
57 
58 BESCatalogResponseHandler::~BESCatalogResponseHandler()
59 {
60 }
61 
72 {
73  BESStopWatch sw;
74  if (BESISDEBUG(TIMING_LOG)) sw.start("BESCatalogResponseHandler::execute", dhi.data[REQUEST_ID]);
75 
76  BESInfo *info = BESInfoList::TheList()->build_info();
77  d_response_object = info;
78 
79  // Remove all of the leading slashes from the container (path) name
80  string container = dhi.data[CONTAINER];
81  string::size_type notslash = container.find_first_not_of("/", 0);
82  if (notslash != string::npos) {
83  container = container.substr(notslash);
84  }
85  if (container.empty()) container = "/";
86 
87  BESCatalog *besCatalog = 0;
88  string catalog = dhi.data[CATALOG];
89  if(catalog.empty()){
90  // Use default catalog to service request
92  catalog = besCatalog->get_catalog_name();
93  }
94  else {
95  // Use the specified catalog.
96  besCatalog = BESCatalogList::TheCatalogList()->find_catalog(catalog);
97  if (!besCatalog) {
98  string err = (string) "Not able to find the catalog '" + catalog + "'";
99  throw BESInternalError(err, __FILE__, __LINE__);
100  }
101  }
102 
103  BESCatalogEntry *entry = 0;
104  // we always want to get the container information from the
105  // default catalog, whether the node is / or not
106  entry = besCatalog->show_catalog(container, entry);
107  if (!entry) {
108  string err = (string) "Failed to find node " + container;
109  throw BESNotFoundError(err, __FILE__, __LINE__);
110  }
111 
112  // now that we have all the catalog entry information, display it
113  // start the response depending on if show catalog or show info
114  info->begin_response(CATALOG_RESPONSE_STR, dhi);
115  dhi.action_name = CATALOG_RESPONSE_STR;
116 
117  // start with the first level entry
118  BESCatalogUtils::display_entry(entry, info);
119 
120  BESCatalogEntry::catalog_citer ei = entry->get_beginning_entry();
121  BESCatalogEntry::catalog_citer ee = entry->get_ending_entry();
122  for (; ei != ee; ei++) {
123  BESCatalogUtils::display_entry((*ei).second, info);
124  info->end_tag("dataset");
125  }
126 
127  info->end_tag("dataset");
128 
129  // end the response object
130  info->end_response();
131 
132  delete entry;
133 }
134 
147 {
148  if (d_response_object) {
149  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
150  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
151  info->transmit(transmitter, dhi);
152  }
153 }
154 
161 void BESCatalogResponseHandler::dump(ostream &strm) const
162 {
163  strm << BESIndent::LMarg << "BESCatalogResponseHandler::dump - (" << (void *) this << ")" << endl;
164  BESIndent::Indent();
166  BESIndent::UnIndent();
167 }
168 
170 BESCatalogResponseHandler::CatalogResponseBuilder(const string &name)
171 {
172  return new BESCatalogResponseHandler(name);
173 }
174 
BESStopWatch::start
virtual bool start(std::string name)
Definition: BESStopWatch.cc:58
BESCatalogResponseHandler::execute
virtual void execute(BESDataHandlerInterface &dhi)
Execute the showCatalog command.
Definition: BESCatalogResponseHandler.cc:71
BESCatalog::show_catalog
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
BESNotFoundError
error thrown if the resource requested cannot be found
Definition: BESNotFoundError.h:40
BESResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESResponseHandler.cc:102
BESInfo::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
BESInfo
informational response object
Definition: BESInfo.h:63
BESCatalog::get_catalog_name
virtual std::string get_catalog_name() const
Get the name for this catalog.
Definition: BESCatalog.h:103
BESCatalogList::default_catalog
virtual BESCatalog * default_catalog() const
The the default catalog.
Definition: BESCatalogList.h:118
BESCatalogResponseHandler
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
Definition: BESCatalogResponseHandler.h:47
BESCatalogList::TheCatalogList
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Definition: BESCatalogList.cc:81
BESCatalogResponseHandler::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
Definition: BESCatalogResponseHandler.cc:146
BESDataHandlerInterface::data
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
Definition: BESDataHandlerInterface.h:90
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESResponseHandler
handler object that knows how to create a specific response object
Definition: BESResponseHandler.h:77
BESStopWatch
Definition: BESStopWatch.h:55
BESTransmitter
Definition: BESTransmitter.h:47
BESCatalogResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESCatalogResponseHandler.cc:161
BESCatalogEntry
Definition: BESCatalogEntry.h:46
BESCatalog
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
BESInfo::begin_response
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56