bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
SiteMapResponseHandler.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2//
3// SiteMapResponseHandler.cc
4//
5// This file is part of the BES default command set
6//
7// Copyright (c) 2018 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25// Please read the full copyright statement in the file COPYRIGHT_URI.
26//
27
28#include "config.h"
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
33#include <time.h>
34
35#include <cerrno>
36#include <cstring>
37
38#include <sstream>
39#include <fstream>
40
41#include "ShowBesKeyCommand.h"
42#include "SiteMapResponseHandler.h"
43
44
45#include "BESTextInfo.h"
46#include "BESDataNames.h"
47#include "BESNames.h"
48#include "TheBESKeys.h"
49#include "BESCatalog.h"
50#include "BESCatalogList.h"
51
52#include "BESError.h"
53#include "BESDebug.h"
54#include "BESStopWatch.h"
55
56using namespace std;
57
58const auto MODULE = "bes";
59#define prolog string("SiteMapResponseHandler::").append(__func__).append("() - ")
60
61SiteMapResponseHandler::SiteMapResponseHandler(const string &name) :
63{
64}
65
66SiteMapResponseHandler::~SiteMapResponseHandler()
67{
68}
69
81{
82 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timing", &dhi);
83
84 // Force this command to use a TextInfo object. The default Info object type
85 // is set using a key in bes.conf. jheg 11/27/18
86 BESInfo *info = new BESTextInfo(); // BESInfoList::TheList()->build_info();
87 d_response_object = info;
88
89 if (dhi.data[SITE_MAP_RESPONSE] != SITE_MAP_RESPONSE)
90 throw BESInternalError("Not a Site Map command in SiteMapResponseHandler::execute().", __FILE__, __LINE__);
91
92 // remove trailing '/' if present
93 if (*(dhi.data[PREFIX].end()-1) == '/')
94 dhi.data[PREFIX].erase(dhi.data[PREFIX].end()-1);
95
96 ostringstream oss;
97 const BESCatalogList *catalog_list = BESCatalogList::TheCatalogList(); // convenience variable
98
99 info->begin_response(SITE_MAP_RESPONSE_STR, dhi);
100
101 // If no catalog is named, return the concatenation of the site maps for
102 // all of the catalogs. This mimics the way shwoNode returns information
103 // for the path '/'.
104 if (dhi.data[CATALOG].empty()) {
105 BESCatalogList::catalog_citer i = catalog_list->first_catalog();
106 BESCatalogList::catalog_citer e = catalog_list->end_catalog();
107 for (; i != e; ++i) {
108 BESCatalog *catalog = i->second;
109 if (!catalog)
110 throw BESInternalError(string("Build site map found a null catalog in the catalog list."), __FILE__, __LINE__);
111
112 // Don't add 'default' to the prefix value here. For other catalogs, do add their
113 // names to the prefix (i->first is the name of the catalog). This matches the
114 // behavior of showNode, where the non-default catalogs 'appear' in the default
115 // catalog's top level as 'phantom' directories.
116 string prefix = (i->first == catalog_list->default_catalog_name()) ? dhi.data[PREFIX]: dhi.data[PREFIX] + "/" + i->first;
117 catalog->get_site_map(prefix, dhi.data[NODE_SUFFIX], dhi.data[LEAF_SUFFIX], oss, "/");
118
119 info->add_data(oss.str());
120
121 // clearing oss to keep the size of the object small.
122 // Maybe change get_site_map so it takes an Info object? jhrg 11/28/18
123 oss.str("");
124 oss.clear();
125 }
126 }
127 else {
128 BESCatalog *catalog = catalog_list->find_catalog(dhi.data[CATALOG]);
129 if (!catalog)
130 throw BESInternalError(string("Build site map could not find the catalog: ") + dhi.data[CATALOG], __FILE__, __LINE__);
131
132 string prefix = (dhi.data[CATALOG] == catalog_list->default_catalog_name()) ? dhi.data[PREFIX]: dhi.data[PREFIX] + "/" + dhi.data[CATALOG];
133 catalog->get_site_map(prefix, dhi.data[NODE_SUFFIX], dhi.data[LEAF_SUFFIX], oss, "/");
134
135 info->add_data(oss.str());
136
137 // As above, no sense keeping this in memory any longer than needed
138 oss.str("");
139 oss.clear();
140 }
141
142 info->end_response();
143}
144
157{
158 if (d_response_object) {
159 BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
160 if (!info) throw BESInternalError("Could not get the Info object in SiteMapResponseHandler::transmit()", __FILE__, __LINE__);
161 info->transmit(transmitter, dhi);
162 }
163}
164
171void SiteMapResponseHandler::dump(ostream &strm) const
172{
173 strm << BESIndent::LMarg << "SiteMapResponseHandler::dump - (" << (void *) this << ")" << std::endl;
174 BESIndent::Indent();
176 BESIndent::UnIndent();
177}
178
180SiteMapResponseHandler::SiteMapResponseBuilder(const string &name)
181{
182 return new SiteMapResponseHandler(name);
183}
List of all registered catalogs.
virtual std::string default_catalog_name() const
The name of the default catalog.
virtual catalog_citer end_catalog() const
Iterator to the last catalog.
virtual catalog_citer first_catalog() const
Iterator to the first catalog.
Catalogs provide a hierarchical organization for data.
Definition BESCatalog.h:51
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 add_data(const std::string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition BESInfo.cc:151
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
represents simple text information in a response object, such as version and help information.
Definition BESTextInfo.h:48
Response handler that returns a site map.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object