bes  Updated for version 3.20.10
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 "config.h"
34 
35 #include "BESCatalogResponseHandler.h"
36 #include "BESInfoList.h"
37 #include "BESInfo.h"
38 #include "BESRequestHandlerList.h"
39 #include "BESRequestHandler.h"
40 #include "BESNames.h"
41 #include "BESDataNames.h"
42 #include "BESCatalogList.h"
43 #include "BESCatalog.h"
44 #include "BESCatalogEntry.h"
45 #include "BESCatalogUtils.h"
46 #include "BESSyntaxUserError.h"
47 #include "BESNotFoundError.h"
48 #include "BESDebug.h"
49 #include "BESStopWatch.h"
50 
51 using std::endl;
52 using std::ostream;
53 using std::string;
54 
55 BESCatalogResponseHandler::BESCatalogResponseHandler(const string &name) :
56  BESResponseHandler(name)
57 {
58 }
59 
60 BESCatalogResponseHandler::~BESCatalogResponseHandler()
61 {
62 }
63 
74 {
75  BESStopWatch sw;
76  if (BESDebug::IsSet(TIMING_LOG_KEY)) sw.start("BESCatalogResponseHandler::execute", dhi.data[REQUEST_ID]);
77 
78  BESInfo *info = BESInfoList::TheList()->build_info();
79  d_response_object = info;
80 
81  // Remove all of the leading slashes from the container (path) name
82  string container = dhi.data[CONTAINER];
83  string::size_type notslash = container.find_first_not_of("/", 0);
84  if (notslash != string::npos) {
85  container = container.substr(notslash);
86  }
87  if (container.empty()) container = "/";
88 
89  BESCatalog *besCatalog = 0;
90  string catalog = dhi.data[CATALOG];
91  if(catalog.empty()){
92  // Use default catalog to service request
94  catalog = besCatalog->get_catalog_name();
95  }
96  else {
97  // Use the specified catalog.
98  besCatalog = BESCatalogList::TheCatalogList()->find_catalog(catalog);
99  if (!besCatalog) {
100  string err = (string) "Not able to find the catalog '" + catalog + "'";
101  throw BESInternalError(err, __FILE__, __LINE__);
102  }
103  }
104 
105  BESCatalogEntry *entry = 0;
106  // we always want to get the container information from the
107  // default catalog, whether the node is / or not
108  entry = besCatalog->show_catalog(container, entry);
109  if (!entry) {
110  string err = (string) "Failed to find node " + container;
111  throw BESNotFoundError(err, __FILE__, __LINE__);
112  }
113 
114  // now that we have all the catalog entry information, display it
115  // start the response depending on if show catalog or show info
116  info->begin_response(CATALOG_RESPONSE_STR, dhi);
117  dhi.action_name = CATALOG_RESPONSE_STR;
118 
119  // start with the first level entry
120  BESCatalogUtils::display_entry(entry, info);
121 
122  BESCatalogEntry::catalog_citer ei = entry->get_beginning_entry();
123  BESCatalogEntry::catalog_citer ee = entry->get_ending_entry();
124  for (; ei != ee; ei++) {
125  BESCatalogUtils::display_entry((*ei).second, info);
126  info->end_tag("dataset");
127  }
128 
129  info->end_tag("dataset");
130 
131  // end the response object
132  info->end_response();
133 
134  delete entry;
135 }
136 
149 {
150  if (d_response_object) {
151  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
152  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
153  info->transmit(transmitter, dhi);
154  }
155 }
156 
163 void BESCatalogResponseHandler::dump(ostream &strm) const
164 {
165  strm << BESIndent::LMarg << "BESCatalogResponseHandler::dump - (" << (void *) this << ")" << endl;
166  BESIndent::Indent();
168  BESIndent::UnIndent();
169 }
170 
172 BESCatalogResponseHandler::CatalogResponseBuilder(const string &name)
173 {
174  return new BESCatalogResponseHandler(name);
175 }
176 
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
virtual BESCatalog * default_catalog() const
The the default catalog.
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
virtual void execute(BESDataHandlerInterface &dhi)
Execute the showCatalog command.
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
virtual void dump(std::ostream &strm) const
dumps information about this object
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
virtual std::string get_catalog_name() const
Get the name for this catalog.
Definition: BESCatalog.h:103
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
informational response object
Definition: BESInfo.h:63
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
exception thrown if internal error encountered
error thrown if the resource requested cannot be found
handler object that knows how to create a specific response object
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual bool start(std::string name)
Definition: BESStopWatch.cc:67