bes  Updated for version 3.20.5
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 BESCatalogResponseHandler::BESCatalogResponseHandler(const string &name) :
50  BESResponseHandler(name)
51 {
52 }
53 
54 BESCatalogResponseHandler::~BESCatalogResponseHandler()
55 {
56 }
57 
68 {
69  BESStopWatch sw;
70  if (BESISDEBUG(TIMING_LOG)) sw.start("BESCatalogResponseHandler::execute", dhi.data[REQUEST_ID]);
71 
72  BESInfo *info = BESInfoList::TheList()->build_info();
73  d_response_object = info;
74 
75  // Remove all of the leading slashes from the container (path) name
76  string container = dhi.data[CONTAINER];
77  string::size_type notslash = container.find_first_not_of("/", 0);
78  if (notslash != string::npos) {
79  container = container.substr(notslash);
80  }
81  if (container.empty()) container = "/";
82 
83  BESCatalog *besCatalog = 0;
84  string catalog = dhi.data[CATALOG];
85  if(catalog.empty()){
86  // Use default catalog to service request
88  catalog = besCatalog->get_catalog_name();
89  }
90  else {
91  // Use the specified catalog.
92  besCatalog = BESCatalogList::TheCatalogList()->find_catalog(catalog);
93  if (!besCatalog) {
94  string err = (string) "Not able to find the catalog '" + catalog + "'";
95  throw BESInternalError(err, __FILE__, __LINE__);
96  }
97  }
98 
99  BESCatalogEntry *entry = 0;
100  // we always want to get the container information from the
101  // default catalog, whether the node is / or not
102  entry = besCatalog->show_catalog(container, entry);
103  if (!entry) {
104  string err = (string) "Failed to find node " + container;
105  throw BESNotFoundError(err, __FILE__, __LINE__);
106  }
107 
108  // now that we have all the catalog entry information, display it
109  // start the response depending on if show catalog or show info
110  info->begin_response(CATALOG_RESPONSE_STR, dhi);
111  dhi.action_name = CATALOG_RESPONSE_STR;
112 
113  // start with the first level entry
114  BESCatalogUtils::display_entry(entry, info);
115 
116  BESCatalogEntry::catalog_citer ei = entry->get_beginning_entry();
117  BESCatalogEntry::catalog_citer ee = entry->get_ending_entry();
118  for (; ei != ee; ei++) {
119  BESCatalogUtils::display_entry((*ei).second, info);
120  info->end_tag("dataset");
121  }
122 
123  info->end_tag("dataset");
124 
125  // end the response object
126  info->end_response();
127 
128  delete entry;
129 }
130 
143 {
144  if (d_response_object) {
145  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
146  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
147  info->transmit(transmitter, dhi);
148  }
149 }
150 
157 void BESCatalogResponseHandler::dump(ostream &strm) const
158 {
159  strm << BESIndent::LMarg << "BESCatalogResponseHandler::dump - (" << (void *) this << ")" << endl;
160  BESIndent::Indent();
162  BESIndent::UnIndent();
163 }
164 
166 BESCatalogResponseHandler::CatalogResponseBuilder(const string &name)
167 {
168  return new BESCatalogResponseHandler(name);
169 }
170 
error thrown if the resource requested cannot be found
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
virtual std::string get_catalog_name() const
Get the name for this catalog.
Definition: BESCatalog.h:103
exception thrown if inernal error encountered
virtual void execute(BESDataHandlerInterface &dhi)
Execute the showCatalog command.
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void dump(ostream &strm) const
dumps information about this object
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
virtual bool start(string name)
Definition: BESStopWatch.cc:57
handler object that knows how to create a specific response object
informational response object
Definition: BESInfo.h:68
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
Structure storing information used by the BES to handle the request.
map< string, string > data
the map of string data that will be required for the current request.
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual BESCatalog * default_catalog() const
The the default catalog.
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object