bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
ShowNodeResponseHandler.cc
1// ShowNodeResponseHandler.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) 2018 OPeNDAP, Inc
7// Author: James Gallagher <jgallagher@opendap.org>
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 OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27#include <memory>
28#include <string>
29
30#include "BESInfoList.h"
31#include "BESInfo.h"
32#include "BESCatalogList.h"
33#include "BESCatalog.h"
34
35#include "BESNames.h"
36#include "BESDataNames.h"
37
38#include "BESDebug.h"
39#include "BESUtil.h"
40#include "BESStopWatch.h"
41
42#include "CatalogNode.h"
43#include "CatalogItem.h"
44#include "ShowNodeResponseHandler.h"
45
46using namespace bes;
47using namespace std;
48
49#define MODULE "bes"
50#define prolog string("ShowNodeResponseHandler::").append(__func__).append("() - ")
51
62{
63 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timing", &dhi);
64
65 // Get the container. By convention, the path can start with a slash,
66 // but doesn't have too. However, get_node() requires the leading '/'.
67 string container = dhi.data[CONTAINER];
68
69 BESDEBUG(MODULE, prolog << "Requested container: " << container << endl);
70
71 // Look for the name of a catalog in the first part of the pathname in
72 // 'container' and, if found, set 'catalog' to that catalog. The value
73 // null is used as a sentinel that the path in 'container' does not
74 // name a catalog.
75 const BESCatalog *catalog = nullptr;
76
77 vector<string> path_tokens;
78 BESUtil::tokenize(container, path_tokens);
79 if (!path_tokens.empty()) {
80
81 catalog = BESCatalogList::TheCatalogList()->find_catalog(path_tokens[0]);
82 if (catalog) {
83 string catalog_name = catalog->get_catalog_name();
84
85 // Remove the catalog name from the start of 'container.' Now 'container'
86 // is a relative path within the catalog.
87 container = container.substr(container.find(catalog_name) + catalog_name.size());
88
89 BESDEBUG(MODULE, prolog << "Modified container/path value to: " << container << endl);
90 }
91 }
92
93 // if we found no catalog name in the first part of the container name/path,
94 // use the default catalog.
95 if (!catalog) {
96 catalog = BESCatalogList::TheCatalogList()->default_catalog();
97 if (!catalog) throw BESInternalError(string("Could not find the default catalog."), __FILE__, __LINE__);
98 }
99
100 BESDEBUG(MODULE, prolog << "Using the '" << catalog->get_catalog_name() << "' catalog."<< endl);
101 BESDEBUG(MODULE, prolog << "use_container: " << container << endl);
102
103 // Get the node info from the catalog.
104 unique_ptr<CatalogNode> node(catalog->get_node(container));
105
106 // If the path name passed into this command is '/' we are using the default
107 // catalog and must add any other catalogs to the set of nodes shown at the
108 // top level. Since the variable 'container' may have been modified above,
109 // use the value of dhi.data[CONTAINER] for the path passed to the command.
110 if (dhi.data[CONTAINER] == "/") {
111
112 BESDEBUG(MODULE, prolog << "Adding additional catalog nodes to top level node." << endl);
113
114 auto i = BESCatalogList::TheCatalogList()->first_catalog();
115 auto e = BESCatalogList::TheCatalogList()->end_catalog();
116 for (; i != e; i++) {
117 string catalog_name = i->first;
118 BESCatalog *cat = i->second;
119
120 BESDEBUG(MODULE, prolog << "Checking catalog '" << catalog_name << "' ptr: " << (void *) cat << endl);
121
122 if (cat != BESCatalogList::TheCatalogList()->default_catalog()) {
123 auto *collection = new CatalogItem(catalog_name, 0, BESUtil::get_time(), false, CatalogItem::node);
124 node->add_node(collection);
125
126 BESDEBUG(MODULE, prolog << "Added catalog node " << catalog_name << " to top level node." << endl);
127 }
128 }
129 }
130
131 BESInfo *info = BESInfoList::TheList()->build_info();
132
133 // Transfer the catalog's node info to the BESInfo object
134 info->begin_response(NODE_RESPONSE_STR, dhi);
135
136 // calls encode_item()
137 node->encode_node(info);
138
139 info->end_response();
140
141 // set the state and return
142 dhi.action_name = NODE_RESPONSE_STR;
143 d_response_object = info;
144}
145
158{
159 if (d_response_object) {
160 BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
161 if (!info) throw BESInternalError("Expected the Response Object to be a BESInfo instance.", __FILE__, __LINE__);
162 info->transmit(transmitter, dhi);
163 }
164}
165
172void ShowNodeResponseHandler::dump(ostream &strm) const
173{
174 strm << BESIndent::LMarg << "ShowNodeResponseHandler::dump - (" << (void *) this << ")" << endl;
175 BESIndent::Indent();
177 BESIndent::UnIndent();
178}
179
181ShowNodeResponseHandler::ShowNodeResponseBuilder(const string &name)
182{
183 return new ShowNodeResponseHandler(name);
184}
185
Catalogs provide a hierarchical organization for data.
Definition BESCatalog.h:51
virtual std::string get_catalog_name() const
Get the name for this catalog.
Definition BESCatalog.h:102
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.
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:120
exception thrown if internal error encountered
handler object that knows how to create a specific response object
void dump(std::ostream &strm) const override
dumps information about this object
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
Definition BESUtil.cc:995
static std::string get_time(bool use_local_time=false)
Definition BESUtil.cc:1017
Evaluate a showNode 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
virtual void execute(BESDataHandlerInterface &dhi)
Execute the showNode command.